1. CoreAnimationEffect.h
  2. //  CoreAnimationEffect
  3. //
  4. //  Created by VincentXue on 13-1-19.
  5. //  Copyright (c) 2013年 VincentXue. All rights reserved.
  6. //
  7. #import <Foundation/Foundation.h>
  8. /**
  9. !  导入QuartzCore.framework
  10. *
  11. *  Example:
  12. *
  13. *  Step.1
  14. *
  15. *      #import "CoreAnimationEffect.h"
  16. *
  17. *  Step.2
  18. *
  19. *      [CoreAnimationEffect animationMoveLeft:your view];
  20. *
  21. */
  22. @interface CoreAnimationEffect : NSObject
  23. #pragma mark - Custom Animation
  24. /**
  25. *   @brief 快速构建一个你自定义的动画,有以下参数供你设置.
  26. *
  27. *   @note  调用系统预置Type需要在调用类引入下句
  28. *
  29. *          #import <QuartzCore/QuartzCore.h>
  30. *
  31. *   @param type                动画过渡类型
  32. *   @param subType             动画过渡方向(子类型)
  33. *   @param duration            动画持续时间
  34. *   @param timingFunction      动画定时函数属性
  35. *   @param theView             需要添加动画的view.
  36. *
  37. *
  38. */
  39. + (void)showAnimationType:(NSString *)type
  40. withSubType:(NSString *)subType
  41. duration:(CFTimeInterval)duration
  42. timingFunction:(NSString *)timingFunction
  43. view:(UIView *)theView;
  44. #pragma mark - Preset Animation
  45. /**
  46. *  下面是一些常用的动画效果
  47. */
  48. // reveal
  49. + (void)animationRevealFromBottom:(UIView *)view;
  50. + (void)animationRevealFromTop:(UIView *)view;
  51. + (void)animationRevealFromLeft:(UIView *)view;
  52. + (void)animationRevealFromRight:(UIView *)view;
  53. // 渐隐渐消
  54. + (void)animationEaseIn:(UIView *)view;
  55. + (void)animationEaseOut:(UIView *)view;
  56. // 翻转
  57. + (void)animationFlipFromLeft:(UIView *)view;
  58. + (void)animationFlipFromRigh:(UIView *)view;
  59. // 翻页
  60. + (void)animationCurlUp:(UIView *)view;
  61. + (void)animationCurlDown:(UIView *)view;
  62. // push
  63. + (void)animationPushUp:(UIView *)view;
  64. + (void)animationPushDown:(UIView *)view;
  65. + (void)animationPushLeft:(UIView *)view;
  66. + (void)animationPushRight:(UIView *)view;
  67. // move
  68. + (void)animationMoveUp:(UIView *)view duration:(CFTimeInterval)duration;
  69. + (void)animationMoveDown:(UIView *)view duration:(CFTimeInterval)duration;
  70. + (void)animationMoveLeft:(UIView *)view;
  71. + (void)animationMoveRight:(UIView *)view;
  72. // 旋转缩放
  73. // 各种旋转缩放效果
  74. + (void)animationRotateAndScaleEffects:(UIView *)view;
  75. // 旋转同时缩小放大效果
  76. + (void)animationRotateAndScaleDownUp:(UIView *)view;
  77. #pragma mark - Private API
  78. /**
  79. *  下面动画里用到的某些属性在当前API里是不合法的,但是也可以用.
  80. */
  81. + (void)animationFlipFromTop:(UIView *)view;
  82. + (void)animationFlipFromBottom:(UIView *)view;
  83. + (void)animationCubeFromLeft:(UIView *)view;
  84. + (void)animationCubeFromRight:(UIView *)view;
  85. + (void)animationCubeFromTop:(UIView *)view;
  86. + (void)animationCubeFromBottom:(UIView *)view;
  87. + (void)animationSuckEffect:(UIView *)view;
  88. + (void)animationRippleEffect:(UIView *)view;
  89. + (void)animationCameraOpen:(UIView *)view;
  90. + (void)animationCameraClose:(UIView *)view;
  91. @end
  92. //
  93. //  CoreAnimationEffect.m
  94. //  CoreAnimationEffect
  95. //
  96. //  Created by VincentXue on 13-1-19.
  97. //  Copyright (c) 2013年 VincentXue. All rights reserved.
  98. //
  99. #import "CoreAnimationEffect.h"
  100. #import <QuartzCore/QuartzCore.h>
  101. @implementation CoreAnimationEffect
  102. /**
  103. *  首先推荐一个不错的网站.   http://www.raywenderlich.com
  104. */
  105. #pragma mark - Custom Animation
  106. + (void)showAnimationType:(NSString *)type
  107. withSubType:(NSString *)subType
  108. duration:(CFTimeInterval)duration
  109. timingFunction:(NSString *)timingFunction
  110. view:(UIView *)theView
  111. {
  112. /** CATransition
  113. *
  114. *  @see http://www.dreamingwish.com/dream-2012/the-concept-of-coreanimation-programming-guide.html
  115. *  @see http://geeklu.com/2012/09/animation-in-ios/
  116. *
  117. *  CATransition 常用设置及属性注解如下:
  118. */
  119. CATransition *animation = [CATransition animation];
  120. /** delegate
  121. *
  122. *  动画的代理,如果你想在动画开始和结束的时候做一些事,可以设置此属性,它会自动回调两个代理方法.
  123. *
  124. *  @see CAAnimationDelegate    (按下command键点击)
  125. */
  126. animation.delegate = self;
  127. /** duration
  128. *
  129. *  动画持续时间
  130. */
  131. animation.duration = duration;
  132. /** timingFunction
  133. *
  134. *  用于变化起点和终点之间的插值计算,形象点说它决定了动画运行的节奏,比如是均匀变化(相同时间变化量相同)还是
  135. *  先快后慢,先慢后快还是先慢再快再慢.
  136. *
  137. *  动画的开始与结束的快慢,有五个预置分别为(下同):
  138. *  kCAMediaTimingFunctionLinear            线性,即匀速
  139. *  kCAMediaTimingFunctionEaseIn            先慢后快
  140. *  kCAMediaTimingFunctionEaseOut           先快后慢
  141. *  kCAMediaTimingFunctionEaseInEaseOut     先慢后快再慢
  142. *  kCAMediaTimingFunctionDefault           实际效果是动画中间比较快.
  143. */
  144. /** timingFunction
  145. *
  146. *  当上面的预置不能满足你的需求的时候,你可以使用下面的两个方法来自定义你的timingFunction
  147. *  具体参见下面的URL
  148. *
  149. *  @see http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/CAMediaTimingFunction_class/Introduction/Introduction.html
  150. *
  151. *  + (id)functionWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y;
  152. *
  153. *  - (id)initWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y;
  154. */
  155. animation.timingFunction = [CAMediaTimingFunction functionWithName:timingFunction];
  156. /** fillMode
  157. *
  158. *  决定当前对象过了非active时间段的行为,比如动画开始之前,动画结束之后.
  159. *  预置为:
  160. *  kCAFillModeRemoved   默认,当动画开始前和动画结束后,动画对layer都没有影响,动画结束后,layer会恢复到之前的状态
  161. *  kCAFillModeForwards  当动画结束后,layer会一直保持着动画最后的状态
  162. *  kCAFillModeBackwards 和kCAFillModeForwards相对,具体参考上面的URL
  163. *  kCAFillModeBoth      kCAFillModeForwards和kCAFillModeBackwards在一起的效果
  164. */
  165. animation.fillMode = kCAFillModeForwards;
  166. /** removedOnCompletion
  167. *
  168. *  这个属性默认为YES.一般情况下,不需要设置这个属性.
  169. *
  170. *  但如果是CAAnimation动画,并且需要设置 fillMode 属性,那么需要将 removedOnCompletion 设置为NO,否则
  171. *  fillMode无效
  172. */
  173. //    animation.removedOnCompletion = NO;
  174. /** type
  175. *
  176. *  各种动画效果  其中除了'fade', `moveIn', `push' , `reveal' ,其他属于似有的API(我是这么认为的,可以点进去看下注释).
  177. *  ↑↑↑上面四个可以分别使用'kCATransitionFade', 'kCATransitionMoveIn', 'kCATransitionPush', 'kCATransitionReveal'来调用.
  178. *  @"cube"                     立方体翻滚效果
  179. *  @"moveIn"                   新视图移到旧视图上面
  180. *  @"reveal"                   显露效果(将旧视图移开,显示下面的新视图)
  181. *  @"fade"                     交叉淡化过渡(不支持过渡方向)             (默认为此效果)
  182. *  @"pageCurl"                 向上翻一页
  183. *  @"pageUnCurl"               向下翻一页
  184. *  @"suckEffect"               收缩效果,类似系统最小化窗口时的神奇效果(不支持过渡方向)
  185. *  @"rippleEffect"             滴水效果,(不支持过渡方向)
  186. *  @"oglFlip"                  上下左右翻转效果
  187. *  @"rotate"                   旋转效果
  188. *  @"push"
  189. *  @"cameraIrisHollowOpen"     相机镜头打开效果(不支持过渡方向)
  190. *  @"cameraIrisHollowClose"    相机镜头关上效果(不支持过渡方向)
  191. */
  192. /** type
  193. *
  194. *  kCATransitionFade            交叉淡化过渡
  195. *  kCATransitionMoveIn          新视图移到旧视图上面
  196. *  kCATransitionPush            新视图把旧视图推出去
  197. *  kCATransitionReveal          将旧视图移开,显示下面的新视图
  198. */
  199. animation.type = type;
  200. /** subtype
  201. *
  202. *  各种动画方向
  203. *
  204. *  kCATransitionFromRight;      同字面意思(下同)
  205. *  kCATransitionFromLeft;
  206. *  kCATransitionFromTop;
  207. *  kCATransitionFromBottom;
  208. */
  209. /** subtype
  210. *
  211. *  当type为@"rotate"(旋转)的时候,它也有几个对应的subtype,分别为:
  212. *  90cw    逆时针旋转90°
  213. *  90ccw   顺时针旋转90°
  214. *  180cw   逆时针旋转180°
  215. *  180ccw  顺时针旋转180°
  216. */
  217. /**
  218. *  type与subtype的对应关系(必看),如果对应错误,动画不会显现.
  219. *
  220. *  @see http://iphonedevwiki.net/index.php/CATransition
  221. */
  222. animation.subtype = subType;
  223. /**
  224. *  所有核心动画和特效都是基于CAAnimation,而CAAnimation是作用于CALayer的.所以把动画添加到layer上.
  225. *  forKey  可以是任意字符串.
  226. */
  227. [theView.layer addAnimation:animation forKey:nil];
  228. }
  229. #pragma mark - Preset Animation
  230. + (void)animationRevealFromBottom:(UIView *)view
  231. {
  232. CATransition *animation = [CATransition animation];
  233. [animation setDuration:0.35f];
  234. [animation setType:kCATransitionReveal];
  235. [animation setSubtype:kCATransitionFromBottom];
  236. [animation setFillMode:kCAFillModeForwards];
  237. [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
  238. [view.layer addAnimation:animation forKey:nil];
  239. }
  240. + (void)animationRevealFromTop:(UIView *)view
  241. {
  242. CATransition *animation = [CATransition animation];
  243. [animation setDuration:0.35f];
  244. [animation setType:kCATransitionReveal];
  245. [animation setSubtype:kCATransitionFromTop];
  246. [animation setFillMode:kCAFillModeForwards];
  247. [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  248. [view.layer addAnimation:animation forKey:nil];
  249. }
  250. + (void)animationRevealFromLeft:(UIView *)view
  251. {
  252. CATransition *animation = [CATransition animation];
  253. [animation setDuration:0.35f];
  254. [animation setType:kCATransitionReveal];
  255. [animation setSubtype:kCATransitionFromLeft];
  256. [animation setFillMode:kCAFillModeForwards];
  257. [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
  258. [view.layer addAnimation:animation forKey:nil];
  259. }
  260. + (void)animationRevealFromRight:(UIView *)view
  261. {
  262. CATransition *animation = [CATransition animation];
  263. [animation setDuration:0.35f];
  264. [animation setType:kCATransitionReveal];
  265. [animation setSubtype:kCATransitionFromRight];
  266. [animation setFillMode:kCAFillModeForwards];
  267. [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
  268. [view.layer addAnimation:animation forKey:nil];
  269. }
  270. + (void)animationEaseIn:(UIView *)view
  271. {
  272. CATransition *animation = [CATransition animation];
  273. [animation setDuration:0.35f];
  274. [animation setType:kCATransitionFade];
  275. [animation setFillMode:kCAFillModeForwards];
  276. [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
  277. [view.layer addAnimation:animation forKey:nil];
  278. }
  279. + (void)animationEaseOut:(UIView *)view
  280. {
  281. CATransition *animation = [CATransition animation];
  282. [animation setDuration:0.35f];
  283. [animation setType:kCATransitionFade];
  284. [animation setFillMode:kCAFillModeForwards];
  285. [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  286. [view.layer addAnimation:animation forKey:nil];
  287. }
  288. /**
  289. *  UIViewAnimation
  290. *
  291. *  @see    http://www.cocoachina.com/bbs/read.php?tid=110168
  292. *
  293. *  @brief  UIView动画应该是最简单便捷创建动画的方式了,详解请猛戳URL.
  294. *
  295. *  @method beginAnimations:context 第一个参数用来作为动画的标识,第二个参数给代理代理传递消息.至于为什么一个使用
  296. *                                  nil而另外一个使用NULL,是因为第一个参数是一个对象指针,而第二个参数是基本数据类型.
  297. *  @method setAnimationCurve:      设置动画的加速或减速的方式(速度)
  298. *  @method setAnimationDuration:   动画持续时间
  299. *  @method setAnimationTransition:forView:cache:   第一个参数定义动画类型,第二个参数是当前视图对象,第三个参数是是否使用缓冲区
  300. *  @method commitAnimations        动画结束
  301. */
  302. + (void)animationFlipFromLeft:(UIView *)view
  303. {
  304. [UIView beginAnimations:nil context:NULL];
  305. [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
  306. [UIView setAnimationDuration:0.35f];
  307. [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:view cache:NO];
  308. [UIView commitAnimations];
  309. }
  310. + (void)animationFlipFromRigh:(UIView *)view
  311. {
  312. [UIView beginAnimations:nil context:NULL];
  313. [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
  314. [UIView setAnimationDuration:0.35f];
  315. [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:view cache:NO];
  316. [UIView commitAnimations];
  317. }
  318. + (void)animationCurlUp:(UIView *)view
  319. {
  320. [UIView beginAnimations:nil context:NULL];
  321. [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
  322. [UIView setAnimationDuration:0.35f];
  323. [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:view cache:NO];
  324. [UIView commitAnimations];
  325. }
  326. + (void)animationCurlDown:(UIView *)view
  327. {
  328. [UIView beginAnimations:nil context:NULL];
  329. [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
  330. [UIView setAnimationDuration:0.35f];
  331. [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:view cache:NO];
  332. [UIView commitAnimations];
  333. }
  334. + (void)animationPushUp:(UIView *)view
  335. {
  336. CATransition *animation = [CATransition animation];
  337. [animation setDuration:0.35f];
  338. [animation setFillMode:kCAFillModeForwards];
  339. [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  340. [animation setType:kCATransitionPush];
  341. [animation setSubtype:kCATransitionFromTop];
  342. [view.layer addAnimation:animation forKey:nil];
  343. }
  344. + (void)animationPushDown:(UIView *)view
  345. {
  346. CATransition *animation = [CATransition animation];
  347. [animation setDuration:0.35f];
  348. [animation setFillMode:kCAFillModeForwards];
  349. [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  350. [animation setType:kCATransitionPush];
  351. [animation setSubtype:kCATransitionFromBottom];
  352. [view.layer addAnimation:animation forKey:nil];
  353. }
  354. + (void)animationPushLeft:(UIView *)view
  355. {
  356. CATransition *animation = [CATransition animation];
  357. [animation setDuration:0.35f];
  358. [animation setFillMode:kCAFillModeForwards];
  359. [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  360. [animation setType:kCATransitionPush];
  361. [animation setSubtype:kCATransitionFromLeft];
  362. [view.layer addAnimation:animation forKey:nil];
  363. }
  364. + (void)animationPushRight:(UIView *)view
  365. {
  366. CATransition *animation = [CATransition animation];
  367. [animation setDuration:0.35f];
  368. [animation setFillMode:kCAFillModeForwards];
  369. [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  370. [animation setType:kCATransitionPush];
  371. [animation setSubtype:kCATransitionFromRight];
  372. [view.layer addAnimation:animation forKey:nil];
  373. }
  374. // presentModalViewController
  375. + (void)animationMoveUp:(UIView *)view duration:(CFTimeInterval)duration
  376. {
  377. CATransition *animation = [CATransition animation];
  378. [animation setDuration:duration];
  379. [animation setFillMode:kCAFillModeForwards];
  380. [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
  381. [animation setType:kCATransitionMoveIn];
  382. [animation setSubtype:kCATransitionFromTop];
  383. [view.layer addAnimation:animation forKey:nil];
  384. }
  385. // dissModalViewController
  386. + (void)animationMoveDown:(UIView *)view duration:(CFTimeInterval)duration
  387. {
  388. CATransition *transition = [CATransition animation];
  389. transition.duration =0.4;
  390. transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  391. transition.type = kCATransitionReveal;
  392. transition.subtype = kCATransitionFromBottom;
  393. [view.layer addAnimation:transition forKey:nil];
  394. }
  395. + (void)animationMoveLeft:(UIView *)view
  396. {
  397. CATransition *animation = [CATransition animation];
  398. [animation setDuration:0.35f];
  399. [animation setFillMode:kCAFillModeForwards];
  400. [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  401. [animation setType:kCATransitionMoveIn];
  402. [animation setSubtype:kCATransitionFromLeft];
  403. [view.layer addAnimation:animation forKey:nil];
  404. }
  405. + (void)animationMoveRight:(UIView *)view
  406. {
  407. CATransition *animation = [CATransition animation];
  408. [animation setDuration:0.35f];
  409. [animation setFillMode:kCAFillModeForwards];
  410. [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  411. [animation setType:kCATransitionMoveIn];
  412. [animation setSubtype:kCATransitionFromRight];
  413. [view.layer addAnimation:animation forKey:nil];
  414. }
  415. +(void)animationRotateAndScaleEffects:(UIView *)view
  416. {
  417. [UIView animateWithDuration:0.35f animations:^
  418. {
  419. /**
  420. *  @see       http://donbe.blog.163.com/blog/static/138048021201061054243442/
  421. *
  422. *  @param     transform   形变属性(结构体),可以利用这个属性去对view做一些翻转或者缩放.详解请猛戳↑URL.
  423. *
  424. *  @method    valueWithCATransform3D: 此方法需要一个CATransform3D的结构体.一些非详细的讲解可以看下面的URL
  425. *
  426. *  @see       http://blog.csdn.net/liubo0_0/article/details/7452166
  427. *
  428. */
  429. view.transform = CGAffineTransformMakeScale(0.001, 0.001);
  430. CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
  431. // 向右旋转45°缩小到最小,然后再从小到大推出.
  432. animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.70, 0.40, 0.80)];
  433. /**
  434. *     其他效果:
  435. *     从底部向上收缩一半后弹出
  436. *     animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.0, 1.0, 0.0)];
  437. *
  438. *     从底部向上完全收缩后弹出
  439. *     animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 1.0, 0.0, 0.0)];
  440. *
  441. *     左旋转45°缩小到最小,然后再从小到大推出.
  442. *     animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.50, -0.50, 0.50)];
  443. *
  444. *     旋转180°缩小到最小,然后再从小到大推出.
  445. *     animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.1, 0.2, 0.2)];
  446. */
  447. animation.duration = 0.45;
  448. animation.repeatCount = 1;
  449. [view.layer addAnimation:animation forKey:nil];
  450. }
  451. completion:^(BOOL finished)
  452. {
  453. [UIView animateWithDuration:0.35f animations:^
  454. {
  455. view.transform = CGAffineTransformMakeScale(1.0, 1.0);
  456. }];
  457. }];
  458. }
  459. /** CABasicAnimation
  460. *
  461. *  @see https://developer.apple.com/library/mac/#documentation/cocoa/conceptual/CoreAnimation_guide/Articles/KVCAdditions.html
  462. *
  463. *  @brief                      便利构造函数 animationWithKeyPath: KeyPath需要一个字符串类型的参数,实际上是一个
  464. *                              键-值编码协议的扩展,参数必须是CALayer的某一项属性,你的代码会对应的去改变该属性的效果
  465. *                              具体可以填写什么请参考上面的URL,切勿乱填!
  466. *                              例如这里填写的是 @"transform.rotation.z" 意思就是围绕z轴旋转,旋转的单位是弧度.
  467. *                              这个动画的效果是把view旋转到最小,再旋转回来.
  468. *                              你也可以填写@"opacity" 去修改透明度...以此类推.修改layer的属性,可以用这个类.
  469. *
  470. *  @param toValue              动画结束的值.CABasicAnimation自己只有三个属性(都很重要)(其他属性是继承来的),分别为:
  471. *                              fromValue(开始值), toValue(结束值), byValue(偏移值),
  472. !                              这三个属性最多只能同时设置两个;
  473. *                              他们之间的关系如下:
  474. *                              如果同时设置了fromValue和toValue,那么动画就会从fromValue过渡到toValue;
  475. *                              如果同时设置了fromValue和byValue,那么动画就会从fromValue过渡到fromValue + byValue;
  476. *                              如果同时设置了byValue  和toValue,那么动画就会从toValue - byValue过渡到toValue;
  477. *
  478. *                              如果只设置了fromValue,那么动画就会从fromValue过渡到当前的value;
  479. *                              如果只设置了toValue  ,那么动画就会从当前的value过渡到toValue;
  480. *                              如果只设置了byValue  ,那么动画就会从从当前的value过渡到当前value + byValue.
  481. *
  482. *                              可以这么理解,当你设置了三个中的一个或多个,系统就会根据以上规则使用插值算法计算出一个时间差并
  483. *                              同时开启一个Timer.Timer的间隔也就是这个时间差,通过这个Timer去不停地刷新keyPath的值.
  484. !                              而实际上,keyPath的值(layer的属性)在动画运行这一过程中,是没有任何变化的,它只是调用了GPU去
  485. *                              完成这些显示效果而已.
  486. *                              在这个动画里,是设置了要旋转到的弧度,根据以上规则,动画将会从它当前的弧度专旋转到我设置的弧度.
  487. *
  488. *  @param duration             动画持续时间
  489. *
  490. *  @param timingFunction       动画起点和终点之间的插值计算,也就是说它决定了动画运行的节奏,是快还是慢,还是先快后慢...
  491. */
  492. /** CAAnimationGroup
  493. *
  494. *  @brief                      顾名思义,这是一个动画组,它允许多个动画组合在一起并行显示.比如这里设置了两个动画,
  495. *                              把他们加在动画组里,一起显示.例如你有几个动画,在动画执行的过程中需要同时修改动画的某些属性,
  496. *                              这时候就可以使用CAAnimationGroup.
  497. *
  498. *  @param duration             动画持续时间,值得一提的是,如果添加到group里的子动画不设置此属性,group里的duration会统一
  499. *                              设置动画(包括子动画)的duration属性;但是如果子动画设置了duration属性,那么group的duration属性
  500. *                              的值不应该小于每个子动画中duration属性的值,否则会造成子动画显示不全就停止了动画.
  501. *
  502. *  @param autoreverses         动画完成后自动重新开始,默认为NO.
  503. *
  504. *  @param repeatCount          动画重复次数,默认为0.
  505. *
  506. *  @param animations           动画组(数组类型),把需要同时运行的动画加到这个数组里.
  507. *
  508. *  @note  addAnimation:forKey  这个方法的forKey参数是一个字符串,这个字符串可以随意设置.
  509. *
  510. *  @note                       如果你需要在动画group执行结束后保存动画效果的话,设置 fillMode 属性,并且把
  511. *                              removedOnCompletion 设置为NO;
  512. */
  513. + (void)animationRotateAndScaleDownUp:(UIView *)view
  514. {
  515. CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  516. rotationAnimation.toValue = [NSNumber numberWithFloat:(2 * M_PI) * 2];
  517. rotationAnimation.duration = 0.35f;
  518. rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  519. CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  520. scaleAnimation.toValue = [NSNumber numberWithFloat:0.0];
  521. scaleAnimation.duration = 0.35f;
  522. scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  523. CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
  524. animationGroup.duration = 0.35f;
  525. animationGroup.autoreverses = YES;
  526. animationGroup.repeatCount = 1;
  527. animationGroup.animations =[NSArray arrayWithObjects:rotationAnimation, scaleAnimation, nil];
  528. [view.layer addAnimation:animationGroup forKey:@"animationGroup"];
  529. }
  530. #pragma mark - Private API
  531. + (void)animationFlipFromTop:(UIView *)view
  532. {
  533. CATransition *animation = [CATransition animation];
  534. [animation setDuration:0.35f];
  535. [animation setFillMode:kCAFillModeForwards];
  536. [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  537. [animation setType:@"oglFlip"];
  538. [animation setSubtype:@"fromTop"];
  539. [view.layer addAnimation:animation forKey:nil];
  540. }
  541. + (void)animationFlipFromBottom:(UIView *)view
  542. {
  543. CATransition *animation = [CATransition animation];
  544. [animation setDuration:0.35f];
  545. [animation setFillMode:kCAFillModeForwards];
  546. [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  547. [animation setType:@"oglFlip"];
  548. [animation setSubtype:@"fromBottom"];
  549. [view.layer addAnimation:animation forKey:nil];
  550. }
  551. + (void)animationCubeFromLeft:(UIView *)view
  552. {
  553. CATransition *animation = [CATransition animation];
  554. [animation setDuration:0.35f];
  555. [animation setFillMode:kCAFillModeForwards];
  556. [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  557. [animation setType:@"cube"];
  558. [animation setSubtype:@"fromLeft"];
  559. [view.layer addAnimation:animation forKey:nil];
  560. }
  561. + (void)animationCubeFromRight:(UIView *)view
  562. {
  563. CATransition *animation = [CATransition animation];
  564. [animation setDuration:0.35f];
  565. [animation setFillMode:kCAFillModeForwards];
  566. [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  567. [animation setType:@"cube"];
  568. [animation setSubtype:@"fromRight"];
  569. [view.layer addAnimation:animation forKey:nil];
  570. }
  571. + (void)animationCubeFromTop:(UIView *)view
  572. {
  573. CATransition *animation = [CATransition animation];
  574. [animation setDuration:0.35f];
  575. [animation setFillMode:kCAFillModeForwards];
  576. [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  577. [animation setType:@"cube"];
  578. [animation setSubtype:@"fromTop"];
  579. [view.layer addAnimation:animation forKey:nil];
  580. }
  581. + (void)animationCubeFromBottom:(UIView *)view
  582. {
  583. CATransition *animation = [CATransition animation];
  584. [animation setDuration:0.35f];
  585. [animation setFillMode:kCAFillModeForwards];
  586. [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  587. [animation setType:@"cube"];
  588. [animation setSubtype:@"fromBottom"];
  589. [view.layer addAnimation:animation forKey:nil];
  590. }
  591. + (void)animationSuckEffect:(UIView *)view
  592. {
  593. CATransition *animation = [CATransition animation];
  594. [animation setDuration:0.35f];
  595. [animation setFillMode:kCAFillModeForwards];
  596. [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  597. [animation setType:@"suckEffect"];
  598. [view.layer addAnimation:animation forKey:nil];
  599. }
  600. + (void)animationRippleEffect:(UIView *)view
  601. {
  602. CATransition *animation = [CATransition animation];
  603. [animation setDuration:0.35f];
  604. [animation setFillMode:kCAFillModeForwards];
  605. [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  606. [animation setType:@"rippleEffect"];
  607. [view.layer addAnimation:animation forKey:nil];
  608. }
  609. + (void)animationCameraOpen:(UIView *)view
  610. {
  611. CATransition *animation = [CATransition animation];
  612. [animation setDuration:0.35f];
  613. [animation setFillMode:kCAFillModeForwards];
  614. [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  615. [animation setType:@"cameraIrisHollowOpen"];
  616. [animation setSubtype:@"fromRight"];
  617. [view.layer addAnimation:animation forKey:nil];
  618. }
  619. + (void)animationCameraClose:(UIView *)view
  620. {
  621. CATransition *animation = [CATransition animation];
  622. [animation setDuration:0.35f];
  623. [animation setFillMode:kCAFillModeForwards];
  624. [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
  625. [animation setType:@"cameraIrisHollowClose"];
  626. [animation setSubtype:@"fromRight"];
  627. [view.layer addAnimation:animation forKey:nil];
  628. }
  629. @end

最新文章

  1. HTML5简介
  2. 在Centos7服务器上搭建网关服务
  3. uva 1339 Ancient Cipher
  4. JPA @MappedSuperclass注解的使用说明
  5. Andriod——区别DVM与JVM
  6. Java实现邮箱找回密码 --转载
  7. poj 2265 Bee Maja
  8. Aptana jQuery自动提示
  9. Effective C++:条款14:在中小企业资源管理copying表现
  10. c/c++ 算法之快速排序法 冒泡排序法,选择排序法,插入排序法
  11. 5种IE hasLayoutt的属性及其值
  12. HUST 1584 摆放餐桌
  13. Linux部署与基本指令
  14. TF30042: The database is full. Contact your Team Foundation Server administrator.
  15. Vue生命周期详解
  16. C#VS2017添加ReportViewer控件
  17. python sort()方法
  18. Hbase表结构模型
  19. THUSC2017 Day1题解
  20. leetcode(js)算法之914卡牌分组

热门文章

  1. ovs router
  2. poj3122
  3. @property中有哪些属性关键字?/ @property 后面可以有哪些修饰符?
  4. MVC4中EasyUI Tree异步加载JSON数据生成树
  5. do while 与while的区别!
  6. selenium webdriver启动Chrome浏览器后无法输入网址的解决办法
  7. [转载]Android利用convertView优化ListView性能
  8. java 获取黑屏信息保存在list中,截取字符执行
  9. 独立硬盘冗余阵列与HDFS
  10. Android音频开发之——如何播放一帧音频