Swift中同一UIView的多个动画

时间:2018-10-22 17:21:14

标签: ios swift animation swift4 cabasicanimation

我正在尝试为我的图像视图添加多个动画,但是只有其中一个动画。请检查以下代码。我为图像视图创建了缩放动画和旋转动画,但是只有在运行以下代码时才能看到缩放动画。

//Rotate animation
let rotation: CABasicAnimation = CABasicAnimation(keyPath: 
"transform.rotation.y")
rotation.toValue = 0
rotation.fromValue = 2.61

//Scale animation
let scale: CABasicAnimation = CABasicAnimation(keyPath: "transform.scale")
scale.toValue = 1
scale.fromValue = 0

//Adding animations to group
let group = CAAnimationGroup()
group.animations = [rotation,scale]
group.duration = 0.2

myImage.layer.add(group, forKey: nil) 

2 个答案:

答案 0 :(得分:1)

发生旋转,但持续时间不太明显

ProfileActivity

当更改为5秒时,请参见

enter image description here

答案 1 :(得分:0)

在完成时,您可以放置​​动画,完成一次后,等待第二秒..等

   let myImage = UIImageView()
    UIView.animate(withDuration: 1.0, animations: {
        let rotation: CABasicAnimation = CABasicAnimation(keyPath: 
        "transform.rotation.y")
        rotation.toValue = 0
        rotation.fromValue = 2.61
    }, completion: { (value: Bool) in
        UIView.animate(withDuration: 1.0, animations: {
            //you can here put your other animation
        })
    })