CATransaction动画问题

时间:2019-04-02 16:34:36

标签: swift xcode cashapelayer catransaction

我有一个CAShapeLayer,并尝试设置一些属性的动画。我一开始确实使用过CABasicAnimation,但是由于这会不断循环,因此我需要最有效的方法来实现它。

以下代码可正确设置动画效果:

    private func animateDidBecomeStraight () {
            CATransaction.flush()
            CATransaction.begin()
            CATransaction.setAnimationDuration(0.2)
                self.myShape.fillColor = UIColor.Red.cgColor
                self.myShape.lineWidth = 1
                self.myShape.strokeColor = UIColorRed.cgColor
            CATransaction.commit()
    }

 private func animateDidBecomeUnStraight () {

        CATransaction.begin()
        CATransaction.setAnimationDuration(0.5)
            self.myShape.fillColor = UIColor(white: 1, alpha: 1).cgColor
            self.myShape.strokeColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5).cgColor
            self.myShape.lineWidth = 0.1
        CATransaction.commit()

    }

海峡和不平直地被叫来,永远不会重复。

问题

第一次,动画以适当的持续时间工作。当海峡第一次打电话和不直电话第一次打电话时。在那之后,他们似乎失去了动画时间。变化似乎是即时的。

1 个答案:

答案 0 :(得分:1)

CATransaction不是执行此操作的方法。创建两个CABasicAnimation对象,一个用于直线,另一个用于非直线。将他们加入一个CAAnimationGroup中。现在将CAAnimationGroup配置为重复。

相关问题