PopAnimationTool.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // PopAnimationTool.m
  3. // PopView
  4. //
  5. // Created by 李林 on 2018/3/8.
  6. // Copyright © 2018年 李林. All rights reserved.
  7. //
  8. #import "PopAnimationTool.h"
  9. @implementation PopAnimationTool
  10. + (CABasicAnimation *)getShowPopAnimationWithType:(PopViewDirection)popDirecton contentView:(UIView *)contentView belowView:(UIView *)belowView{
  11. CABasicAnimation *showAnima = [CABasicAnimation animation];
  12. showAnima.duration = animationDuration;
  13. showAnima.repeatCount = 1;
  14. showAnima.fillMode = kCAFillModeForwards;
  15. showAnima.removedOnCompletion = YES;
  16. CGFloat width = contentView.bounds.size.width;
  17. CGFloat height = contentView.bounds.size.height;
  18. CGFloat ScreenWidth = [UIScreen mainScreen].bounds.size.width;
  19. CGFloat ScreenHeight = [UIScreen mainScreen].bounds.size.height;
  20. switch (popDirecton) {
  21. case PopViewDirection_PopUpLeft:
  22. case PopViewDirection_PopUpRight:
  23. case PopViewDirection_PopUpTop:
  24. case PopViewDirection_PopUpBottom:
  25. case PopViewDirection_PopUpNone:{
  26. showAnima.keyPath = @"transform";
  27. CATransform3D tofrom = CATransform3DMakeScale(1, 1, 1);
  28. CATransform3D from = CATransform3DMakeScale(0, 0, 1);
  29. showAnima.fromValue = [NSValue valueWithCATransform3D:from];
  30. showAnima.toValue = [NSValue valueWithCATransform3D:tofrom];
  31. }break;
  32. case PopViewDirection_SlideInCenter:{
  33. showAnima.keyPath = @"position";
  34. showAnima.fromValue = [NSValue valueWithCGPoint:CGPointMake(ScreenWidth/2, -height/2)];
  35. showAnima.toValue = [NSValue valueWithCGPoint:CGPointMake(ScreenWidth/2, ScreenHeight/2)];
  36. showAnima.removedOnCompletion = YES;
  37. }break;
  38. case PopViewDirection_SlideFromLeft:
  39. showAnima.keyPath = @"position";
  40. showAnima.fromValue = [NSValue valueWithCGPoint:CGPointMake(-width/2, ScreenHeight/2)];
  41. showAnima.toValue = [NSValue valueWithCGPoint:CGPointMake(width/2, ScreenHeight/2)];
  42. break;
  43. case PopViewDirection_SlideFromRight:
  44. showAnima.keyPath = @"position";
  45. showAnima.fromValue = [NSValue valueWithCGPoint:CGPointMake(ScreenWidth+width/2, ScreenHeight/2)];
  46. showAnima.toValue = [NSValue valueWithCGPoint:CGPointMake(ScreenWidth-width/2, ScreenHeight/2)];
  47. break;
  48. case PopViewDirection_SlideFromUp:
  49. showAnima.keyPath = @"position";
  50. showAnima.fromValue = [NSValue valueWithCGPoint:CGPointMake(ScreenWidth/2, -height/2)];
  51. showAnima.toValue = [NSValue valueWithCGPoint:CGPointMake(ScreenWidth/2, height/2)];
  52. break;
  53. case PopViewDirection_SlideFromBottom:
  54. showAnima.keyPath = @"position";
  55. showAnima.fromValue = [NSValue valueWithCGPoint:CGPointMake(ScreenWidth/2, ScreenHeight+height/2)];
  56. showAnima.toValue = [NSValue valueWithCGPoint:CGPointMake(ScreenWidth/2, ScreenHeight-height/2)];
  57. break;
  58. case PopViewDirection_SlideBelowView:
  59. showAnima.keyPath = @"position";
  60. showAnima.fromValue = [NSValue valueWithCGPoint:CGPointMake(belowView.bounds.size.width/2, -contentView.bounds.size.height/2)];
  61. showAnima.toValue = [NSValue valueWithCGPoint:CGPointMake(belowView.bounds.size.width/2, contentView.bounds.size.height/2)];
  62. break;
  63. default:
  64. break;
  65. }
  66. return showAnima;
  67. }
  68. + (CABasicAnimation *)getHidenPopAnimationWithType:(PopViewDirection)popDirecton contentView:(UIView *)contentView belowView:(UIView *)belowView{
  69. CABasicAnimation *hidenAnima = [CABasicAnimation animation];
  70. hidenAnima.duration = animationDuration;
  71. hidenAnima.fillMode = kCAFillModeForwards;
  72. hidenAnima.removedOnCompletion = YES;
  73. hidenAnima.repeatCount = 1;
  74. CGFloat width = contentView.bounds.size.width;
  75. CGFloat height = contentView.bounds.size.height;
  76. CGFloat ScreenWidth = [UIScreen mainScreen].bounds.size.width;
  77. CGFloat ScreenHeight = [UIScreen mainScreen].bounds.size.height;
  78. switch (popDirecton) {
  79. case PopViewDirection_PopUpLeft:
  80. case PopViewDirection_PopUpRight:
  81. case PopViewDirection_PopUpTop:
  82. case PopViewDirection_PopUpBottom:
  83. case PopViewDirection_PopUpNone:{
  84. hidenAnima.keyPath = @"transform";
  85. CATransform3D tofrom = CATransform3DMakeScale(0, 0, 1);
  86. CATransform3D from = CATransform3DMakeScale(1, 1, 1);
  87. hidenAnima.fromValue = [NSValue valueWithCATransform3D:from];
  88. hidenAnima.toValue = [NSValue valueWithCATransform3D:tofrom];
  89. }break;
  90. case PopViewDirection_SlideInCenter:{
  91. hidenAnima.keyPath = @"position";
  92. hidenAnima.fromValue = [NSValue valueWithCGPoint:CGPointMake(ScreenWidth/2, ScreenHeight/2)];
  93. hidenAnima.toValue = [NSValue valueWithCGPoint:CGPointMake(ScreenWidth/2, ScreenHeight+height/2)];
  94. }break;
  95. case PopViewDirection_SlideFromLeft:
  96. hidenAnima.keyPath = @"position";
  97. hidenAnima.fromValue = [NSValue valueWithCGPoint:CGPointMake(width/2, ScreenHeight/2)];
  98. hidenAnima.toValue = [NSValue valueWithCGPoint:CGPointMake(-width/2, ScreenHeight/2)];
  99. break;
  100. case PopViewDirection_SlideFromRight:
  101. hidenAnima.keyPath = @"position";
  102. hidenAnima.fromValue = [NSValue valueWithCGPoint:CGPointMake(ScreenWidth-width/2, ScreenHeight/2)];
  103. hidenAnima.toValue = [NSValue valueWithCGPoint:CGPointMake(ScreenWidth+width/2, ScreenHeight/2)];
  104. break;
  105. case PopViewDirection_SlideFromUp:
  106. hidenAnima.keyPath = @"position";
  107. hidenAnima.fromValue = [NSValue valueWithCGPoint:CGPointMake(ScreenWidth/2, height/2)];
  108. hidenAnima.toValue = [NSValue valueWithCGPoint:CGPointMake(ScreenWidth/2, -height/2)];
  109. break;
  110. case PopViewDirection_SlideFromBottom:
  111. hidenAnima.keyPath = @"position";
  112. hidenAnima.fromValue = [NSValue valueWithCGPoint:CGPointMake(ScreenWidth/2, ScreenHeight-height/2)];
  113. hidenAnima.toValue = [NSValue valueWithCGPoint:CGPointMake(ScreenWidth/2, ScreenHeight+height/2)];
  114. break;
  115. case PopViewDirection_SlideBelowView:
  116. hidenAnima.keyPath = @"position";
  117. hidenAnima.fromValue = [NSValue valueWithCGPoint:CGPointMake(belowView.bounds.size.width/2, contentView.bounds.size.height/2)];
  118. hidenAnima.toValue = [NSValue valueWithCGPoint:CGPointMake(belowView.bounds.size.width/2, -contentView.bounds.size.height/2)];
  119. default:
  120. break;
  121. }
  122. return hidenAnima;
  123. }
  124. @end