如何在UIView中正确定义CAAnimation重复计数?

时间:2018-12-10 15:14:44

标签: ios swift core-animation

我想创建包含一些UIImage对象的动画。鉴于调查不足,我正在尝试向UIImages层添加动画。

您可以看到以下代码,我为每个UIImage创建了动画,并将这些动画添加到了UIImages层。之后,我将所有这些UIImages添加到UIView中成为子视图。

问题是,当我将此动画类(它从UIView继承)添加到另一个视图时,它根据确定的动画属性工作。但是动画只能播放一次。实际上,我希望动画重复播放直到停止为止。如果我想确定每个动画的重复计数,则它们是异步工作的。它没有按照正确的顺序进行动画处理。

最后,我必须刷新相关视图中的所有动画,或者我必须计算动画持续时间,我不知道可能是我使用了错误的方式。 我怎么解决这个问题?或者,如果有其他方法,您可以与我分享吗?

import UIKit

class ActivityIndicator: UIView {

// MARK: - Initialization

init()
{
    super.init(frame: CGRect(x: 50, y: 300, width: 144, height: 124))
    self.setupLayers()
}
required init?(coder aDecoder: NSCoder)
{
    super.init(coder: aDecoder)
    self.setupLayers()
}

// MARK: - I added some UIImage and their animations. Begin times are nested.

private func setupLayers() {

    let repeatCount:Float = 0

    let yellowBubble = UIImageView(image: #imageLiteral(resourceName: "yellowBubble"))
    yellowBubble.frame = CGRect(x: 11.8, y: 0, width: 132.084549, height: 104.150719)
    yellowBubble.contentMode = .scaleAspectFill
    yellowBubble.layer.contentsGravity = CALayerContentsGravity.center

    let blackBubble = UIImageView(image: #imageLiteral(resourceName: "blackBubble"))
    blackBubble.frame = CGRect(x: 0, y: 17.685972, width:132.084549, height: 104.150719)
    blackBubble.contentMode = .scaleAspectFill
    blackBubble.layer.contentsGravity = CALayerContentsGravity.center

    let shadowLine = UIImageView(image: #imageLiteral(resourceName: "shadowBubble"))
    shadowLine.frame = CGRect(x: 0, y: 17.685972, width: 132.084549, height: 86.464752)
    shadowLine.contentMode = .scaleAspectFill
    shadowLine.layer.contentsGravity = CALayerContentsGravity.center

    let textAndSign = UIImageView(image: #imageLiteral(resourceName: "logoAndSign"))
    textAndSign.frame = CGRect(x: 28.82774, y: 44.301903, width: 81, height: 44)
    textAndSign.contentMode = .scaleAspectFill
    textAndSign.layer.contentsGravity = CALayerContentsGravity.center

    // Animations have a few properties that includes duration,beginTime etc.
    let allLogoAnimation = CASpringAnimation()
    allLogoAnimation.beginTime = self.layer.convertTime(CACurrentMediaTime(), from: nil) + 2
    allLogoAnimation.duration = 3
    allLogoAnimation.repeatCount = repeatCount
    allLogoAnimation.fillMode = CAMediaTimingFillMode.forwards
    allLogoAnimation.keyPath = "transform.rotation.z"
    allLogoAnimation.toValue = 0
    allLogoAnimation.fromValue = 15
    self.layer.add(allLogoAnimation, forKey: "allLogoAnimation")

    let yellowBubbleAnimation = CASpringAnimation()
    yellowBubbleAnimation.beginTime = self.layer.convertTime(CACurrentMediaTime(), from: nil)
    yellowBubbleAnimation.duration = 3
    yellowBubbleAnimation.repeatCount = repeatCount
    yellowBubbleAnimation.speed = 1.2
    yellowBubbleAnimation.fillMode = CAMediaTimingFillMode.forwards
    yellowBubbleAnimation.keyPath = "transform.translation.y"
    yellowBubbleAnimation.toValue = 0
    yellowBubbleAnimation.fromValue = -70
    yellowBubble.layer.add(yellowBubbleAnimation, forKey: "yellowBubbleAnimation")

    let blackBubbleAnimation = CASpringAnimation()
    blackBubbleAnimation.beginTime = self.layer.convertTime(CACurrentMediaTime(), from: nil) + 0.3
    blackBubbleAnimation.duration = 3
    yellowBubbleAnimation.repeatCount = repeatCount
    blackBubbleAnimation.speed = 1.2
    blackBubbleAnimation.fillMode = CAMediaTimingFillMode.forwards
    blackBubbleAnimation.keyPath = "transform.translation.x"
    blackBubbleAnimation.toValue = 0
    blackBubbleAnimation.fromValue = -70
    blackBubble.layer.add(blackBubbleAnimation, forKey: "blackBubbleAnimation")

    let shadowLineAnimation = CASpringAnimation()
    shadowLineAnimation.beginTime = self.layer.convertTime(CACurrentMediaTime(), from: nil) + 0.6
    shadowLineAnimation.duration = 3
    shadowLineAnimation.repeatCount = repeatCount
    shadowLineAnimation.speed = 1.2
    shadowLineAnimation.fillMode = CAMediaTimingFillMode.forwards
    shadowLineAnimation.keyPath = "transform.translation.x"
    shadowLineAnimation.toValue = 0
    shadowLineAnimation.fromValue = 70
    shadowLine.layer.add(shadowLineAnimation, forKey: "shadowLineAnimation")

    let textAndSignScaleAnimation = CASpringAnimation()
    textAndSignScaleAnimation.beginTime = self.layer.convertTime(CACurrentMediaTime(), from: nil) + 1
    textAndSignScaleAnimation.duration = 3
    textAndSignScaleAnimation.repeatCount = repeatCount
    textAndSignScaleAnimation.fillMode = CAMediaTimingFillMode.forwards
    textAndSignScaleAnimation.keyPath = "transform.scale.xy"
    textAndSignScaleAnimation.toValue = 1
    textAndSignScaleAnimation.fromValue = 3
    textAndSign.layer.add(textAndSignScaleAnimation, forKey: "textAndSignScaleAnimation")

    let textAndSignOpacityAnimation = CASpringAnimation()
    textAndSignOpacityAnimation.beginTime = self.layer.convertTime(CACurrentMediaTime(), from: nil) + 1
    textAndSignOpacityAnimation.duration = 3
    textAndSignOpacityAnimation.repeatCount = repeatCount
    textAndSignOpacityAnimation.fillMode = CAMediaTimingFillMode.forwards
    textAndSignOpacityAnimation.keyPath = "opacity"
    textAndSignOpacityAnimation.toValue = 1
    textAndSignOpacityAnimation.fromValue = 0
    textAndSign.layer.add(textAndSignOpacityAnimation, forKey: "textAndSignOpacityAnimation")

    self.layer.anchorPoint = CGPoint(x: 0.5, y: 0.5)
    self.layer.contentsGravity = CALayerContentsGravity.center
    self.backgroundColor = .clear
    // I added all UIImages to ActivityIndicatorView
    self.addSubviews(yellowBubble,blackBubble,shadowLine,textAndSign)
}
}

之后,我将动画视图添加到了视图控制器中。

 override func viewWillAppear(_ animated: Bool) {

    let activityView = ActivityIndicator()
    activityView.backgroundColor = .clear
    self.view.addSubview(activityView)

}

0 个答案:

没有答案