Animation text does not appear in my UIView object - swift

I am using a UIObject to display an animation using Lottie framework which is a submit button, and when this UIView object is tapped, the animation is played.
However the text within this animation does not appear, and the dimensions are also wrong. My UIView object has dimensions of 193*33 and the animation also does. Here is my code;
let SubmitButton = LOTAnimationView(name: "submit_195_33")
self.SubmitViewName.addSubview(SubmitButton)
SubmitButton.frame = CGRect(x: 0, y: 0, width: 195, height: 33)
SubmitButton.contentMode = .scaleAspectFill
SubmitButton.play()
My UIView object is larger then the animation even if I use this code, and the animation is super small like this:

Maybe you can try setting constraints to the button, for example:
submitButton.translatesAutoresizingMaskIntoConstraints = false
submitButton.widthAnchor.constraint(equalToConstant: 195).isActive = true
submitButton.heightAnchor.constraint(equalToConstant: 33).isActive = true

Related

insertSubview() covers the whole Mainview even though inserted at 0

I insert a UIView into a UIButton with following code:
let backgroundView = UIView()
backgroundView.backgroundColor = .red
backgroundView.translatesAutoresizingMaskIntoConstraints = false
blueButton.insertSubview(backgroundView, at: 0)
NSLayoutConstraint.activate([
backgroundView.leadingAnchor.constraint(equalTo: blueButton.leadingAnchor),
backgroundView.trailingAnchor.constraint(equalTo: blueButton.trailingAnchor),
backgroundView.topAnchor.constraint(equalTo: blueButton.topAnchor),
backgroundView.bottomAnchor.constraint(equalTo: blueButton.bottomAnchor)
])
The promlem is that the backgroundView covers the whole button and I only see the red field instead of the button (its an image) with a red background (my subView).
This is the storyboard part of my image. I don´t do something else in the code with this buttons with the exception of trying to put a subView into:
My goal is to have a circle background like in the picture below. For example the red circle button has a rounded red (but brighter than the buttons red) backgroundView.The standard background of the button should stay the same.
I also tried to send the subView into the background but it didn´t work for me:
blueButton.sendSubviewToBack(backgroundView)
I reduced the alpha value of the subview to see if the button image is still there.
for view in blueButton.subviews {
view.alpha = 0.5
}
And it is:
How can I solve this problem?
Do not inject extra subviews into a button. If the goal is to make the background color of the button red, set its backgroundColor to .red. If the goal is to put something behind the button image, that is what the backgroundImage is for. Or just make the button image look like the image you really want.
I made this button:
It looks a lot like your picture. Here's the code:
let im = UIGraphicsImageRenderer(size: CGSize(width: 30, height: 30)).image {
ctx in
UIColor.red.withAlphaComponent(0.2).setFill()
ctx.cgContext.fillEllipse(in: CGRect(x: 0, y: 0, width: 30, height: 30))
UIColor.red.setFill()
ctx.cgContext.fillEllipse(in: CGRect(x: 5, y: 5, width: 20, height: 20))
}
b.setImage(im.withRenderingMode(.alwaysOriginal), for: .normal)
I think that's a lot better than messing with unwanted illegal subviews.

I am trying to create a subclass of UIImageView in swift to add a simple counter

I am trying to create a subclass of a UIImageView so that I can add a simple counter that will be used to decide whether the image flips horizontally or not when tapped. I read the instructions Apple provides for inheritance and it looks to me as if I'm doing it right. I'm sure its something easy.
Here's the attempt:
class flippableImage: UIImageView {
var flipNumber:Int = 0
}
Then I want to be able to change that property when I tap the image. When I go assign flipNumber a value when setting up the object it says that "UIImageView" has no member "flipNumber". Yes, I initialized the subclass and not the parent class. What am I doing?
EDIT to show creation:
let imageChosen = UIImage(named: availableImages[NSUserDefaults.standardUserDefaults().integerForKey("image")])
image = Face(image: imageChosen)
image.frame = CGRect(x: self.view.frame.size.width/2, y: self.view.frame.height/2, width: (faceChosen?.size.width)! * 0.75, height: (faceChosen?.size.height)! * 0.75)
image.userInteractionEnabled = true
image.multipleTouchEnabled = true
backgroundImage.addSubview(image)
backgroundImage.userInteractionEnabled = true
image.center = self.view.center
image.flipNumber = 0 //this doesn't work
Within the ViewController, set each image's tag property to its counter's value.

Swift get Y coordinate of current element

I got some UILabel which can be pressed to open a menu. When pressed, they call my function with the label itself as a parameter. I make a popover that behave correctly in phone view but I would like to position it when it is on ipad view.
My problem is that I can not get the exact position of the UILabel that was pressed.
I tried
myYCoordinates = label.bounds.origin.y
or
myYCoordinates = label.frame.origin.y
to
optionMenu.popoverPresentationController?.sourceRect = CGRectMake(self.view.bounds.size.width / 2, myYCoordinates, 1.0, 1.0)
But I cant get the good coordinate.
I don't know if it is because my label are in a tableview with 2 section or if there is a way at all.
Thank in advance for the help.
Here is my sceen (cropped du to development confidentiality)
Here is what append when I press my label
And here is what I would like ti to look like
I tried
optionMenu.popoverPresentationController?.sourceRect = label.frame
Based on #Özgür Erzil answer, I found out that I could simply use
optionMenu.popoverPresentationController?.sourceView = label
optionMenu.popoverPresentationController?.sourceRect = CGRectMake(label.frame.width, label.frame.height / 2, 1.0, 1.0)
By setting the source view to my label and using the label width and height to position it.
Thank
if you create your UILabel dynamically, try to set the position when you set it like:
CGRectMake(x, y, width, height)
var label = UILabel(frame: CGRectMake(0, 0, 200, 20))
or if it is in storyBoard, you have to use constraint methods and use autolayout. Explained here

How to make image switch positions

How can I make an image start at a predetermined location on screen, and when tapped, appear at a different location, also predetermined. And when tapped then, return to the original location?
Thanks
PS: Working on Xcode 6 - Swift
If you are using an UIImageView, simply change its frame. (Let me know if you're using autolayouts):
let imageView = UIImageView(frame: CGRectMake(0, 0, width, height))
imageView.image = UIImage(named: "assetName")
then when clicked (you can detect by overlaying a clear button over your image, etc):
imageView.frame = CGRectMake(100, 100, width, height)
etc.
Every UIView has a frame property of type CGRect which specifies both its size and position. To change the position of a view, you thus have to modify the x and y values of the frame's origin, the location of the upper-left point of the framing rectangle:
let view = UIView()
// Set location and size
view.frame = CGRectMake(0, 0, width, height)
// Modify location
view.frame.origin = CGPointMake(newX, newY)
Depending on what type your view has, there are different approaches to react to taps on the view. A button, for example, can specify a target function that is called when it's tapped.
Generally speaking, you can attach a UIGestureRecognzier (such as a UITapGestureRecognizer) to detect gestures the user performed on the given UIView. You also need to make sure its userInteractionEnabled property is set to true so that the interaction can be detected in the first place.

Mask a View with the alpha from another view in Swift

I've been struggling for a view hours to figure out how to get my view masked by a shape that is in another view. Basically I have a circular countdown timer that I want to be masked out by an animating circle that scales up from the center of the timer when the timer is reset.
I tried setting timerMask.maskView = timerCircleGrahics where timerCircleGraphics is the name of my timer animation view. But this is giving me very strange results when I test the app. It seems to clip the view to the rectangle bounds of my mask view rather than the alpha of the bounds that are drawn within that view. The mask layer is centered and being drawn properly, but I've never attempted this before so am not sure if I am doing it right.
Here is the class for my mask shape:
class timerBackgroundMask: UIView {
override func drawRect(rect: CGRect) {
var ovalPath = UIBezierPath(ovalInRect: CGRectMake(0, 0, 238, 238))
colorGreen.setFill()
ovalPath.fill()
}
}
Then using IB, I assign this mask to a manually placed View in my Storyboard called timerMask. I am realizeing now that by assigning timerBackgroundMask class to timerMask I have programatically added a subview to my manually placed Storyboard view, but I feel like the alpha should come through just the same when set this view to mask out anotherview. Here is the code i use to set the mask
timerCircleGraphics.layer.mask = timerMask.layer
The result I am getting is pretty weird:
The red portion should be a circle that is partially clipped by my timerMask from the center outward. The light green circle that you see is simply the background view of the counter, however it happens to be the exact position and size as my timerBackgroundMask for reference.
Don't bother creating a class and using the Storyboard. Do it straight in code, the simple way:
var ovalPath = UIBezierPath(ovalInRect: CGRectMake(0, 0, 238, 238))
colorGreen.setFill()
ovalPath.fill()
var mask = CAShapeLayer()
mask.path = ovalPath.CGPath
timerCircleGraphics.layer.mask = mask