简体   繁体   中英

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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