简体   繁体   English

在UICollectionViewCell中观察图层框架大小的变化

[英]Observe layer frame size change in UICollectionViewCell

Is it possible to observe self.layer.frame.size change in UICollectionViewCell ? 是否有可能观察self.layer.frame.size变革UICollectionViewCell

I tried to register observer do it like this: 我试图注册观察者这样做:

override func awakeFromNib() {
    super.awakeFromNib()
    self.layer.addObserver(self, forKeyPath: "frame", options: NSKeyValueObservingOptions(rawValue: 0), context: nil)
}

And then observe like this: 然后像这样观察:

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    print(keyPath ?? "NIL")
}

And it came out that observeValue method has been never called. 结果是从未调用过observeValue方法。

You can override the var frame and add your function on didSet 您可以覆盖var框架并在didSet上添加函数

override var frame: CGRect {
    didSet {
        //do something
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM