我是对UIImageView进行子类化,因此每次设置图像属性时都会发生动画。以下是成功的:
.card:nth-child(n+1):nth-child(-n+4) {
@include span(4 of 20); }
.card:nth-child(5) {
@include span(4 of 20 last); }
这并不奇怪。我将UIImageView子类化并添加了一个名为' img'的全新变量,后者又修改了UIImageView的图像'属性。
问题是最终用户可能会改变AnimatedImageView的图像'属性。
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;
})
}
}
这确实会导致堆栈溢出,因为当我调用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;
})
}
}
时,它会反复调用我在子类中重写的setter方法。那么,什么是覆盖'图像'的getter / setter方法的正确方法。 UIImageView上的属性
答案 0 :(得分:7)
只需使用super.image
代替,这将阻止循环播放。
答案 1 :(得分:0)
使用super.image的另一种方法是通过ivar _image引用图像。它直接引用它,而不调用getter,并避免循环。
import UIKit
class AnimatedImageView: UIImageView {
override var image: UIImage! {
get {
return _image
}
set {
_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;
})
}