I'm subclassing a UIImageView, so that each time the image property is set an animation occurs. The following was successful:
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 no surprise. I subclassed UIImageView and added an entirely new variable called 'img', which in turn modifies UIImageView's 'image' property.
The issue is that the end-user could conceivably still alter AnimatedImageView's '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;
})
}
}
Sure enough this causes a stackoverflow because when I call self.image = newValue
it repeatedly calls the setter method that I've overridden in my subclass. So, what's the right way to override the getter/setter methods of the 'image' property on UIImageView
Just use super.image
instead, that will prevent a loop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With