OliveappLivenessDetectionViewController+Animation.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // OliveappLivenessDetectionViewController+Animation.m
  3. // AppSampleYitu
  4. //
  5. // Created by Xiaoyang Lin on 17/2/8.
  6. // Copyright © 2017年 Oliveapp. All rights reserved.
  7. //
  8. #import "OliveappLivenessDetectionViewController+Animation.h"
  9. #import "OliveappLivenessDetectionViewController.h"
  10. #import "OliveappApplicationParameter.h"
  11. #import "OliveappChinLayer.h"
  12. #import "OliveappChinUpLayer.h"
  13. #import "OliveappMouthLayer.h"
  14. CABasicAnimation * mAnimation;
  15. const float HINT_TEXT_DURATION = 0.15;
  16. NSString * mHintText;
  17. int mXBias = 0;
  18. OliveappChinLayer * chinLayer;
  19. OliveappChinUpLayer * chinUpLayer;
  20. OliveappMouthLayer * mouthLayerIn;
  21. OliveappMouthLayer * mouthLayerOut;
  22. OliveappEyeLayer * leftEyeLayer;
  23. OliveappEyeLayer * rightEyeLayer;
  24. @implementation OliveappLivenessDetectionViewController (Animation)
  25. - (void) playAperture {
  26. [self.mBlueFrame animationWithScale:1.2f
  27. withDuration:HINT_TEXT_DURATION];
  28. [self.mLeftDownYellowFrame flashAnimationWithTranslate:-25.0f
  29. withDuration:HINT_TEXT_DURATION];
  30. [self.mRightUpYellowFrame flashAnimationWithTranslate:25.0f
  31. withDuration:HINT_TEXT_DURATION];
  32. }
  33. - (void) playHintTextAnimation: (NSString *) hintText {
  34. mHintText = hintText;
  35. __weak typeof(self) weakSelf = self;
  36. mAnimation = [self.mHintView animationWithScale:0.f
  37. withDuration:HINT_TEXT_DURATION
  38. withDelay:0
  39. withTimeFunction:nil
  40. withDelegate:weakSelf];
  41. mXBias = self.mRightUpYellowFrame.frame.origin.x - self.mHintView.center.x;
  42. [self.mLeftDownYellowFrame animationWithTranslate:mXBias
  43. withDuration:HINT_TEXT_DURATION
  44. withDelay:0.f];
  45. [self.mRightUpYellowFrame animationWithTranslate:-mXBias
  46. withDuration:HINT_TEXT_DURATION
  47. withDelay:0.f];
  48. }
  49. - (void) playActionAnimation: (int) actionType
  50. withLocation: (NSArray *) locationArray {
  51. [self.view.layer removeAllAnimations];
  52. [chinLayer removeFromSuperlayer];
  53. [chinUpLayer removeFromSuperlayer];
  54. [leftEyeLayer removeFromSuperlayer];
  55. [rightEyeLayer removeFromSuperlayer];
  56. [mouthLayerIn removeFromSuperlayer];
  57. [mouthLayerOut removeFromSuperlayer];
  58. switch (actionType) {
  59. case EYE_CLOSE_ACTION: {
  60. //闭眼的动画
  61. CGPoint leftPoint = [locationArray[0] CGPointValue];
  62. CGPoint rightPoint = [locationArray[1] CGPointValue];
  63. //左右眼layer
  64. leftEyeLayer = [OliveappEyeLayer layer];
  65. leftEyeLayer.contents = (__bridge id)([UIImage imageNamed:@"oliveapp_eye_blink.png"].CGImage);
  66. leftEyeLayer.bounds = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width / 4, [UIScreen mainScreen].bounds.size.width / 4);
  67. rightEyeLayer = [OliveappEyeLayer layer];
  68. rightEyeLayer.contents = (__bridge id)([UIImage imageNamed:@"oliveapp_eye_blink.png"].CGImage);
  69. rightEyeLayer.bounds = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width / 4, [UIScreen mainScreen].bounds.size.width / 4);
  70. [self.view.layer addSublayer:leftEyeLayer];
  71. [self.view.layer addSublayer:rightEyeLayer];
  72. [leftEyeLayer animationAt:leftPoint
  73. withDelay:0];
  74. [rightEyeLayer animationAt:rightPoint
  75. withDelay:0.25f];
  76. break;
  77. }
  78. case HEAD_UP_ACTION: {
  79. //抬头的动画
  80. chinLayer = [OliveappChinLayer layer];
  81. chinLayer.contents = (__bridge id)([UIImage imageNamed:@"oliveapp_chin.png"].CGImage);
  82. chinLayer.bounds = CGRectMake(0, 0, self.mBlueFrame.frame.size.width / 4 * 3, self.mBlueFrame.frame.size.width / 2);
  83. CGPoint chinPoint = CGPointMake(self.mBlueFrame.center.x, self.mBlueFrame.center.y + self.mBlueFrame.frame.size.width / 4);
  84. chinLayer.position = chinPoint;
  85. chinLayer.contentsGravity = kCAGravityResizeAspect;
  86. [self.view.layer addSublayer:chinLayer];
  87. [chinLayer animationWithScale:1.5 withDuration:HINT_TEXT_DURATION * 2];
  88. chinUpLayer = [OliveappChinUpLayer layer];
  89. chinUpLayer.contents = (__bridge id)([UIImage imageNamed:@"oliveapp_chin_up.png"].CGImage);
  90. chinUpLayer.bounds = CGRectMake(0, 0, self.mBlueFrame.frame.size.width / 7, self.mBlueFrame.frame.size.width / 5);
  91. CGPoint chinUpPoint = CGPointMake(self.mBlueFrame.center.x, self.mBlueFrame.center.y + self.mBlueFrame.frame.size.width / 4);
  92. chinUpLayer.position = chinUpPoint;
  93. chinUpLayer.contentsGravity = kCAGravityResizeAspect;
  94. [self.view.layer addSublayer:chinUpLayer];
  95. [chinUpLayer animationWithDuration:HINT_TEXT_DURATION * 2];
  96. break;
  97. }
  98. case MOUTH_OPEN_ACTION: {
  99. CGPoint mouthPosition = [locationArray[0] CGPointValue];
  100. mouthLayerIn = [OliveappMouthLayer layer];
  101. mouthLayerIn.contents = (__bridge id)([UIImage imageNamed:@"oliveapp_mouth_close.png"].CGImage);
  102. mouthLayerIn.bounds = CGRectMake(0, 0, self.mBlueFrame.frame.size.width / 4, self.mBlueFrame.frame.size.width / 8);
  103. mouthLayerIn.position = mouthPosition;
  104. mouthLayerIn.contentsGravity = kCAGravityResizeAspect;
  105. [self.view.layer addSublayer:mouthLayerIn];
  106. [mouthLayerIn animationFadeIn:2
  107. withDuration:HINT_TEXT_DURATION * 3
  108. withDelay:0];
  109. mouthLayerOut = [OliveappMouthLayer layer];
  110. mouthLayerOut.contents = (__bridge id)([UIImage imageNamed:@"oliveapp_mouth_open.png"].CGImage);
  111. mouthLayerOut.bounds = CGRectMake(0, 0, self.mBlueFrame.frame.size.width / 4, self.mBlueFrame.frame.size.width / 8);
  112. mouthLayerOut.position = mouthPosition;
  113. mouthLayerOut.contentsGravity = kCAGravityResizeAspect;
  114. [self.view.layer addSublayer:mouthLayerOut];
  115. [mouthLayerOut animationFadeOut:2
  116. withDuration:HINT_TEXT_DURATION * 3
  117. withDelay:HINT_TEXT_DURATION * 3];
  118. break;
  119. }
  120. default:
  121. break;
  122. }
  123. }
  124. - (void)animationDidStop:(CABasicAnimation *)anim
  125. finished:(BOOL)flag {
  126. if (flag) {
  127. if (anim == [self.mHintView.layer animationForKey:@"hintView"]) {
  128. [self.mHintView.layer removeAllAnimations];
  129. self.mHintView.text = mHintText;
  130. mAnimation = [self.mHintView animationWithScale:1.f
  131. withDuration:HINT_TEXT_DURATION
  132. withDelay:0
  133. withTimeFunction:nil
  134. withDelegate:nil];
  135. [self.mLeftDownYellowFrame animationWithTranslate:-mXBias
  136. withDuration:HINT_TEXT_DURATION
  137. withDelay:0.f];
  138. [self.mRightUpYellowFrame animationWithTranslate:mXBias
  139. withDuration:HINT_TEXT_DURATION
  140. withDelay:0.f];
  141. }
  142. if (anim == [self.mHintView.layer animationForKey:@"mouthClose"]) {
  143. [self.mHintView.layer removeAllAnimations];
  144. self.mHintView.text = mHintText;
  145. mAnimation = [self.mHintView animationWithScale:1.f
  146. withDuration:HINT_TEXT_DURATION
  147. withDelay:0
  148. withTimeFunction:nil
  149. withDelegate:nil];
  150. [self.mLeftDownYellowFrame animationWithTranslate:-mXBias
  151. withDuration:HINT_TEXT_DURATION
  152. withDelay:0.f];
  153. [self.mRightUpYellowFrame animationWithTranslate:mXBias
  154. withDuration:HINT_TEXT_DURATION
  155. withDelay:0.f];
  156. }
  157. }
  158. }
  159. @end