How to update opacity using swift?
Can someone please provide me with an example of animating image opacity in swift ??
I couldn't even find a good example in object c
func showCorrectImage(element: AnyObject){
var animation : CABasicAnimation = CABasicAnimation(keyPath: "opacity");
animation.delegate = self
animation.fromValue = NSValue(nonretainedObject: 0.0)
animation.toValue = NSValue(nonretainedObject: 1.0)
animation.duration = 1.0
element.layer?.addAnimation(animation, forKey: nil)
}
I think I have most of this right (not really sure though), can someone help me?
element = image view
Thanks in advance! ~
+5
source to share
3 answers
You can also write it like this:
animation.fromValue = 0.0
animation.toValue = 1.0
All the code should be like this:
let animation = CABasicAnimation(keyPath: "opacity")
animation.delegate = self
animation.fromValue = 0.0
animation.toValue = 1.0
animation.duration = 1.0
animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
element.layer?.addAnimation(animation, forKey: "fade")
+3
source to share