无法停止重复UIView.animateWithDuration(Swift)

时间:2014-08-25 01:34:58

标签: animation swift ios8 uiviewanimation repeat

我正在制作一个简单的动画,从一杯咖啡中升起两个笔画作为蒸汽。我已经设置了2个形状图层的动画。我将这两个图层添加到2个子视图中,并使用UIView.animateWithDuration将它们垂直设置为动画并淡出,并使用较小的偏移量。

我希望这个动画重复,但只有在我的按钮被选中时才会重复。使用UIViewAnimationOptions我可以将选项设置为.Repeat,但随后会永久继续。我已经尝试了一切我能想到的停止动画,就像我将在代码末尾看到的那样,但动画不会消失。

我的var buttonSelected = false,在我的班级顶部的绘图代码之前,我用来检查我的蒸汽动画是否应该运行。我也在使用bool来运行一个函数,该函数将.Repeatnil返回到我的UIView动画选项,具体取决于是否选中了该按钮。

回到我的视图控制器中,我在我的Steam动画类的实例中设置了buttonSelected,我有一个函数告诉类.setNeedsDisplay()。我的理解是这个函数会导致我的类实例刷新自己。这似乎可以用于更新我在同一类中修改的Graphic的颜色,但也许我需要调用其他东西来正确刷新动画的删除?

//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()
        }

2 个答案:

答案 0 :(得分:4)

你可以这样做:

steam1.layer.removeAllAnimations()
steam1.transform = // reset to original state
steam.alpha = 1 // reset to original alpha level
steam2.layer.removeAllAnimations()
...

您的动画可能会停留在动画之间的某个位置,因此如果需要,请重置值。

答案 1 :(得分:0)

对于UIButton:

self.Button.layer.removeAllAnimations()