objective-c 动画 花瓣飘落

//动画

//  ViewController.mspa

//  花瓣飘落.net

//3d



#import "ViewController.h"代理


@interface ViewController ()orm

{事务

    CALayer *_layer;//建立一个图层get

}animation


@end it


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    //设置背景

    UIImage *backgroundImage = [UIImage imageNamed:@"background"];

    self.view.backgroundColor = [UIColor colorWithPatternImage:backgroundImage];

    

    //自定义一个图层(花瓣)

    _layer = [[CALayer alloc]init];

    _layer.bounds = CGRectMake(0, 0, 16, 30);

    _layer.position = CGPointMake(50, 200);

    _layer.contents = (id)[UIImage imageNamed:@"petal"].CGImage;

    [self.view.layer addSublayer:_layer];

    //执行动画

    [self keyFrameAnimation];

}



#pragma mark 关键帧动画

- (void)keyFrameAnimation

{

    //1.建立关键帧动画

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

    

    //2.设置关键帧

    //绘制贝塞尔曲线

    CGMutablePathRef path = CGPathCreateMutable();

    CGPathMoveToPoint(path, NULL, _layer.position.x, _layer.position.y);//路径移动到起点

    CGPathAddCurveToPoint(path, NULL, 160, 280, -30, 300, 50, 400);//绘制2次贝塞尔曲线

    keyframeAni.path = path;//设置动画沿路径进行

    

    //设置一些其它的属性

    keyframeAni.duration = 10.0;

    keyframeAni.beginTime = CACurrentMediaTime() + 2;//设置延迟2秒执行

    keyframeAni.autoreverses = YES;

    keyframeAni.repeatCount = HUGE_VALF;

    //添加动画到图层

    [_layer addAnimation:keyframeAni forKey:@"keyFrameAnimation"];

}



//-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

//{

//    //获取点击位置

//    UITouch *touch = [touches anyObject];

//    CGPoint location = [touch locationInView:self.view];

//    

//    //1.建立动画并指定动画属性

//    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"position"];

//    //2.设置动画属性初始值和结束值

//    //初始值能够不设置,默认图层初始位置

//    basicAnimation.toValue = [NSValue valueWithCGPoint:location];

//    //3.设置其余动画属性

//    basicAnimation.duration = 5.0;//动画时间

//    basicAnimation.repeatCount = 1;//循环次数

//    basicAnimation.delegate = self;

//    //记录鼠标点击位置,在动画结束后使用

//    [basicAnimation setValue:[NSValue valueWithCGPoint:location] forKey:@"animationLocation"];

//    

//    //4.添加动画。注意key至关于给动画命名,之后可使用此名称获取该动画

//    [_layer addAnimation:basicAnimation forKey:@"basicAnimation"];

//}

//

//#pragma mark 动画代理方法

//#pragma mark 动画结束

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

//{

//    //开启动画事务

//    [CATransaction begin];

//    //禁用隐式动画

//    [CATransaction setDisableActions:YES];

//    _layer.position = [[anim valueForKey:@"animationLocation"] CGPointValue];

//    //递交动画事务

//    [CATransaction commit];

//}




@end

相关文章
相关标签/搜索