我正在创建一个简单的动画,两个笔划作为蒸汽从一杯咖啡中升起。我已经设置了2个形状层的动画。我将这两个层添加到两个子视图中,并使用UIView.animateWithDuration将它们垂直地添加到动画中,然后用一个小偏移量淡出。
我想要重复此动画,但仅当我的按钮被选中时。使用UIViewAnimationOptions,我可以将选项设置为.Repeat,但它们将永远继续。我已经尝试了我能想到的所有方法来停止动画,就像你将在我的代码末尾看到的那样,但是动画不会消失。
在我的类的顶部,在我的绘图代码之前有var buttonSelected = false,我用它来检查和查看我的steam动画是否应该运行。我还使用该布尔值运行一个函数,该函数根据按钮是否被选中而将.Repeat或nil返回给我的UIView动画选项。
回到我的视图控制器中,我在steam动画类的实例中设置了buttonSelected,我有一个函数告诉类.setNeedsDisplay()。我的理解是这个函数会导致我的类的实例刷新它自己。这似乎适用于更新从类中修改的图形的颜色,但也许我需要调用其他东西来正确刷新动画的删除?
//Turn steam elements into Shape Layers then into Views
colorRed.setFill()
shapePath.fill()
//Turn steam shapes into shape layers
var steam1 = CAShapeLayer()
var steam2 = CAShapeLayer()
steam1.path = shape2Path.CGPath
steam2.path = shape3Path.CGPath
steam1.fillColor = transparent.CGColor
steam2.fillColor = transparent.CGColor
//Create the views for the steam
var steam1View = UIView()
var steam2View = UIView()
self.addSubview(steam1View)
self.addSubview(steam2View)
steam1View.layer.addSublayer(steam1)
steam2View.layer.addSublayer(steam2)
func shouldRepeat()->UIViewAnimationOptions{
if repeatSteam == true {
return .Repeat
}
else {
return nil
}
}
//Set initial properties for steam layers & views
steam1.fillColor = colorRed.CGColor
steam2.fillColor = colorRed.CGColor
steam1View.alpha = 1.0
steam2View.alpha = 1.0
steam1View.transform = CGAffineTransformMakeTranslation(0.0, 20)
steam2View.transform = CGAffineTransformMakeTranslation(0.0, 30)
//Animate Steam 1
UIView.animateWithDuration(1.5, delay: 0.0, options:shouldRepeat(), animations: {
steam1View.transform = CGAffineTransformMakeTranslation(0.0, -10)
steam1View.alpha = 0.0
}, completion: nil)
//Animate Steam 2
UIView.animateWithDuration(1.9, delay: 0.0, options:shouldRepeat(), animations: {
steam2View.transform = CGAffineTransformMakeTranslation(5, -10)
steam2View.alpha = 0.0
}, completion: nil)
//Check the buttonSelected state and stop the steam animations if false
if self.buttonSelected == false {
//Make smoke transparent
steam1.fillColor = transparent.CGColor
steam2.fillColor = transparent.CGColor
steam1.setNeedsDisplay()
steam2.setNeedsDisplay()
println("Smoke should dissapear")
steam1.hidden = true
steam2.hidden = true
self.layer.removeAllAnimations()
self.layer.setNeedsDisplay()
//steam2View.layer.removeAllAnimations()
//Fill cup with green
colorGreen.setFill()
shapePath.fill()
}发布于 2015-03-27 04:58:48
您可以执行以下操作:
steam1.layer.removeAllAnimations()
steam1.transform = // reset to original state
steam.alpha = 1 // reset to original alpha level
steam2.layer.removeAllAnimations()
...您的动画可能会在动画之间的某个位置停止,因此如果需要,请重置这些值。
发布于 2016-06-23 09:48:35
对于UIButton:
self.Button.layer.removeAllAnimations()https://stackoverflow.com/questions/25477959
复制相似问题