2017-06-12 171 views
0

我們試圖沿着z訪問切換2個imageview視圖的位置切換。 2 imageViews是在屏幕上的2 Image 到目前爲止,我已經嘗試使用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") 
    } 

任何幫助將不勝感激!

回答

0

您的代碼中至少有兩個問題。

首先,你在這兩個ImageView應用相同的動畫(關於positionrotationposition.z),儘快開始動畫,他們會因此在整個過程中,他們完全執行完全相同的動畫堆疊。

其次你使用keyTimes屬性錯誤,看起來像你的原始設計將是[0, 0.5, 1]