CATransform3D的使用以及各个参数的含义

1. 缩放

CABasicAnimation *theAnimation=[CABasicAnimation animationWithKeyPath:@"transform"];
    //x,y,z放大缩小倍数
    CATransform3D transform=CATransform3DMakeScale(0.5, 0.5, 1.0);
    NSValue *value=[NSValue valueWithCATransform3D:transform];
    [theAnimation setToValue:value];
    
    
    transform=CATransform3DMakeScale(1.0, 1.0, 1.0);
    value=[NSValue valueWithCATransform3D:transform];
    
    [theAnimation setAutoreverses:YES];  //原路返回的动画一遍
    [theAnimation setDuration:1.0];//执行动画的时间
    [theAnimation setRepeatCount:2];//执行动画的次数
    
    [layer addAnimation:theAnimation forKey:nil];

2.动画旋转

CABasicAnimation *theAnimation=[CABasicAnimation animationWithKeyPath:@"transform"];
    CATransform3D transform=CATransform3DMakeRotation(90*M_PI/180, 1, 1, 0);//
    NSValue *value = [NSValue valueWithCATransform3D:transform];
    [theAnimation setToValue:value];

   /*
     angle:旋转的弧度,所以要把角度转换成弧度:角度 * M_PI / 180。
     
     x:向X轴方向旋转。值范围-1 --- 1之间
     
     y:向Y轴方向旋转。值范围-1 --- 1之间
     
     z:向Z轴方向旋转。值范围-1 --- 1之间
     向 X轴,Y轴都旋转60度,就是沿着对角线旋转。
     */
    transform = CATransform3DMakeRotation(1, 1, 1, 0);
    value = [NSValue valueWithCATransform3D:transform];
    [theAnimation setFromValue:value];
    theAnimation.duration=2;
    theAnimation.autoreverses=YES;
    [layer addAnimation:theAnimation forKey:nil];

3.组动画

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];

    CATransform3D rotateTransform = CATransform3DMakeRotation(1.57, 0, 0, -1);
    CATransform3D scaleTransform = CATransform3DMakeScale(2, 2, 2);
    //(CGFloat tx,CGFloat ty, CGFloat tz ,x,y,z轴的偏移量
    CATransform3D positionTransform = CATransform3DMakeTranslation(1, 200, 0); //位置移动
    CATransform3D combinedTransform =CATransform3DConcat(rotateTransform, scaleTransform); //Concat就是combine的意思
    combinedTransform = CATransform3DConcat(combinedTransform, positionTransform); //再combine一次把三个动作连起来
    
    [anim setFromValue:[NSValue valueWithCATransform3D:CATransform3DIdentity]]; //放在3D坐标系中最正的位置
    [anim setToValue:[NSValue valueWithCATransform3D:combinedTransform]];
    [anim setDuration:5.0f];
    
    [layer addAnimation:anim forKey:nil];
    
    [layer setTransform:combinedTransform];  //如果没有这句,layer执行完动画又会返回最初的state

 

转载于:https://www.cnblogs.com/hualuoshuijia/p/5001676.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为下列代码实现可暂停效果: import UIKit class ViewController: UIViewController { private let radarAnimation = "radarAnimation" private var animationLayer: CALayer? private var animationGroup: CAAnimationGroup? private var opBtn: UIButton! override func viewDidLoad() { super.viewDidLoad() let first = makeRadarAnimation(showRect: CGRect(x: 120, y: 100, width: 100, height: 100), isRound: true) view.layer.addSublayer(first) opBtn = UIButton(frame: CGRect(x: 100, y: 450, width: 80, height: 80)) opBtn.backgroundColor = UIColor.red opBtn.clipsToBounds = true opBtn.setTitle("Hsu", for: .normal) opBtn.layer.cornerRadius = 10 view.addSubview(opBtn) let second = makeRadarAnimation(showRect: opBtn.frame, isRound: false) view.layer.insertSublayer(second, below: opBtn.layer) } @IBAction func startAction(_ sender: UIButton) { animationLayer?.add(animationGroup!, forKey: radarAnimation) } @IBAction func stopAction(_ sender: UIButton) { animationLayer?.removeAnimation(forKey: radarAnimation) } private func makeRadarAnimation(showRect: CGRect, isRound: Bool) -> CALayer { // 1. 一个动态波 let shapeLayer = CAShapeLayer() shapeLayer.frame = showRect // showRect 最大内切圆 if isRound { shapeLayer.path = UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: showRect.width, height: showRect.height)).cgPath } else { // 矩形 shapeLayer.path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: showRect.width, height: showRect.height), cornerRadius: 10).cgPath } shapeLayer.fillColor = UIColor.orange.cgColor // 默认初始颜色透明度 shapeLayer.opacity = 0.0 animationLayer = shapeLayer // 2. 需要重复的动态波,即创建副本 let replicator = CAReplicatorLayer() replicator.frame = shapeLayer.bounds replicator.instanceCount = 4 replicator.instanceDelay = 1.0 replicator.addSublayer(shapeLayer) // 3. 创建动画组 let opacityAnimation = CABasicAnimation(keyPath: "opacity") opacityAnimation.fromValue = NSNumber(floatLiteral: 1.0) // 开始透明度 opacityAnimation.toValue = NSNumber(floatLiteral: 0) // 结束时透明底 let scaleAnimation = CABasicAnimation(keyPath: "transform") if isRound { scaleAnimation.fromValue = NSValue.init(caTransform3D: CATransform3DScale(CATransform3DIdentity, 0, 0, 0)) // 缩放起始大小 } else { scaleAnimation.fromValue = NSValue.init(caTransform3D: CATransform3DScale(CATransform3DIdentity, 1.0, 1.0, 0)) // 缩放起始大小 } scaleAnimation.toValue = NSValue.init(caTransform3D: CATransform3DScale(CATransform3DIdentity, 1.5, 1.5, 0)) // 缩放结束大小 let animationGroup = CAAnimationGroup() animationGroup.animations = [opacityAnimation, scaleAnimation] animationGroup.duration = 3.0 // 动画执行时间 animationGroup.repeatCount = HUGE // 最大重复 animationGroup.autoreverses = false self.animationGroup = animationGroup shapeLayer.add(animationGroup, forKey: radarAnimation) return replicator } }
最新发布
06-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值