GAAnimationGroup动画组

来源:互联网 发布:热模拟软件 编辑:程序博客网 时间:2024/05/22 16:36

            动画组,也是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