简体   繁体   English

UIView.animate和完成问题

[英]Trouble with UIView.animate and completion

I have trouble with UIView.animate. 我在UIView.animate上遇到麻烦。

The functionality I want is (seemingly) simple: There is one button, when you keep it pressed, the whole screen is slowly filled by a different color from the bottom (achieved by a UIView called bar that moves upwards to the top, eventually filling the whole screen). 我想要的功能很简单(看似):有一个按钮,当您按住它时,整个屏幕将慢慢填充底部不同的颜色(通过名为bar的UIView实现,该控件向上移动到顶部,最终填充整个屏幕)。 When the bar reaches the top, the background changes its color to the color of the bar and the bar moves back to its original state, hidden from view, and changes its color. 当条形图到达顶部时,背景将其颜色更改为条形图的颜色,并且该条形图将移回其原始状态(从视图中隐藏)并更改其颜色。 When the person releases the button before the bar reaches the top, the bar moves down again very quickly. 当人员在栏杆到达顶部之前释放按钮时,栏杆将再次快速向下移动。

Right now I have solved it as follows: 现在,我已经解决了以下问题:

  @IBAction func buttonPressed(_ sender: Any) {        
    UIView.animate(withDuration: 3, animations: {
        self.bar.frame.origin.y = 0
    }, completion: {(finished: Bool) in
        if self.bar.frame.origin.y == 0 {

            self.backColor = self.barColor
            self.view.backgroundColor = UIColor(rgb: self.colors[self.backColor])

            self.changeBarColor()
            self.bar.backgroundColor = UIColor(rgb: self.colors[self.barColor])

            self.bar.frame.origin.y = self.view.frame.size.height

        }
    })

}


@IBAction func buttonReleased(_ sender: Any) {

    UIView.animate(withDuration: 0.5, animations: {
            self.bar.frame.origin.y = self.view.frame.size.height
    })
}

The trouble appears when the user releases the button and presses it again before the 3 seconds of the original animation is over. 当用户释放按钮并在原始动画的3秒结束之前再次按下按钮时,就会出现问题。 Suddenly everything inside the completion block is executed. 突然,完成块中的所有内容都会执行。 I tried mitigating the problem with the if statement, but this does not seem to work. 我尝试使用if语句缓解问题,但这似乎不起作用。

What I believe needs to happen is that when the user releases the button before the first animation is finished, the animation needs to be cancelled. 我认为需要发生的是,当用户在第一个动画完成之前释放按钮时,需要取消动画。 Could someone point me in the right direction? 有人可以指出我正确的方向吗?

I have attached a video to illustrate the problem: 我已附上视频以说明问题:

Video of Problem 问题录像

The issue you are facing is because the previous animation is still executing. 您面临的问题是因为先前的动画仍在执行。 The key is to remove the previous animation. 关键是要删除以前的动画。

@IBAction func buttonPressed(_ sender: Any) {     

if self.bar.frame.origin.y != self.view.frame.size.height
{
    view.layer.removeAllAnimations()
    UIView.animate(withDuration: 3, animations: {
        self.bar.frame.origin.y = 0
    }, completion: {(finished: Bool) in
         self.backColor = self.barColor
         self.view.backgroundColor = UIColor(rgb: self.colors[self.backColor])
         self.changeBarColor()
         self.bar.backgroundColor = UIColor(rgb: self.colors[self.barColor])
         self.bar.frame.origin.y = self.view.frame.size.height
    })
}
else
{   
    UIView.animate(withDuration: 3, animations: {
        self.bar.frame.origin.y = 0
    }, completion: {(finished: Bool) in
        if self.bar.frame.origin.y == 0 {

            self.backColor = self.barColor
            self.view.backgroundColor = UIColor(rgb: self.colors[self.backColor])

            self.changeBarColor()
            self.bar.backgroundColor = UIColor(rgb: self.colors[self.barColor])

            self.bar.frame.origin.y = self.view.frame.size.height

        }
    })
}
}

@IBAction func buttonReleased(_ sender: Any) {
    view.layer.removeAllAnimations()
    UIView.animate(withDuration: 0.5, animations: {                
            self.bar.frame.origin.y = self.view.frame.size.height
    })
}

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

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