为2个图像视图的位置切换设置动画
问题描述:
我们试图沿着z访问切换2个imageview视图的位置切换。 2 imageViews是在屏幕上的2 到目前为止,我已经尝试使用CAAnimationGroup进行位置和旋转,但它只显示其中一个图像,并且它看起来位置和旋转关闭。这是代码的样子:为2个图像视图的位置切换设置动画
@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")
}
任何帮助将不胜感激!
答
您的代码中至少有两个问题。
首先,你在这两个ImageView
应用相同的动画(关于position
,rotation
和position.z
),尽快开始动画,他们会因此在整个过程中,他们完全执行完全相同的动画堆叠。
其次你使用keyTimes
属性错误,看起来像你的原始设计将是[0, 0.5, 1]
。