如何暂停CADisplayLink?(How to pause a CADisplayLink?)

编程入门 行业动态 更新时间:2024-10-19 11:43:42
如何暂停CADisplayLink?(How to pause a CADisplayLink?)

我的应用有问题。 我想要发生的是当我点击button2时,它消失并停止移动。 现在发生的事情是,当我点击button2时,它会消失,但不会停止移动(即使它隐藏起来)。任何帮助? 码:

@IBOutlet var label: UILabel! @IBOutlet var label2: UILabel! @IBOutlet var label3: UILabel! @IBOutlet var button2: UIButton! @IBAction func button3(sender: UIButton) { label.hidden = false button2.hidden = true } @IBOutlet var button4: UIButton! @IBAction func button5(sender: UIButton) { button4.hidden = true label2.hidden = false } @IBAction func button1(sender: UIButton) { label.hidden = true label2.hidden = true button2.hidden = false button2.frame = CGRectMake(120, 400, 100, 100) let displayLink = CADisplayLink(target: self, selector: "handleDisplayLink:") displayLink.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode) let time = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), 2 * Int64(NSEC_PER_SEC)) dispatch_after(time, dispatch_get_main_queue()) { self.button4.hidden = false self.button4.frame = CGRectMake(120, 400, 100, 100) let displayLink1 = CADisplayLink(target: self, selector: "handleDisplayLink1:") displayLink1.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)} } func handleDisplayLink(displayLink: CADisplayLink) { var buttonFrame = button2.frame buttonFrame.origin.y += -2 button2.frame = buttonFrame if button2.frame.origin.y <= 50 { displayLink.invalidate() label3.hidden = false button2.hidden = true } } func handleDisplayLink1(displayLink1: CADisplayLink) { var button4Frame = button4.frame button4Frame.origin.y += -2 button4.frame = button4Frame if button4.frame.origin.y <= 50 { displayLink1.invalidate() label3.hidden = false button4.hidden = true } } override func viewDidLoad() { super.viewDidLoad() label.hidden = true button2.hidden = true label2.hidden = true button4.hidden = true label3.hidden = true // Do any additional setup after loading the view, typically from a nib. }

谢谢。 安东

I have a problem in my app. What I want to happened is when I click button2, it disappears and stops moving. What's happening now is that when I click button2, it disappears but doesn't stop moving (even while its hidden).Any help? Code:

@IBOutlet var label: UILabel! @IBOutlet var label2: UILabel! @IBOutlet var label3: UILabel! @IBOutlet var button2: UIButton! @IBAction func button3(sender: UIButton) { label.hidden = false button2.hidden = true } @IBOutlet var button4: UIButton! @IBAction func button5(sender: UIButton) { button4.hidden = true label2.hidden = false } @IBAction func button1(sender: UIButton) { label.hidden = true label2.hidden = true button2.hidden = false button2.frame = CGRectMake(120, 400, 100, 100) let displayLink = CADisplayLink(target: self, selector: "handleDisplayLink:") displayLink.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode) let time = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), 2 * Int64(NSEC_PER_SEC)) dispatch_after(time, dispatch_get_main_queue()) { self.button4.hidden = false self.button4.frame = CGRectMake(120, 400, 100, 100) let displayLink1 = CADisplayLink(target: self, selector: "handleDisplayLink1:") displayLink1.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)} } func handleDisplayLink(displayLink: CADisplayLink) { var buttonFrame = button2.frame buttonFrame.origin.y += -2 button2.frame = buttonFrame if button2.frame.origin.y <= 50 { displayLink.invalidate() label3.hidden = false button2.hidden = true } } func handleDisplayLink1(displayLink1: CADisplayLink) { var button4Frame = button4.frame button4Frame.origin.y += -2 button4.frame = button4Frame if button4.frame.origin.y <= 50 { displayLink1.invalidate() label3.hidden = false button4.hidden = true } } override func viewDidLoad() { super.viewDidLoad() label.hidden = true button2.hidden = true label2.hidden = true button4.hidden = true label3.hidden = true // Do any additional setup after loading the view, typically from a nib. }

Thank you. Anton

最满意答案

这非常令人困惑,因为不清楚称为button2和button4的两个插座与称为button1 , button3和button5的@IBAction方法之间的关系? 你有多少个按钮? 二? 四? 五? 这些各种标签是什么? 如果他们不是问题的一部分,他们不应该包含在代码片段中。

但是如果它是被调用的button3或button5 ,那么是的,那些隐藏按钮,但不会invalidate显示链接invalidate ,所以显示链接将会进行。 如果您希望它停止显示链接,那么您必须调用invalidate :

var displayLink: CADisplayLink? var displayLink1: CADisplayLink? @IBAction func button3(sender: UIButton) { label.hidden = false button2.hidden = true displayLink?.invalidate() displayLink = nil } @IBAction func button5(sender: UIButton) { button4.hidden = true label2.hidden = false displayLink1?.invalidate() displayLink1 = nil }

这显然意味着button5应该使用这些属性,而不是使用displayLink和displayLink1局部变量:

@IBAction func button1(sender: UIButton) { label.hidden = true label2.hidden = true button2.hidden = false button2.frame = CGRectMake(120, 400, 100, 100) // NB: No `let` on the next line displayLink = CADisplayLink(target: self, selector: "handleDisplayLink:") displayLink?.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode) let time = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), 2 * Int64(NSEC_PER_SEC)) dispatch_after(time, dispatch_get_main_queue()) { self.button4.hidden = false self.button4.frame = CGRectMake(120, 400, 100, 100) // NB: No `let` on the next line self.displayLink1 = CADisplayLink(target: self, selector: "handleDisplayLink1:") self.displayLink1?.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode) } }

另外一些意见:

如果您正在使用自动布局,则应该非常谨慎地调整这些控件的frame 。 在自动布局中,如果您碰巧做了任何事情来触发应用约束引擎(例如更新这些标签的text ),则控件将回到约束定义的位置。

你正在改变这些控件的frame ,每次调用2点。 随着您的应用程序变得越来越复杂,这可能最终会导致动画UI的结局或变化。 您不应每次更新显示链接处理程序的固定数量,而是使用一些基于时间的函数来确定已经过多少时间,并根据该时间计算新的坐标。

我个人不会使用这种东西的显示链接。 我只想使用标准的UIView.animateWithDuration ,然后当我想停止button2移动时,我会:

let currentButton2Frame = button2.layer.presentationLayer()!.frame button2.layer.removeAllAnimations() button2.frame = currentButton2Frame

这标识它的位置,动画中期,停止动画,并将frame重置为先前标识的坐标。 这避免了显示链接的复杂性。

This is exceedingly confusing because it's unclear as to the relationship between the two outlets called button2 and button4 and the @IBAction methods called button1, button3, and button5? How many buttons do you actually have? Two? Four? Five? And what these various labels? If they're not part of the question, they shouldn't be included in the code snippet.

But if it's button3 or button5 that's getting called, then yes, those hide the button, but don't invalidate the display link, so the display link will progress. If you want it to stop the display link, then you have to call invalidate:

var displayLink: CADisplayLink? var displayLink1: CADisplayLink? @IBAction func button3(sender: UIButton) { label.hidden = false button2.hidden = true displayLink?.invalidate() displayLink = nil } @IBAction func button5(sender: UIButton) { button4.hidden = true label2.hidden = false displayLink1?.invalidate() displayLink1 = nil }

This obviously means that button5 should be using these properties, not using local variables for displayLink and displayLink1:

@IBAction func button1(sender: UIButton) { label.hidden = true label2.hidden = true button2.hidden = false button2.frame = CGRectMake(120, 400, 100, 100) // NB: No `let` on the next line displayLink = CADisplayLink(target: self, selector: "handleDisplayLink:") displayLink?.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode) let time = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), 2 * Int64(NSEC_PER_SEC)) dispatch_after(time, dispatch_get_main_queue()) { self.button4.hidden = false self.button4.frame = CGRectMake(120, 400, 100, 100) // NB: No `let` on the next line self.displayLink1 = CADisplayLink(target: self, selector: "handleDisplayLink1:") self.displayLink1?.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode) } }

A few additional observations:

If you're using auto-layout, you should be very wary about just adjusting the frame of these controls. In auto-layout, if you happen to do anything to trigger the constraints engine to be applied (e.g. update the text of these labels), the controls will move back to where the constraints defined them to be.

You're changing the frame of these controls, moving them 2 points per call. That's might end up with stuttering or changing of speeds of the animation UI as your app gets more complicated. You shouldn't update a fixed amount per call of the display link handler, but rather use some time based functions to determine how much time has elapsed and calculate the new coordinates from that.

I personally wouldn't use display links for this kind of stuff. I would just use the standard UIView.animateWithDuration, and then when I wanted to stop button2 from moving, I would:

let currentButton2Frame = button2.layer.presentationLayer()!.frame button2.layer.removeAllAnimations() button2.frame = currentButton2Frame

This identifies where it is, mid-animation, stops the animation, and resets the frame to the coordinates previously identified. This avoids the complexities of display links.

更多推荐

本文发布于:2023-08-04 08:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1412542.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:CADisplayLink   pause

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!