簡體   English   中英

如何使用 UIButton 使 CADisplayLink 無效?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM