我们正试图沿着z访问动画切换2个图像视图的位置。 2个imageViews是屏幕{2}的2位 到目前为止,我已尝试使用CAAnimationGroup进行位置和旋转,但它只显示其中一个图像,并且看起来位置和旋转都已关闭。这就是代码的样子:
@IBOutlet weak var meMatchImageView: UIImageView!
@IBOutlet weak var theyMatchImageView: UIImageView!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
let groupAnimation = CAAnimationGroup()
groupAnimation.beginTime = 0.0
groupAnimation.duration = 1.5
groupAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut
)
groupAnimation.delegate = self
let rotationAnimation = CAKeyframeAnimation(keyPath: "transform.rotation")
rotationAnimation.values = [0, 0.14, 0]
rotationAnimation.keyTimes = [0, 0.5, 0]
let positionZAnimation = CABasicAnimation(keyPath: "position.z")
positionZAnimation.fromValue = -1
positionZAnimation.toValue = 1
let point0 = NSValue(cgPoint: CGPoint(x: 0, y: 0))
let point1 = NSValue(cgPoint: CGPoint(x: 110, y: -20))
let positionAnimation = CAKeyframeAnimation(keyPath: "position")
positionAnimation.values = [point0, point1, point0]
positionAnimation.keyTimes = [0, 0.5, 0]
groupAnimation.animations = [rotationAnimation, positionAnimation, positionZAnimation]
meMatchImageView.layer.add(groupAnimation, forKey: "switch")
theyMatchImageView.layer.add(groupAnimation, forKey: "switch")
}
任何帮助将不胜感激!
答案 0 :(得分:0)
您的代码中至少存在两个问题。
首先,一旦动画开始,您就会在两个position
上应用相同的动画(关于rotation
,position.z
和ImageView
),他们将执行完全相同的动画,因此在整个过程中它们完全堆叠。
其次,您使用keyTimes
属性错误,看起来您的原始设计是[0, 0.5, 1]
。