02 - 关键帧动画
02-关键帧动画
-
// ViewController.h
- //
- // ViewController.h
- // 02-关键帧动画
- //
- // Created by apple on 13-12-23.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import
- @interfaceViewController :UIViewController
- @end
-
// ViewController.m
- //
- // ViewController.m
- // 02-关键帧动画
- //
- // Created by apple on 13-12-23.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import"ViewController.h"
- #import"MyView.h"
- @interfaceViewController()
- {
- MyView*_myView;
- }
- @end
- @implementationViewController
- - (void)viewDidLoad
- {
- [superviewDidLoad];
- _myView= [[MyViewalloc]initWithFrame:CGRectMake(50,50,100,100)];
- [self.viewaddSubview:_myView];
- }
- #pragma mark -触摸事件
- - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
- {
- UITouch*touch = touches.anyObject;
- CGPointlocation = [touchlocationInView:self.view];
- // [_myView moveCurveTo:location];
- [_myViewshake];
- }
- @end
-
// MyView.h
- //
- // MyView.h
- // 02-关键帧动画
- //
- // Created by apple on 13-12-23.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import
- @interfaceMyView :UIView
- #pragma mark摇晃动画
- - (void)shake;
- #pragma mark曲线运动(两个个控制点的曲线)
- - (void)moveCurveTo:(CGPoint)to;
- #pragma mark曲线运动
- - (void)moveQuadTo:(CGPoint)to;
- #pragma mark按照路径运行的动画
- - (void)moveRectPathBy:(CGPoint)by;
- #pragma mark移动到目标点
- - (void)moveTo:(CGPoint)to;
- @end
-
// MyView.m
- //
- // MyView.m
- // 02-关键帧动画
- //
- // Created by apple on 13-12-23.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import"MyView.h"
- @implementationMyView
- - (id)initWithFrame:(CGRect)frame
- {
- self= [superinitWithFrame:frame];
- if(self) {
- self.backgroundColor= [UIColorredColor];
- }
- returnself;
- }
- #pragma mark -动画代理方法
- - (void)animationDidStop:(CAAnimation*)anim finished:(BOOL)flag
- {
- //修正动画结束的位置
- // CAKeyframeAnimation *animation = (CAKeyframeAnimation *)anim;
- // NSValue *p = animation.values.lastObject;
- //
- // self.center = [p CGPointValue];
- // 1.取出动画类型
- NSString*type = [animvalueForKey:@"animationType"];
- if([typeisEqualToString:@"moveAnimation"]) {
- CGPointto = [[animvalueForKey:@"targetPosition”] CGPointValue];
- self.center= to;
- }
- }
- #pragma mark -随机点方法
- - (CGPoint)randomPoint
- {
- CGSizesize =self.superview.bounds.size;
- CGFloatx =arc4random_uniform(size.width);
- CGFloaty =arc4random_uniform(size.height);
- returnCGPointMake(x, y);
- }
- #pragma mark -按照路径平移的关键帧动画
- - (CAKeyframeAnimation*)moveWithPath:(CGPathRef)path duration:(NSTimeInterval)duration
- {
- // 1.动画
- CAKeyframeAnimation*anim = [CAKeyframeAnimationanimationWithKeyPath:@"position"];
- // 2.设置路径
- anim.path= path;
- // 3.设置时长
- anim.duration= duration;
- returnanim;
- }
- #pragma mark -动画方法
- #pragma mark摇晃动画
- - (void)shake
- {
- // 1.动画
- CAKeyframeAnimation*anim = [CAKeyframeAnimationanimationWithKeyPath:@"transform.rotation"];
- // 2.设置角度
- CGFloatangle =M_PI_4/10;
- anim.values=@[@(-angle),@(angle),@(-angle)];
- anim.duration=0.2f;
- anim.repeatCount=HUGE_VALF;
- [self.layeraddAnimation:animforKey:nil];
- }
- #pragma mark曲线运动(两个个控制点的曲线)
- //提示:不适合做过大的视图的动画,比较适合做小的颗粒效果,或者小飞虫之类的粒子效果
- - (void)moveCurveTo:(CGPoint)to
- {
- //设置路径
- CGMutablePathRefpath =CGPathCreateMutable();
- //初始点
- CGPathMoveToPoint(path,NULL,self.center.x,self.center.y);
- //设置路径
- CGPointp1 = [selfrandomPoint];
- CGPointp2 = [selfrandomPoint];
- CGPathAddCurveToPoint(path,NULL, p1.x, p1.y, p2.x, p2.y, to.x, to.y);
- CAKeyframeAnimation*anim = [selfmoveWithPath:pathduration:1.0f];
- //释放路径
- CGPathRelease(path);
- //利用KVC来记录目标点,在程序运行时,动态为anim添加了两个属性
- // 1.记录目标点
- [animsetValue:[NSValuevalueWithCGPoint:to]forKey:@"targetPosition"];
- // 2.记录动画类型
- [animsetValue:@"moveAnimation"forKey:@"animationType"];
- // 3.设置代理
- [animsetDelegate:self];
- [self.layeraddAnimation:animforKey:nil];
- }
- #pragma mark曲线运动(一个控制点的曲线)
- - (void)moveQuadTo:(CGPoint)to
- {
- //设置路径
- CGMutablePathRefpath =CGPathCreateMutable();
- //起始点
- CGPathMoveToPoint(path,NULL,self.center.x,self.center.y);
- //设置路径(曲线路径)
- CGPathAddQuadCurveToPoint(path,NULL,320,460, to.x, to.y);
- CAKeyframeAnimation*anim = [selfmoveWithPath:pathduration:1.0f];
- //释放路径
- CGPathRelease(path);
- //利用KVC来记录目标点,在程序运行时,动态为anim添加了两个属性
- // 1.记录目标点
- [animsetValue:[NSValuevalueWithCGPoint:to]forKey:@"targetPosition"];
- // 2.记录动画类型
- [animsetValue:@"moveAnimation"forKey:@"animationType"];
- // 3.设置代理
- [animsetDelegate:self];
- [self.layeraddAnimation:animforKey:nil];
- }
- #pragma mark按照路径运行的动画
- - (void)moveRectPathBy:(CGPoint)by
- {
- // 1.动画
- CAKeyframeAnimation*anim = [CAKeyframeAnimationanimationWithKeyPath:@"position"];
- // 2.路径
- CGMutablePathRefpath =CGPathCreateMutable();
- CGPointstart =self.center;
- CGRectrect =CGRectMake(start.x, start.y, by.x- start.x, by.y- start.y);
- CGPathAddRect(path,NULL, rect);
- //将路径添加到动画
- anim.path= path;
- //释放路径
- CGPathRelease(path);
- anim.duration=1.0f;
- [self.layeraddAnimation:animforKey:nil];
- }
- #pragma mark平移到目标点
- - (void)moveTo:(CGPoint)to
- {
- //在做动画时,最忌讳一成不变的动画!无论多么绚丽的动画,用户都会审美疲劳的
- //增加动画的随机性和不可预见性,非常非常重要!
- //增加一个中间点
- //中心点用个多个随机点
- // 1.动画
- CAKeyframeAnimation*anim = [CAKeyframeAnimationanimationWithKeyPath:@"position"];
- NSIntegercount =arc4random_uniform(5) +2;
- NSMutableArray*arrayM = [NSMutableArrayarrayWithCapacity:count +2];
- //起始点
- NSValue*p1 = [NSValuevalueWithCGPoint:self.center];
- [arrayMaddObject:p1];
- for(NSIntegeri =0; i < count; i++) {
- NSValue*p2 = [NSValuevalueWithCGPoint:[selfrandomPoint]];
- [arrayMaddObject:p2];
- }
- //目标点
- NSValue*p3 = [NSValuevalueWithCGPoint:to];
- [arrayMaddObject:p3];
- anim.values= arrayM;
- anim.duration=1.0f;
- anim.removedOnCompletion=NO;
- anim.fillMode=kCAFillModeForwards;
- anim.delegate=self;
- [self.layeraddAnimation:animforKey:nil];
- }
- #pragma mark平移到目标点
- - (void)moveTo1:(CGPoint)to
- {
- //增加一个中间点
- //中心点用个随机点
- // 1.动画
- CAKeyframeAnimation*anim = [CAKeyframeAnimationanimationWithKeyPath:@"position"];
- NSValue*p1 = [NSValue valueWithCGPoint:self.center];
- NSValue*p2 = [NSValue valueWithCGPoint:[selfrandomPoint]];
- NSValue*p3 = [NSValue valueWithCGPoint:to];
- anim.values=@[p1, p2, p3];
- anim.duration=1.0f;
- anim.removedOnCompletion=NO;
- anim.fillMode=kCAFillModeForwards;
- anim.delegate=self;
- [self.layeraddAnimation:animforKey:nil];
- }
- @end
作者:
出处:http://www.cnblogs.com/ChenYilong/(点击RSS订阅)
本文版权归作者和博客园共有,欢迎转载,
但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。