猎豆优选

CALayer+Transition.m 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #import "CALayer+Transition.h"
  2. @implementation CALayer (Transition)
  3. /**
  4. * 转场动画
  5. * @param animType 转场动画类型
  6. * @param subtype 转动动画方向
  7. * @param curve 转动动画曲线
  8. * @param duration 转动动画时长
  9. * @return 转场动画实例
  10. */
  11. -(CATransition *)transitionWithAnimType:(TransitionAnimType)animType subType:(TransitionSubType)subType curve:(TransitionCurve)curve duration:(CGFloat)duration{
  12. NSString *key = @"transition";
  13. if([self animationForKey:key]!=nil){
  14. [self removeAnimationForKey:key];
  15. }
  16. CATransition *transition=[CATransition animation];
  17. //动画时长
  18. transition.duration=duration;
  19. //动画类型
  20. transition.type=[self animaTypeWithTransitionType:animType];
  21. //动画方向
  22. transition.subtype=[self animaSubtype:subType];
  23. //缓动函数
  24. transition.timingFunction=[CAMediaTimingFunction functionWithName:[self curve:curve]];
  25. //完成动画删除
  26. transition.removedOnCompletion = YES;
  27. [self addAnimation:transition forKey:key];
  28. return transition;
  29. }
  30. // 返回动画曲线
  31. -(NSString *)curve:(TransitionCurve)curve{
  32. //曲线数组
  33. NSArray *funcNames=@[kCAMediaTimingFunctionDefault,kCAMediaTimingFunctionEaseIn,kCAMediaTimingFunctionEaseInEaseOut,kCAMediaTimingFunctionEaseOut,kCAMediaTimingFunctionLinear];
  34. return [self objFromArray:funcNames index:curve isRamdom:(TransitionCurveRamdom == curve)];
  35. }
  36. /*
  37. * 返回动画方向
  38. */
  39. -(NSString *)animaSubtype:(TransitionSubType)subType{
  40. //设置转场动画的方向
  41. NSArray *subtypes=@[kCATransitionFromTop,kCATransitionFromLeft,kCATransitionFromBottom,kCATransitionFromRight];
  42. return [self objFromArray:subtypes index:subType isRamdom:(TransitionSubtypesFromRamdom == subType)];
  43. }
  44. /*
  45. * 返回动画类型
  46. */
  47. -(NSString *)animaTypeWithTransitionType:(TransitionAnimType)type{
  48. //设置转场动画的类型
  49. NSArray *animArray=@[@"rippleEffect",@"suckEffect",@"pageCurl",@"oglFlip",@"cube",@"reveal",@"pageUnCurl"];
  50. return [self objFromArray:animArray index:type isRamdom:(TransitionAnimTypeRamdom == type)];
  51. }
  52. // 统一从数据返回对象
  53. -(id)objFromArray:(NSArray *)array index:(NSUInteger)index isRamdom:(BOOL)isRamdom{
  54. NSUInteger count = array.count;
  55. NSUInteger i = isRamdom?arc4random_uniform((u_int32_t)count) : index;
  56. return array[i];
  57. }
  58. @end