I will subclass UIImageView so that every time an image property is set, animation happens. The following has been completed:
import UIKit class AnimatedImageView: UIImageView { var img: UIImage! { get { return self.image } set { self.image = newValue UIView.animateWithDuration(0.5, delay: 0.4, usingSpringWithDamping: 0.2, initialSpringVelocity: 5.0, options: .CurveEaseIn, animations: {_ in self.transform = CGAffineTransformMakeScale(1.1, 1.1); }, completion: {_ in self.transform = CGAffineTransformIdentity; }) } }
This is not surprising. I subclassed UIImageView and added a brand new variable called "img", which in turn changes the image property of UIImageView.
The problem is that the end user can apparently still modify the AnimatedImageView 'image' property.
import UIKit class AnimatedImageView: UIImageView { override var image: UIImage! { get { return self.image } set { self.image = newValue UIView.animateWithDuration(0.5, delay: 0.4, usingSpringWithDamping: 0.2, initialSpringVelocity: 5.0, options: .CurveEaseIn, animations: {_ in self.transform = CGAffineTransformMakeScale(1.1, 1.1); }, completion: {_ in self.transform = CGAffineTransformIdentity; }) } }
Of course, this calls stackoverflow, because when I call self.image = newValue , it repeatedly calls the setter method, which I redefined in my subclass. So what is the right way to override the getter / setter methods of the image property in a UIImageView
source share