自定义动画--角度抖动、路径旋转、左右晃动

本文介绍了如何在iOS中实现自定义动画,包括角度抖动、路径旋转和左右晃动的效果。通过创建`CAKeyframeAnimation`和`CATransform3D`,设置关键帧和动画属性,实现了视图的动态变换,为UI增加了丰富的交互体验。

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

 

 self.label1 = [[UILabelalloc]initWithFrame:CGRectMake(50,50,50,50)];

    self.label1.backgroundColor = [UIColorredColor];

    self.label1.text =@"旋转路径";

    [self.viewaddSubview:self.label1];


//图标抖动动画

CAKeyframeAnimation * keyAnimaion = [CAKeyframeAnimationanimation];

    keyAnimaion.keyPath =@"transform.rotation";

    keyAnimaion.values =@[@(-10 /180.0 * M_PI),@(10 /180.0 *M_PI),@(-10/180.0 * M_PI),@(0)];//度数转弧度

    

    keyAnimaion.removedOnCompletion =NO;

    keyAnimaion.fillMode =kCAFillModeForwards;

    keyAnimaion.duration =0.2;

    keyAnimaion.repeatCount =MAXFLOAT;

    [self.label1.layeraddAnimation:keyAnimaion forKey:nil];


//******************************//


//路径加旋转动画组合


//添加路径

    CGMutablePathRef path = CGPathCreateMutable();

    CGPathMoveToPoint(path, nil, 0, 0);

    CGPathAddCurveToPoint(path, nil, 0, 0, 200, 100, 0, 400);

    

    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];

    animation.path = path;

    animation.removedOnCompletion = NO;

    animation.fillMode = kCAFillModeForwards ;

    animation.timingFunction = [CAMediaTimingFunction functionWithName : kCAMediaTimingFunctionEaseIn];

    animation.autoreverses = NO ;

    animation.duration = 1;

    animation.repeatCount = MAXFLOAT;

    

    //添加旋转

    CATransform3D rotationTransform = CATransform3DMakeRotation(M_PI, 50 , 100 , 0);

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

    

    animation1.toValue = [ NSValue valueWithCATransform3D :rotationTransform];

    animation1.duration =  0.1;

    animation1.autoreverses = YES ;

    animation1.cumulative = NO ;

    animation1.fillMode = kCAFillModeForwards ;

    animation1.repeatCount = MAX_INPUT;

    

    [self.label1.layer addAnimation:animation forKey:@"旋转路径"];

    [self.label1.layer addAnimation:animation1 forKey:nil];


/***********************************/

//左右晃动动画

  CAKeyframeAnimation * animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.x"];

    CGFloat currentTx = self.label1.transform.ty;

    NSLog(@"%f",currentTx);

//  animation.delegate = self;

    animation.duration = 0.5;

    animation.values = @[ @(currentTx), @(currentTx + 10), @(currentTx-8), @(currentTx + 8), @(currentTx -5), @(currentTx + 5), @(currentTx) ];

    animation.repeatCount = 5;

    //时间段

    //animation.keyTimes = @[ @(0), @(0.225), @(0.425), @(0.6), @(0.75), @(0.875), @(1) ];

    //动画出现先快后慢

   //animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    [self.label1.layer addAnimation:animation forKey:nil];



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值