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

3
我正在创建一个简单的动画,其中两个笔画从一杯咖啡中升起,如同蒸汽。我已经设置了2个形状图层的动画。我将这两个图层添加到2个子视图中,并使用UIView.animateWithDuration使它们向上垂直移动并逐渐消失,带有小的偏移量。
我希望这个动画重复播放,但只在我的按钮被选中时。使用UIViewAnimationOptions,我可以将选项设置为.Repeat,但然后它们会无限期地继续。我已经尝试了我所能想到的一切来停止动画,正如你在我的代码末尾看到的那样,但是动画不会消失。
在我的类顶部的绘图代码之前,我有一个var buttonSelected = false,我用它来检查和查看我的蒸汽动画是否应该运行。我还使用该布尔值来运行一个函数,根据按钮是否被选中返回.Repeatnil到我的UIView动画选项。
回到我的视图控制器,在我的蒸汽动画类实例中设置我的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()
        }
2个回答

5

你可以做:

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

您的动画可能会在动画过程中停止,因此如有需要,请重置值。

0

对于 UIButton:

self.Button.layer.removeAllAnimations()

请访问此链接,它将有助于提高您的内容质量。 - Willie Cheng

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接