简体   繁体   English

如何使用 UIButton 使 CADisplayLink 无效?

[英]How do I invalidate CADisplayLink with a UIButton?

Extreme noob.极端菜鸟。 I define a CADisplayLink, then I want to invalidate it later using a UIButton.我定义了一个 CADisplayLink,然后我想稍后使用 UIButton 使其无效。 I get the error 'cannot find in scope' because (I assume) the link is initialized inside the func.我收到错误“在范围内找不到”,因为(我假设)链接是在 func 内部初始化的。 Cannot figure out how to "get inside" the func to invaidate it.无法弄清楚如何“进入”该函数来入侵它。 Code: ``` func myDisplayLink() {let displayLink = CADisplayLink(target: self, selector: #selector(myCycle)) displayLink.add(to: .current, forMode: RunLoop.Mode.default) }代码:``` 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: 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.

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