简体   繁体   中英

How do I invalidate CADisplayLink with a UIButton?

Extreme noob. I define a CADisplayLink, then I want to invalidate it later using a UIButton. I get the error 'cannot find in scope' because (I assume) the link is initialized inside the 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) }

@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()
 }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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