[英]Definitively, do you have to invalidate() a CADisplayLink when the controller disappears?
[英]How do I invalidate CADisplayLink with a UIButton?
极端菜鸟。 我定义了一个 CADisplayLink,然后我想稍后使用 UIButton 使其无效。 我收到错误“在范围内找不到”,因为(我假设)链接是在 func 内部初始化的。 无法弄清楚如何“进入”该函数来入侵它。 代码:``` func myDisplayLink() {let displayLink = CADisplayLink(target: self, selector: #selector(myCycle)) displayLink.add(to: .current, forMode: RunLoop.Mode.default) }
@IBAction func OFF(_ sender: UIButton)
{ ????? }
```
You can declare it as an optional outside the scope of your function, at the root of your class then just initialize it in your function, like so:
class: SomeClass {
var displayLink: CADisplayLink?
func myDisplayLink() {
displayLink = CADisplayLink(target: self, selector: #selector(myCycle))
displayLink!.add(to: .current, forMode: RunLoop.Mode.default)
}
@IBAction func OFF(_ sender: UIButton) {
displayLink!.invalidate()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.