CAAnimation KeyPath学习总结

本文详细介绍了如何使用 CAAnimation 对 UIView 进行动画设置,包括位置、尺寸、旋转、圆角、背景颜色、透明度等属性的变化,并通过示例代码展示了如何组合不同类型的动画效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

KeyPath描述
transform.scale比例变化0 ~ 1
transform.scale.x宽比例变化0 ~ 1
transform.scale.y宽比例变化0 ~ 1
transform.rotation.x围绕X轴旋转0 ~ 2*M_PI
transform.rotation.y围绕Y轴旋转0 ~ 2*M_PI
transform.rotation.z围绕Z轴旋转0 ~ 2*M_PI
cornerRadius圆角变化0 ~ 2*MAX(width,height)
backgroundColor颜色变化,透明度不变AnyColor.cgColor^1
opacity透明度变化0 ~ 1
bounds大小变化,中心不变CGRect
position中心变化CGPoint
position.x中心X变化CGFloat
position.y中心Y变化CGFloat
contents内容变化 如ImageView.imageimage.cgImage^1
borderWidth边框宽0 ~

示例代码如下


        let testView: UIView = UIView(frame: CGRect(x: (Screen_Width()-self.View_Width())/2, y: 100, width: self.View_Width(), height: self.View_Width()))
        testView.backgroundColor = UIColor.green
        self.view.addSubview(testView)
        
        let iv: UIImageView = UIImageView(frame: CGRect(x: 20, y: 50, width: 150, height: 200))
        iv.image = UIImage(named: "1.jpeg")
        self.view.addSubview(iv)
        
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
        
            print("begin")
            let bounds: CABasicAnimation = CABasicAnimation(keyPath: "bounds")
            bounds.fromValue = testView.frame
            bounds.toValue = CGRect(x: (Screen_Width()-self.View_Width())/2, y: Screen_Height() - 300, width: 50, height: 50)
            
            let position: CABasicAnimation = CABasicAnimation(keyPath: "position.y")
            position.fromValue = testView.center.y
            position.toValue = Screen_Height() - 150
            
            let rotation: CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.x")
            rotation.fromValue = 0
            rotation.toValue = M_PI * 2
            
            let cornerRadius: CABasicAnimation = CABasicAnimation(keyPath: "cornerRadius")
            cornerRadius.fromValue = 0
            cornerRadius.toValue = 25
            
            let bgColor: CABasicAnimation = CABasicAnimation(keyPath: "backgroundColor")
            bgColor.fromValue = UIColor.green.cgColor
            bgColor.toValue = UIColor(red: 0.3, green: 0.2, blue: 0.9, alpha: 0.1)
            
            let opacity: CABasicAnimation = CABasicAnimation(keyPath: "opacity")
            opacity.fromValue = 1.0
            opacity.toValue = 0.5
            
            let borderWidth: CABasicAnimation = CABasicAnimation(keyPath: "borderWidth")
            borderWidth.fromValue = 0
            borderWidth.toValue = 2
            
            let group1: CAAnimationGroup = CAAnimationGroup()
            group1.animations = [bounds, position, cornerRadius, bgColor, opacity, borderWidth]
            group1.fillMode = kCAFillModeForwards
            group1.isRemovedOnCompletion = false
            group1.duration = 2
            testView.layer.add(group1, forKey: "test")
            
            
            
            let contents: CABasicAnimation = CABasicAnimation(keyPath: "contents")
            contents.fromValue = iv.image?.cgImage
            contents.toValue = UIImage(named: "2.jpeg")?.cgImage
            
            let group2: CAAnimationGroup = CAAnimationGroup()
            group2.animations = [contents, borderWidth]
            group2.fillMode = kCAFillModeForwards
            group2.isRemovedOnCompletion = false
            group2.duration = 2
            iv.layer.add(group2, forKey: "iv")
            
        }

复制代码

CAAnimation中 当isRemovedOnCompletion = false 时 fillMode = kCAFillModeForwards 才会生效

官方参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值