简体   繁体   English

中断animateWithDuration

[英]Interrupting animateWithDuration

I've got an animated timer bar that counts down, and I want to be able to cancel the animation and reset the bar when certain conditions are met so it doesn't get all the way to 0. Similarly, if it does make it to 0 I want to call a function. 我有一个倒计时的动画计时器栏,我希望能够在满足某些条件时取消动画并重置该栏,以使它不会一直到0。类似地,如果确实做到了到0我想调用一个函数。

For now I just have a button hooked up for testing. 现在,我只是挂了一个按钮进行测试。 Pressing the button again does keep it from getting all the way down, but it springs across the screen, and the print statement in completion block always comes back true. 再次按下该按钮不会使其完全滑落,但是会在屏幕上弹起,并且完成语句中的print语句始终返回true。 I think multiple animations are getting piled a top one another. 我认为多个动画正相互叠加。

How does one stop an animation? 如何停止动画?

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.
}

Setting the options to AllowUserInteration in the new usingSpringWithDamping animation call can do this if I am understanding your question properly. 如果我正确地理解了您的问题,那么在新的usingSpringWithDamping动画调用中将选项设置为AllowUserInteration可以做到这一点。

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

Additionally, the session on Building Interruptible and Responsive Interactions from the WWDC 2014 talk here: https://developer.apple.com/videos/wwdc/2014/ might be a useful resource (they explain how this works in greater detail). 此外,来自WWDC 2014的构建可中断和响应式交互的会话可在以下位置进行讨论: https : //developer.apple.com/videos/wwdc/2014/可能是有用的资源(它们详细解释了它是如何工作的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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