Я хочу создать анимацию, которая включает несколько объектов UIImage.В свете наших исследований я пытаюсь добавить анимацию к слою UIImages.
Как вы можете видеть из приведенных ниже кодов, я создал анимацию для каждого UIImages и добавил эти анимации в слои 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)
}