我有一个名为 - (woowwww)的ImageView: ImageView
@IBOutlet weak var ImageView: UIImageView!
...我想在每次ImageView的transform
- 属性发生变化时触发事件。
每次ImageView被旋转时都应该调用一个事件:
self.ImageView.transform = CGAffineTransform(rotationAngle: CGFloat(10))
为了达到这个目的,我尝试使用某种 KVO ,但显然它没有按预期工作......
override func viewDidLoad() {
super.viewDidLoad()
self.ImageView.addObserver(self.ImageView.transform, forKeyPath: "test", options: .new, context: nil)
}
func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutableRawPointer) {
print("changed")
}
任何帮助都将非常感谢!感谢。
答案 0 :(得分:0)
在keyPath
中设置“transform”,如下所示:
imgView.addObserver(self, forKeyPath: "transform", options: .new, context: nil)
然后添加以下代码:
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if let newValue = change?[.newKey] as? NSObject {
print("Changed \(newValue)")
}
希望它会对你有所帮助。
答案 1 :(得分:0)
keyPath
应该是nameOfObject.property
所以如果imageView名称是dd来收听转换它应该是dd.transform
试试这个
class ViewController: UIViewController {
@IBOutlet weak var dd: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.addObserver(self, forKeyPath: "dd.transform", options: .new, context: nil)
self.dd.transform = CGAffineTransform(rotationAngle: CGFloat(10))
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if let newValue = change?[.newKey] as? NSObject {
print("Changed \(newValue)")
}
}}