Swift 1.2 is broken when using constant value within nested blocks - ios

I made a bouncing animation (with some wisdom here to prevent flickering) with the following UIView animation block:
UIView.animateWithDuration(0.3, delay: 0.0,
options: .Repeat | .Autoreverse | .CurveEaseInOut,
animations: {
UIView.setAnimationRepeatCount(1.5)
cellToAnimate?.layer.position.y -= 25.0
}) { (finished) in
UIView.animateWithDuration(0.3, delay: 0.0,
options: .CurveEaseInOut,
animations: {
cellToAnimate?.layer.position.y += 25.0
}, completion: nil)
}
This worked perfectly fine, but then when I abstracted out the constant '25.0' from the two blocks like so:
let yDisplacement = 25.0
UIView.animateWithDuration(0.3, delay: 0.0,
options: .Repeat | .Autoreverse | .CurveEaseInOut,
animations: {
UIView.setAnimationRepeatCount(1.5)
cellToAnimate?.layer.position.y -= yDisplacement
}) { (finished) in
UIView.animateWithDuration(0.3, delay: 0.0,
options: .CurveEaseInOut,
animations: {
cellToAnimate?.layer.position.y += yDisplacement
}, completion: nil)
}
... the Swift compiler started to complain:
Could not find member 'Repeat'.
I couldn't for the life of me figure out why this was happening, until I realised via binary chop that the problem was the constant.
Anybody know why this happens? Or whether this is fixed in Swift 2.0?

When upgrading this code to Swift 2.0 the error also points to .Repeat but states:
Type of expression is ambiguous without more context.
I managed to fix it by being explicit with the yDisplacement type:
let yDisplacement: CGFloat = 25.0
As you cannot use the -= or += operators on CGFloat and Double operands.
This error seems terribly misleading as it is highlighting completely the wrong part of code.
Final code:
let yDisplacement: CGFloat = 25.0
UIView.animateWithDuration(0.3, delay: 0.0,
options: [.Repeat, .Autoreverse, .CurveEaseInOut], // Swift 2.0, use .Repeat | .Autoreverse | .CurveEaseInOut in Swift 1.2
animations: {
UIView.setAnimationRepeatCount(1.5)
cellToAnimate?.layer.position.y -= yDisplacement
}) { (finished) in
UIView.animateWithDuration(0.3, delay: 0.0,
options: .CurveEaseInOut,
animations: {
cellToAnimate?.layer.position.y += yDisplacement
}, completion: nil)
}

The problem is that type of yDisplacement is defined as Double, but you cant mix types in arithmetic operations. In your case you want to subtract it from CGFloat position.y.
Just define type explicitly:
let yDisplacement: CGFloat = 25.0

Related

How can I Use Alpha Extension For All Object in swift?

I have below code for changing the value of object like text,image , ... with Effect. and it work:
UIView.animate(withDuration: 1.0, delay: 0.0, options: UIViewAnimationOptions.curveEaseOut, animations: {
self.lblCityTemp.alpha = 0.0
self.imgCityIcon.alpha = 0.0
}, completion: {
(finished: Bool) -> Void in
//Once the label is completely invisible, set the text and fade it back in
self.imgCityIcon.image = UIImage(named: icon)
self.lblCityTemp.text = "\(temp)°"
// Fade in
UIView.animate(withDuration: 1.0, delay: 0.0, options: UIViewAnimationOptions.curveEaseIn, animations: {
self.lblCityTemp.alpha = 1.0
self.imgCityIcon.alpha = 1.0
}, completion: nil)
})
Now I want make a extension of this code that I use for all my object that I write below code:
extension NSObject {
func Fade (alphaOUT: Double,alphaIN: Double, input: Any) {
UIView.animate(withDuration: 1.0, delay: 0.0, options: UIViewAnimationOptions.curveEaseOut, animations: {
self.alpha = CGFloat(alphaOUT)
}, completion: {
(finished: Bool) -> Void in
// Fade in
UIView.animate(withDuration: 1.0, delay: 0.0, options: UIViewAnimationOptions.curveEaseIn, animations: {
self.alpha = CGFloat(alphaIN)
}, completion: nil)
})
}
}
But I have error on self word and I do not know how can I set value for every object like label, imageView , ....
How can I do this?
Is the extension is best way or no?
If no, so what is the best way?
The whole idea should be take some UIView and animate it. Then you need extension of UIView since this class has properties that you need to.
extension UIView { ... }
Anyway, what now if you need to do something when animation ends? Then you'll need completion handler parameter for your method
func fade(..., completion: #escaping () -> Void = { }) {
... which will be called after the first animation ends.
Next suggestions:
You can say that alpha parameters should be of type CGFloat, then you don’t have to convert Double to CGFloat
Also, what is input? I think you won't need it with this solution
Method name should start with small capital letter and for naming parameters you should use camelCase
extension UIView {
func fade(alphaOut: CGFloat, alphaIn: CGFloat, completion: #escaping () -> Void = { }) {
UIView.animate(withDuration: 1, delay: 0, options: .curveEaseOut, animations: {
self.alpha = alphaOut
}, completion: { _ in
completion() // this is called when animation ends
UIView.animate(withDuration: 1, delay: 0, options: .curveEaseIn, animations: {
self.alpha = alphaIn
})
})
}
}
Then when you need to call it, change what you need after animation ends in completion closure
imgCityIcon.fade(alphaOut: ___, alphaIn: ___) {
self.imgCityIcon.image = UIImage(named: icon)
}
Note that you're using old syntax for some stuff:
For example UIViewAnimationOptions.curveEaseIn has been renamed to UIView.AnimationOptions.curveEaseIn. I would suggest you to start using newer versions of Swift
You need to implement an extension for UIView instead of NSObject.
since NSObject doesn't have any property like alpha you will get an
error.
Coding Example:
extension UIView {
func Fade (alphaOUT: Double,alphaIN: Double, input: Any) {
UIView.animate(withDuration: 1.0, delay: 0.0, options: UIView.AnimationOptions.curveEaseOut, animations: {
self.alpha = CGFloat(alphaOUT)
}, completion: {
(finished: Bool) -> Void in
// Fade in
UIView.animate(withDuration: 1.0, delay: 0.0, options: UIView.AnimationOptions.curveEaseIn, animations: {
self.alpha = CGFloat(alphaIN)
}, completion: nil)
})
}
}
class AnimationHelper {
class func Fade (alphaOUT: Double,alphaIN: Double, input: Any , yourLabel: UILabel, yourTextField: UITextField) {
UIView.animate(withDuration: 1.0, delay: 0.0, options: UIView.AnimationOptions.curveEaseOut, animations: {
yourLabel.alpha = CGFloat(alphaOUT)
yourTextField.alpha = CGFloat(alphaOUT)
}, completion: {
(finished: Bool) -> Void in
// Fade in
UIView.animate(withDuration: 1.0, delay: 0.0, options: UIView.AnimationOptions.curveEaseIn, animations: {
// Access your text field and label here.
}, completion: nil)
})
}
}

Swift skips one animation (UIView.animate)

I have a logoImage on a view, animating the constraints.
The constraints animated are width and height ; going from original state of 220.0 to 240.0 (works fine) and THEN I would like to make it go back to 220.0 but the animation is skipped and the image goes directly back in 0.1second to 220.0.
Here is the code of the animation
func animate() {
self.imageHeightConstraint.constant = 240.0
self.imageWidthConstraint.constant = 240.0
UIView.animate(withDuration: 1.0,
delay:0.0,
options: .curveEaseIn,
animations:{
self.logoImage.layoutIfNeeded()
}, completion: { (finished: Bool) -> Void in
self.imageHeightConstraint.constant = 220.0
self.imageWidthConstraint.constant = 220.0
UIView.animate(withDuration: 2.0,
delay:0.0,
options: .curveEaseIn,
animations: {
self.logoImage.layoutIfNeeded()
})
})
}
Thanks in advance for any help I could get! ;-)
You need to call self.logoImage.setNeedsLayout() within your completion block, before you re-set the constants. This since you need to invalidate the current layout of the receiver and trigger a layout update during the next update cycle.
Try this code instead and it will work for you:
func animate() {
self.imageHeightConstraint.constant = 240.0
self.imageWidthConstraint.constant = 240.0
UIView.animate(withDuration: 1.0,
delay:0.0,
options: .curveEaseIn,
animations:{
self.logoImage.layoutIfNeeded()
}, completion: { (finished: Bool) -> Void in
self.logoImage.setNeedsLayout()
self.imageHeightConstraint.constant = 220.0
self.imageWidthConstraint.constant = 220.0
UIView.animate(withDuration: 2.0,
delay:0.0,
options: .curveEaseIn,
animations: {
self.logoImage.layoutIfNeeded()
})
})
}

High CPU Usage because of UIView.animate and UIView.animateKeyframes

I have two view controllers. AnimationVC has some UIView animations, and DestinationVC has none.
I have a CPU usage problem. After I perform the segue, the animation blocks still show up in the Instruments, even though these lines belong to the AnimationVC that performs the segue.
UIView.animate(withDuration: 1.0, delay: 0.0, options: [.curveEaseInOut, .autoreverse, .repeat], animations: {
self.s1.alpha = 0.0
self.s3.alpha = 0.0
}, completion: nil)
and
let dur = 0.5/12
UIView.animateKeyframes(withDuration: 30.0, delay: 0.0, options: [.repeat, .calculationModeCubic], animations: {
UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: dur, animations: {
self.sc.alpha = 1.0
})
UIView.addKeyframe(withRelativeStartTime: 1.0-dur, relativeDuration: dur, animations: {
self.sc.alpha = 0.0
})
UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 1.0, animations: {
self.sc.transform = CGAffineTransform(rotationAngle: .pi*0.2)
})
UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 1.0, animations: {
self.sc.center.y = 0.1*self.frame.size.height
})
}, completion: nil)
I tried calling this destruct function...
func destruct(){
layer.removeAllAnimations()
subviews.forEach { $0.removeFromSuperview() }
}
...in
just before performing the segue
in the AnimationVC's deinit method
Still, it shows alive and hogs the CPU with 40% load. How can I destruct this?
I even reset the navigation stack on the DestinationVC with...
self.navigationController?.viewControllers = [self]
...and I see the lines of deinit from AnimationVC, the animation delay closure is still alive.
As it turns out
...
}) { (finished) in
if finished {
self.specialAniamtion(delay: 2.0)
}
}
and calling destruct() on deinit fixed it.
func destruct(){
layer.removeAllAnimations()
}

Overriding withRowAnimation styles for row insertion in Swift

I'm using an insertRowAtIndexPath function to insert a series of rows inside my tableview.
However, the in-build animation styles (.Fade, .Top, .Bottom e.t.c.) do not fill my needs entirely.
I need to create my own custom animations, i know this can be done by overriding the the UITableViewRowAnimation, but i'm not sure how this should be handled.
Looking forward to your advices.
Appreciate any insights.
Try This in your UITableView subclass.
override func insertRowsAtIndexPaths(indexPaths: [NSIndexPath], withRowAnimation animation: UITableViewRowAnimation) {
super.insertRowsAtIndexPaths(indexPaths, withRowAnimation: UITableViewRowAnimation.None)
self.endUpdates()
self.beginUpdates()
for indexPath:NSIndexPath in indexPaths {
let cell:UITableViewCell? = (super.cellForRowAtIndexPath(indexPath))
if cell != nil {
var frame:CGRect = (cell?.frame)!
frame.origin.x = (cell?.frame.size.width)!
cell?.frame = frame
frame.origin.x = 0
let animationBlock = { () -> Void in
cell!.frame = frame;
}
if UIView.respondsToSelector(Selector("animateWithDuration(duration: , delay: , usingSpringWithDamping dampingRatio: , initialSpringVelocity velocity: , options: , animations: , completion: ")) {
// UIView.animateWithDuration(0.3, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.0, options:0.0, animations: animationBlock, completion:nil)
UIView.animateWithDuration(0.3, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: animationBlock, completion: nil)
} else {
UIView.animateWithDuration(0.3, delay: 0.0, options:UIViewAnimationOptions.CurveEaseIn, animations: animationBlock, completion: nil)
}
}
}
}

Setting options in animateWithDuration in Swift 2

I am animating a textfield with the following code:
UIView.animateWithDuration(0.5, delay: 0.4,
options: .Repeat | .Autoreverse | .CurveEaseOut, animations: {
self.password.center.x += self.view.bounds.width
}, completion: nil)
I get the error Could not find member 'Repeat'
The code only works if I set only one option. Why is it not letting me set multiple options?
The syntax for combining OptionSetType has changed, you will be able to do it like this:
UIView.animateWithDuration(0.5, delay: 0.4,
options: [.Repeat, .CurveEaseOut, .Autoreverse], animations: {
self.password.center.x += self.view.bounds.width
}, completion: nil)

Resources