【问题标题】:Interrupting animateWithDuration中断 animateWithDuration
【发布时间】:2014-11-01 00:30:09
【问题描述】:

我有一个倒计时的动画计时器栏,我希望能够在满足某些条件时取消动画并重置该栏,这样它就不会一直为 0。同样,如果它确实使它变为 0 我想调用一个函数。

现在我只有一个用于测试的按钮。再次按下按钮确实可以防止它一直向下,但它会在屏幕上弹出,并且完成块中的打印语句总是返回 true。我认为多个动画彼此堆叠在一起。

如何停止动画?

var countDownBar = UIView()
var button       = UIButton()


override func viewDidLoad() {
    super.viewDidLoad()

    // Place the countDownBar in the center of the view
    countDownBar.frame = CGRectMake(0, 0, 150, 15)
    countDownBar.center = view.center
    countDownBar.backgroundColor = UIColor.blueColor()
    view.addSubview(countDownBar)

    // Add in a button
    button  = UIButton.buttonWithType(UIButtonType.System) as UIButton
    button.frame = CGRectMake(0, 0, 50, 20)
    button.center = CGPointMake(view.center.x, view.center.y + 30)
    button.backgroundColor = UIColor.lightGrayColor()
    button.setTitle("button", forState: UIControlState.Normal)
    button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
    view.addSubview(button)

}

// Do this when the button is pressed
func buttonAction (sender: UIButton) {

    // Shrink the bar down to 0 width
    UIView.animateWithDuration(3.0,
        animations: ({

            self.countDownBar.frame = CGRectMake(self.countDownBar.frame.minX, self.countDownBar.frame.minY, 0, self.countDownBar.frame.height)

        }),
        completion: {finished in

            // When finished, reset the bar to its original length
            self.countDownBar.frame = CGRectMake(0, 0, 150, 15)
            self.countDownBar.center = self.view.center

            // Display if completed fully or interrupted
            if finished {
                print("animation finished")
            } else {
                print("animation interrupted")
            }
    })
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

【问题讨论】:

    标签: ios swift animatewithduration


    【解决方案1】:

    如果我正确理解您的问题,在新的 usingSpringWithDamping 动画调用中将选项设置为 AllowUserInteration 即可。

    animateWithDuration:delay:usingSpringWithDamping:initialSpringVelocity:options:animations:completion:
    

    此外,WWDC 2014 演讲中关于构建可中断和响应式交互的会议在这​​里:https://developer.apple.com/videos/wwdc/2014/ 可能是一个有用的资源(他们更详细地解释了这是如何工作的)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-12
      • 1970-01-01
      相关资源
      最近更新 更多