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

dcb*_*nji 3 animation repeat uiviewanimation swift ios8

我正在创建一个简单的动画,其中有两个笔画从一杯咖啡中冒出蒸汽。我已经设置了2个形状层的动画。我将这两个图层添加到2个子视图中,并UIView.animateWithDuration用于垂直设置它们的动画并逐渐淡出,且偏移很小。

我希望重复此动画,但是仅在选择我的按钮时才可以。使用UIViewAnimationOptionsi可以将选项设置为.Repeat,但是它们将永远持续下去。我已经尽力想尽一切办法停止动画,就像您在代码结尾看到的那样,但是动画不会消失。

var buttonSelected = false在班级顶部的绘图代码之前有,我正在使用它检查并查看我的Steam动画是否应该运行。我还使用该布尔值来运行返回.Repeat或返回nilUIView动画选项的函数,具体取决于是否选择了按钮。

回到我buttonSelected在我的Steam动画类实例中设置我的视图控制器的位置,我有一个告诉该类的函数.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()
        }
Run Code Online (Sandbox Code Playgroud)

Hen*_*ijn 5

你可以做:

steam1.layer.removeAllAnimations()
steam1.transform = // reset to original state
steam.alpha = 1 // reset to original alpha level
steam2.layer.removeAllAnimations()
...
Run Code Online (Sandbox Code Playgroud)

您的动画可能会在动画之间停下来,因此如果需要,请重置值。