1

我的应用程序有问题。我想要发生的是当我单击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.  
}

谢谢你。安东

4

2 回答 2

0

这非常令人困惑,因为不清楚称为 and 的两个出口button2与称为,和?button4@IBAction方法之间的关系。你实际上有多少个按钮?二?四?五?这些各种各样的标签是什么?如果它们不是问题的一部分,则不应将它们包含在代码片段中。button1button3button5

但是,如果它button3button5那被调用,那么是的,那些隐藏按钮,但不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应该使用这些属性,而不是对displayLinkand使用局部变量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)
    }
}

一些额外的观察:

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

  2. 您正在更改frame这些控件,每次调用将它们移动 2 点。随着您的应用程序变得越来越复杂,这可能会导致动画 UI 的速度出现卡顿或变化。您不应该在每次调用显示链接处理程序时更新固定数量,而是使用一些基于时间的函数来确定已经过去了多少时间并从中计算新坐标。

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

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

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

于 2015-12-11T01:46:46.657 回答
0

您需要存储对您在按钮单击处理程序中创建的显示链接的引用。因此,为它们创建类级属性:

@IBAction func button5(sender: UIButton) {
    button4.hidden = true
    label2.hidden = false
}

// new code here:
var displayLink: CADisplayLink?
var displayLink1: CADisplayLink?

然后在创建它们之前使它们无效,以防实例已经存在:

@IBAction func button1(sender: UIButton) {
   displayLink?.invalidate()
   displayLink1?.invalidate()

   displayLink = CADisplayLink(target: self, selector: "handleDisplayLink:")
   displayLink1 = CADisplayLink(target: self, selector: "handleDisplayLink1:")

   displayLink = 
   label.hidden = true
   label2.hidden = true
   button2.hidden = false

   // ... rest of your method
}
于 2015-12-10T21:34:24.820 回答