zoukankan      html  css  js  c++  java
  • 02

    02-关键帧动画


    1. // ViewController.h

    2. //
    3. // ViewController.h
    4. // 02-关键帧动画
    5. //
    6. // Created by apple on 13-12-23.
    7. // Copyright (c) 2013itcast. All rights reserved.
    8. //

    9. #import

    10. @interfaceViewController :UIViewController

    11. @end
    12. // ViewController.m

    13. //
    14. // ViewController.m
    15. // 02-关键帧动画
    16. //
    17. // Created by apple on 13-12-23.
    18. // Copyright (c) 2013itcast. All rights reserved.
    19. //

    20. #import"ViewController.h"
    21. #import"MyView.h"

    22. @interfaceViewController()
    23. {
    24.  MyView*_myView;
    25.  
    26. }

    27. @end

    28. @implementationViewController

    29. - (void)viewDidLoad
    30. {
    31. [superviewDidLoad];

    32.  _myView= [[MyViewalloc]initWithFrame:CGRectMake(50,50,100,100)];
    33. [self.viewaddSubview:_myView];
    34. }

    35. #pragma mark -触摸事件
    36. - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
    37. {
    38.  UITouch*touch = touches.anyObject;
    39.  
    40.  CGPointlocation = [touchlocationInView:self.view];
    41.  
    42. // [_myView moveCurveTo:location];
    43. [_myViewshake];
    44. }

    45. @end
    46. // MyView.h

    47. //
    48. // MyView.h
    49. // 02-关键帧动画
    50. //
    51. // Created by apple on 13-12-23.
    52. // Copyright (c) 2013itcast. All rights reserved.
    53. //

    54. #import

    55. @interfaceMyView :UIView

    56. #pragma mark摇晃动画
    57. - (void)shake;

    58. #pragma mark曲线运动(两个个控制点的曲线)
    59. - (void)moveCurveTo:(CGPoint)to;

    60. #pragma mark曲线运动
    61. - (void)moveQuadTo:(CGPoint)to;

    62. #pragma mark按照路径运行的动画
    63. - (void)moveRectPathBy:(CGPoint)by;

    64. #pragma mark移动到目标点
    65. - (void)moveTo:(CGPoint)to;

    66. @end
    67. // MyView.m

    68. //
    69. // MyView.m
    70. // 02-关键帧动画
    71. //
    72. // Created by apple on 13-12-23.
    73. // Copyright (c) 2013itcast. All rights reserved.
    74. //

    75. #import"MyView.h"

    76. @implementationMyView

    77. - (id)initWithFrame:(CGRect)frame
    78. {
    79.  self= [superinitWithFrame:frame];
    80.  
    81.  if(self) {
    82.       self.backgroundColor= [UIColorredColor];
    83. }
    84.  
    85.  returnself;
    86. }
    87. #pragma mark -动画代理方法
    88. - (void)animationDidStop:(CAAnimation*)anim finished:(BOOL)flag
    89. {
    90.  //修正动画结束的位置
    91. // CAKeyframeAnimation *animation = (CAKeyframeAnimation *)anim;
    92. // NSValue *p = animation.values.lastObject;
    93. // 
    94. // self.center = [p CGPointValue];
    95.  // 1.取出动画类型
    96.  NSString*type = [animvalueForKey:@"animationType"];
    97.  
    98.  if([typeisEqualToString:@"moveAnimation"]) {
    99.       CGPointto = [[animvalueForKey:@"targetPositionCGPointValue];
    100.       
    101.       self.center= to;
    102. }
    103. }

    104. #pragma mark -随机点方法
    105. - (CGPoint)randomPoint
    106. {
    107.  CGSizesize =self.superview.bounds.size;
    108.  
    109.  CGFloatx =arc4random_uniform(size.width);
    110.  CGFloaty =arc4random_uniform(size.height);
    111.  
    112.  returnCGPointMake(x, y);
    113. }

    114. #pragma mark -按照路径平移的关键帧动画
    115. - (CAKeyframeAnimation*)moveWithPath:(CGPathRef)path duration:(NSTimeInterval)duration
    116. {
    117.  // 1.动画
    118.  CAKeyframeAnimation*anim = [CAKeyframeAnimationanimationWithKeyPath:@"position"];
    119.  
    120.  // 2.设置路径
    121. anim.path= path;
    122.  
    123.  // 3.设置时长
    124. anim.duration= duration;
    125.  
    126.  returnanim;
    127. }

    128. #pragma mark -动画方法
    129. #pragma mark摇晃动画
    130. - (void)shake
    131. {
    132.  // 1.动画
    133.  CAKeyframeAnimation*anim = [CAKeyframeAnimationanimationWithKeyPath:@"transform.rotation"];
    134.  
    135.  // 2.设置角度
    136.  CGFloatangle =M_PI_4/10;
    137.  
    138. anim.values=@[@(-angle),@(angle),@(-angle)];
    139.  
    140. anim.duration=0.2f;
    141. anim.repeatCount=HUGE_VALF;
    142.  
    143. [self.layeraddAnimation:animforKey:nil];
    144. }

    145. #pragma mark曲线运动(两个个控制点的曲线)
    146. //提示:不适合做过大的视图的动画,比较适合做小的颗粒效果,或者小飞虫之类的粒子效果
    147. - (void)moveCurveTo:(CGPoint)to
    148. {
    149.  //设置路径
    150.  CGMutablePathRefpath =CGPathCreateMutable();
    151.  
    152.  //初始点
    153. CGPathMoveToPoint(path,NULL,self.center.x,self.center.y);
    154.  
    155.  //设置路径
    156.  CGPointp1 = [selfrandomPoint];
    157.  CGPointp2 = [selfrandomPoint];
    158.  
    159.  CGPathAddCurveToPoint(path,NULL, p1.x, p1.y, p2.x, p2.y, to.x, to.y);
    160.  
    161.  CAKeyframeAnimation*anim = [selfmoveWithPath:pathduration:1.0f];
    162.  
    163.  //释放路径
    164.  CGPathRelease(path);
    165.  
    166.  //利用KVC来记录目标点,在程序运行时,动态为anim添加了两个属性
    167.  // 1.记录目标点
    168. [animsetValue:[NSValuevalueWithCGPoint:to]forKey:@"targetPosition"];
    169.  // 2.记录动画类型
    170. [animsetValue:@"moveAnimation"forKey:@"animationType"];
    171.  
    172.  // 3.设置代理
    173. [animsetDelegate:self];
    174.  
    175. [self.layeraddAnimation:animforKey:nil];
    176. }

    177. #pragma mark曲线运动(一个控制点的曲线)
    178. - (void)moveQuadTo:(CGPoint)to
    179. {
    180.  //设置路径
    181.  CGMutablePathRefpath =CGPathCreateMutable();
    182.  
    183.  //起始点
    184. CGPathMoveToPoint(path,NULL,self.center.x,self.center.y);
    185.  
    186.  //设置路径(曲线路径)
    187.  CGPathAddQuadCurveToPoint(path,NULL,320,460, to.x, to.y);
    188.  
    189.  CAKeyframeAnimation*anim = [selfmoveWithPath:pathduration:1.0f];

    190.  //释放路径
    191.  CGPathRelease(path);
    192.  
    193.  //利用KVC来记录目标点,在程序运行时,动态为anim添加了两个属性
    194.  // 1.记录目标点
    195. [animsetValue:[NSValuevalueWithCGPoint:to]forKey:@"targetPosition"];
    196.  // 2.记录动画类型
    197. [animsetValue:@"moveAnimation"forKey:@"animationType"];
    198.  
    199.  // 3.设置代理
    200. [animsetDelegate:self];
    201.  
    202. [self.layeraddAnimation:animforKey:nil];
    203. }

    204. #pragma mark按照路径运行的动画
    205. - (void)moveRectPathBy:(CGPoint)by
    206. {
    207.  // 1.动画
    208.  CAKeyframeAnimation*anim = [CAKeyframeAnimationanimationWithKeyPath:@"position"];
    209.  
    210.  // 2.路径
    211.  CGMutablePathRefpath =CGPathCreateMutable();
    212.  
    213.  CGPointstart =self.center;
    214.  CGRectrect =CGRectMake(start.x, start.y, by.x- start.x, by.y- start.y);
    215.  
    216.  CGPathAddRect(path,NULL, rect);
    217.  
    218.  //将路径添加到动画
    219. anim.path= path;
    220.  //释放路径
    221.  CGPathRelease(path);
    222.  
    223. anim.duration=1.0f;
    224.  
    225. [self.layeraddAnimation:animforKey:nil];
    226. }

    227. #pragma mark平移到目标点
    228. - (void)moveTo:(CGPoint)to
    229. {
    230.  //在做动画时,最忌讳一成不变的动画!无论多么绚丽的动画,用户都会审美疲劳的
    231.  //增加动画的随机性和不可预见性,非常非常重要!
    232.  //增加一个中间点
    233.  //中心点用个多个随机点
    234.  // 1.动画
    235.  CAKeyframeAnimation*anim = [CAKeyframeAnimationanimationWithKeyPath:@"position"];
    236.  
    237.  NSIntegercount =arc4random_uniform(5) +2;
    238.  NSMutableArray*arrayM = [NSMutableArrayarrayWithCapacity:count +2];
    239.  
    240.  //起始点
    241.  NSValue*p1 = [NSValuevalueWithCGPoint:self.center];
    242. [arrayMaddObject:p1];
    243.  
    244.  for(NSIntegeri =0; i < count; i++) {
    245.       NSValue*p2 = [NSValuevalueWithCGPoint:[selfrandomPoint]];
    246.       
    247.       [arrayMaddObject:p2];
    248. }
    249.  
    250.  //目标点
    251.  NSValue*p3 = [NSValuevalueWithCGPoint:to];
    252. [arrayMaddObject:p3];
    253.  
    254. anim.values= arrayM;
    255.  
    256. anim.duration=1.0f;
    257.  
    258. anim.removedOnCompletion=NO;
    259. anim.fillMode=kCAFillModeForwards;
    260.  
    261. anim.delegate=self;
    262.  
    263. [self.layeraddAnimation:animforKey:nil];
    264. }

    265. #pragma mark平移到目标点
    266. - (void)moveTo1:(CGPoint)to
    267. {
    268.  //增加一个中间点
    269.  //中心点用个随机点
    270.  // 1.动画
    271.  CAKeyframeAnimation*anim = [CAKeyframeAnimationanimationWithKeyPath:@"position"];
    272.  
    273.  NSValue*p1 = [NSValue valueWithCGPoint:self.center];
    274.  NSValue*p2 = [NSValue valueWithCGPoint:[selfrandomPoint]];
    275.  NSValue*p3 = [NSValue valueWithCGPoint:to];
    276.  
    277. anim.values=@[p1, p2, p3];
    278.  
    279. anim.duration=1.0f;
    280.  
    281. anim.removedOnCompletion=NO;
    282. anim.fillMode=kCAFillModeForwards;
    283.  
    284. anim.delegate=self;
    285.  
    286. [self.layeraddAnimation:animforKey:nil];
    287. }

    288. @end
    © chenyilong. Powered by Postach.io


    作者:
    出处:http://www.cnblogs.com/ChenYilong/(点击RSS订阅)
    本文版权归作者和博客园共有,欢迎转载,
    但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    Smobiler实现手机弹窗
    在 ASP.NET Web API 中使用 Attribute 统一处理异常
    VMWare的网络模式说明
    Windows Server2008 IIS通过Nginx实现绑定多个https域名
    Error while Launching activity
    Android 动态渐变按钮
    mysql安装和配置详解以及Navicat连接失败问题
    selenium的基础学习
    使用Pandas.read_csv时出现OSError: Initializing from file failed
    sklearn 机器学习 Pipeline 模板
  • 原文地址:https://www.cnblogs.com/ChenYilong/p/3556356.html
Copyright © 2011-2022 走看看