首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >中断animateWithDuration

中断animateWithDuration
EN

Stack Overflow用户
提问于 2014-11-01 08:30:10
回答 1查看 542关注 0票数 0

我有一个倒计时的动画计时器条,我希望能够取消动画,并在满足特定条件时重置该计时器条,这样它就不会一直到0。类似地,如果它真的变成了0,我想调用一个函数。

现在,我只连接了一个按钮进行测试。再次按下按钮确实可以防止它一直向下,但它会跳过屏幕,并且完成块中的print语句总是返回true。我认为多个动画相互堆叠在一起。

如何停止动画?

代码语言:javascript
复制
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.
}
EN

回答 1

Stack Overflow用户

发布于 2014-11-01 10:45:35

如果我正确理解了您的问题,那么在新的usingSpringWithDamping动画调用中将选项设置为AllowUserInteration可以做到这一点。

代码语言:javascript
复制
animateWithDuration:delay:usingSpringWithDamping:initialSpringVelocity:options:animations:completion:

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26685174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档