设置2个imageViews

时间:2017-06-12 23:25:21

标签: ios swift caanimation cakeyframeanimation

我们正试图沿着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")
  }

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

您的代码中至少存在两个问题。

首先,一旦动画开始,您就会在两个position上应用相同的动画(关于rotationposition.zImageView),他们将执行完全相同的动画,因此在整个过程中它们完全堆叠。

其次,您使用keyTimes属性错误,看起来您的原始设计是[0, 0.5, 1]