GAAnimationGroup动画组

            动画组,也是CAAnimation的子类,可以保存一组动画对象,将CAAnimationGroup对象加入图层后,组中所有动画对象可以同时并发运行。


相关属性:

  1.animations:保存一组动画对象的NSArray;

  2.默认情况,一组动画对象可以同时运行,可以通过设置动画对象的beginTime属性来更改动画开始的时间。

#import "ViewController.h"

@interface ViewController () {

    UIImageView *_imgView;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 50, 50)];
    _imgView.image = [UIImage imageNamed:@"024.png"];
    [self.view addSubview:_imgView];
}
//开始触摸屏幕调用的方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {


//获得触摸点
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self.view];
    
    //创建动画组对象
    CAAnimationGroup *group = [CAAnimationGroup animation];
    
    //设置属性。 调用圆周运动和抖动的方法
    CAAnimation *animation1 = [self movieWithArc:p];
    CAAnimation *animation2 = [self getAnimation];
    //把动画交给animations属性
    group.animations = @[animation1,animation2];
    //设置无限次数
    group.repeatCount = MAXFLOAT;
    //设置时长
    group.duration = 3;
    //设代理
    group.delegate = self;
    //添加到图层
    [_imgView.layer addAnimation:group forKey:nil];
    
}
//圆周运动
- (CAKeyframeAnimation *)movieWithArc:(CGPoint)touchPoint {
    
    //以手指点击的地方作为圆心,150作为半径的圆运动
    
    //创建动画对象
    CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    //设置属性
    keyFrameAnimation.duration = 2;
    
    keyFrameAnimation.repeatCount = MAXFLOAT;
    
    //设置运动的路径
    CGMutablePathRef path = CGPathCreateMutable();
    
    CGPathAddArc(path, NULL, touchPoint.x, touchPoint.y, 150, 0, M_PI*2, 1);
    
    keyFrameAnimation.path = path;
    //释放路径(有<span style="font-family: Arial, Helvetica, sans-serif;">Create,必须有release</span>)
    CGPathRelease(path);
    
    return keyFrameAnimation;
}
// 抖动
- (CAKeyframeAnimation *)getAnimation {

    //创建动画化对象
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
    
    //设置属性,抖动的角度
    CGFloat num1 = M_PI_4/5.0;
    CGFloat num2 = -M_PI_4/5.0;
    
    animation.values = @[@(num1),@(num2),@(num1)];
    
    animation.duration = 0.5;
    animation.repeatCount = MAXFLOAT;
    
    //加速方式
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    
    return animation;
    
}

动画开始和结束时调用的方法

#pragma mark - CAAnimation Delegate
- (void)animationDidStart:(CAAnimation *)anim {

    NSLog(@"animationDidStart");
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {

    NSLog(@"animationDidStop");
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值