[英]Interrupting animateWithDuration
我有一個倒計時的動畫計時器欄,我希望能夠在滿足某些條件時取消動畫並重置該欄,以使它不會一直到0。類似地,如果確實做到了到0我想調用一個函數。
現在,我只是掛了一個按鈕進行測試。 再次按下該按鈕不會使其完全滑落,但是會在屏幕上彈起,並且完成語句中的print語句始終返回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.
}
如果我正確地理解了您的問題,那么在新的usingSpringWithDamping動畫調用中將選項設置為AllowUserInteration可以做到這一點。
animateWithDuration:delay:usingSpringWithDamping:initialSpringVelocity:options:animations:completion:
此外,來自WWDC 2014的構建可中斷和響應式交互的會話可在以下位置進行討論: https : //developer.apple.com/videos/wwdc/2014/可能是有用的資源(它們詳細解釋了它是如何工作的)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.