KSDelayCheckView.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. //
  2. // KSDelayCheckView.m
  3. // KulexiuSchoolStudent
  4. //
  5. // Created by 王智 on 2024/7/8.
  6. //
  7. #import "KSDelayCheckView.h"
  8. #import <Lottie/Lottie.h>
  9. #import <AVFoundation/AVFoundation.h>
  10. #import <MediaPlayer/MediaPlayer.h>
  11. @interface KSDelayCheckView ()
  12. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *shipTopSpace;
  13. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *shipboardWidth;
  14. @property (weak, nonatomic) IBOutlet UIImageView *shipView;
  15. @property (weak, nonatomic) IBOutlet UIImageView *tipsImage; // 提示
  16. @property (weak, nonatomic) IBOutlet UIView *mainView; // 主动画
  17. @property (nonatomic, assign) DELAYCHECK_TYPE status;
  18. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *backButtonLeft;
  19. @property (weak, nonatomic) IBOutlet UIButton *retryButton;
  20. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *retryButtonRight;
  21. @property (nonatomic, copy) DelayCheckCallback callback;
  22. @property (weak, nonatomic) IBOutlet UIView *otherTipsView;
  23. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *tipsWidth;
  24. @property (nonatomic, strong) NSTimer *timer;
  25. @property (nonatomic, assign) NSInteger timeSecond;
  26. @property (nonatomic, assign) BOOL hasSendStart;
  27. @property (nonatomic, assign) BOOL skipFirst;
  28. @property (nonatomic, assign) CGFloat systemVolume;
  29. @property (weak, nonatomic) IBOutlet UIView *lightBgView;
  30. @property (nonatomic, strong) LOTAnimationView *ligntAniView;
  31. @property (weak, nonatomic) IBOutlet UIView *sandbeachAniBgView;
  32. @property (nonatomic, strong) LOTAnimationView *sandbeachAniView;
  33. @property (weak, nonatomic) IBOutlet UIView *seaGrassLeft;
  34. @property (nonatomic, strong) LOTAnimationView *seaGrassLeftAniView;
  35. @property (weak, nonatomic) IBOutlet UIView *seaGrassRight;
  36. @property (nonatomic, strong) LOTAnimationView *seaGrassRightAniView;
  37. @property (weak, nonatomic) IBOutlet UIView *bubbleBgView;
  38. @property (nonatomic, strong) LOTAnimationView *bubbleAniView;
  39. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *tipsBottomSpace;
  40. @end
  41. @implementation KSDelayCheckView
  42. - (void)awakeFromNib {
  43. [super awakeFromNib];
  44. self.otherTipsView.hidden = YES;
  45. [self prepareAnimation];
  46. self.backButtonLeft.constant = 25;
  47. self.systemVolume = [self getCurrentVolume];
  48. if (IS_IPAD) {
  49. CGFloat shipWidth = 298 * 1.5;
  50. self.shipboardWidth.constant = shipWidth;
  51. self.tipsWidth.constant = 340 * 1.2;
  52. self.tipsBottomSpace.constant = 80.0f;
  53. self.shipTopSpace.constant = (KLandscapeHeight - shipWidth / 298 * 254) / 2.0f - 50;
  54. }
  55. self.retryButtonRight.constant = self.tipsWidth.constant / 340 * 54;
  56. }
  57. // 设置音量
  58. - (void)setCurrentVolume {
  59. if (self.systemVolume < 1.0) {
  60. MPMusicPlayerController *mp = [MPMusicPlayerController applicationMusicPlayer];
  61. mp.volume = 1;//0为最小1为最大
  62. }
  63. }
  64. + (instancetype)shareInstance {
  65. KSDelayCheckView *view = [[[NSBundle mainBundle] loadNibNamed:@"KSDelayCheckView" owner:nil options:nil] firstObject];
  66. return view;
  67. }
  68. - (void)delayCheckCallback:(DelayCheckCallback)callback {
  69. if (callback) {
  70. self.callback = callback;
  71. }
  72. }
  73. - (void)changeCheckType:(DELAYCHECK_TYPE)status {
  74. if (status == self.status) {
  75. return;
  76. }
  77. self.status = status;
  78. }
  79. - (void)cancelDelayCheck {
  80. [self removeCheckView];
  81. if (self.callback) {
  82. self.callback(DELAYCHECK_CALLBACK_CANCEL);
  83. }
  84. }
  85. // 取消检测
  86. - (IBAction)cancelAction:(id)sender {
  87. [self cancelDelayCheck];
  88. }
  89. - (void)stopPlayAllAnimation {
  90. if (self.ligntAniView.isAnimationPlaying) {
  91. [self.ligntAniView stop];
  92. }
  93. if (self.sandbeachAniView.isAnimationPlaying) {
  94. [self.sandbeachAniView stop];
  95. }
  96. if (self.seaGrassLeftAniView.isAnimationPlaying) {
  97. [self.seaGrassLeftAniView stop];
  98. }
  99. if (self.seaGrassRightAniView.isAnimationPlaying) {
  100. [self.seaGrassRightAniView stop];
  101. }
  102. if (self.bubbleAniView.isAnimationPlaying) {
  103. [self.bubbleAniView stop];
  104. }
  105. [self removeMoveAnimation];
  106. }
  107. - (void)finishAction {
  108. [UIView animateWithDuration:0.5f delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
  109. [self layoutIfNeeded];
  110. } completion:^(BOOL finished) {
  111. self.layer.opacity = 0.0f;
  112. if (finished) {
  113. [self removeCheckView];
  114. if (self.callback) {
  115. self.callback(DELAYCHECK_CALLBACK_FINISH);
  116. }
  117. }
  118. }];
  119. }
  120. - (void)removeCheckView {
  121. [self stopPlayAllAnimation];
  122. [self removeFromSuperview];
  123. [self.timer setFireDate:[NSDate distantPast]];
  124. [_timer invalidate];
  125. _timer = nil;
  126. }
  127. - (IBAction)retryCheck:(id)sender {
  128. if (self.status == DELAYCHECK_TYPE_FAIL) {
  129. [self changeCheckType:DELAYCHECK_TYPE_PREPARE];
  130. }
  131. }
  132. - (void)prepareAnimation {
  133. self.status = DELAYCHECK_TYPE_PREPARE;
  134. self.ligntAniView = [self configAnimation:@"delay_check_ light" attachInView:self.lightBgView];
  135. [self.ligntAniView play];
  136. self.sandbeachAniView = [self configAnimation:@"delay_check_sandbeach" attachInView:self.sandbeachAniBgView];
  137. [self.sandbeachAniView play];
  138. self.seaGrassLeftAniView = [self configAnimation:@"delay_check_seaGrass_left" attachInView:self.seaGrassLeft];
  139. [self.seaGrassLeftAniView play];
  140. self.seaGrassRightAniView = [self configAnimation:@"delay_check_seaGrass_right" attachInView:self.seaGrassRight];
  141. [self.seaGrassRightAniView play];
  142. self.bubbleAniView = [self configAnimation:@"delay_check_ bubble" attachInView:self.bubbleBgView];
  143. [self.bubbleAniView play];
  144. [self addMoveAnimation];
  145. }
  146. - (LOTAnimationView *)configAnimation:(NSString *)aniName attachInView:(UIView *)view {
  147. LOTAnimationView *aniView = [LOTAnimationView animationWithFilePath:[[NSBundle mainBundle] pathForResource:aniName ofType:@"json"]];
  148. aniView.loopAnimation = YES;
  149. aniView.contentMode = UIViewContentModeScaleAspectFill;
  150. aniView.animationSpeed = 1.0f;
  151. aniView.userInteractionEnabled = YES;
  152. [view addSubview:aniView];
  153. [aniView mas_makeConstraints:^(MASConstraintMaker *make) {
  154. make.left.right.top.bottom.mas_equalTo(view);
  155. }];
  156. return aniView;
  157. }
  158. - (void)setStatus:(DELAYCHECK_TYPE)status {
  159. _status = status;
  160. switch (status) {
  161. case DELAYCHECK_TYPE_PREPARE:
  162. {
  163. self.hasSendStart = NO;
  164. self.retryButton.hidden = YES;
  165. [self.tipsImage setImage:[UIImage imageNamed:@"check_prepareTips"]];
  166. [self.shipView setImage:[UIImage imageNamed:@"delayCheck_ship_prepare"]];
  167. if (self.isHeadsetOff == NO) {
  168. [self startTimer];
  169. }
  170. [self startDelayCheck];
  171. }
  172. break;
  173. case DELAYCHECK_TYPE_ING:
  174. {
  175. self.retryButton.hidden = YES;
  176. [self.tipsImage setImage:[UIImage imageNamed:@"check_ingTips"]];
  177. [self hideTips];
  178. [self.shipView setImage:[UIImage imageNamed:@"delayCheck_ship_checking"]];
  179. }
  180. break;
  181. case DELAYCHECK_TYPE_SUCCESS:
  182. {
  183. self.retryButton.hidden = YES;
  184. [self.tipsImage setImage:[UIImage imageNamed:@"check_finishTips"]];
  185. [self.shipView setImage:[UIImage imageNamed:@"delayCheck_ship_success"]];
  186. @weakObj(self);
  187. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  188. @strongObj(self);
  189. [self finishAction];
  190. });
  191. [self hideTips];
  192. }
  193. break;
  194. case DELAYCHECK_TYPE_FAIL:
  195. {
  196. self.retryButton.hidden = NO;
  197. [self.tipsImage setImage:[UIImage imageNamed:@"check_failedTips"]];
  198. [self.shipView setImage:[UIImage imageNamed:@"delayCheck_ship_failer"]];
  199. [self hideTips];
  200. }
  201. break;
  202. default:
  203. break;
  204. }
  205. }
  206. - (IBAction)tipsCloseAction:(id)sender {
  207. [self hideTips];
  208. }
  209. - (void)hideTips {
  210. self.otherTipsView.hidden = YES;
  211. }
  212. #pragma mark ----- setter
  213. - (void)setIsSocketConnected:(BOOL)isSocketConnected {
  214. _isSocketConnected = isSocketConnected;
  215. [self startDelayCheck];
  216. }
  217. - (void)setIsHeadsetOff:(BOOL)isHeadsetOff {
  218. _isHeadsetOff = isHeadsetOff;
  219. if (isHeadsetOff) {
  220. [self setCurrentVolume];
  221. }
  222. if (self.status == DELAYCHECK_TYPE_ING && isHeadsetOff == NO) {
  223. if (self.callback) {
  224. self.callback(DELAYCHECK_CALLBACK_FAILED);
  225. }
  226. }
  227. else if (self.status == DELAYCHECK_TYPE_PREPARE) {
  228. if (isHeadsetOff == NO) {
  229. [self startTimer];
  230. }
  231. else if (isHeadsetOff == YES) {
  232. [self stopTimer];
  233. [self hideTips];
  234. }
  235. }
  236. [self startDelayCheck];
  237. }
  238. - (void)startDelayCheck {
  239. NSLog(@"-----isHeadsetOff :%d, isSocketConnected : %d",_isHeadsetOff, _isSocketConnected);
  240. if (self.isHeadsetOff && self.isSocketConnected) {
  241. // 直接开始下一步
  242. if (self.status == DELAYCHECK_TYPE_PREPARE && self.hasSendStart == NO) {
  243. self.hasSendStart = YES;
  244. @weakObj(self);
  245. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  246. @strongObj(self);
  247. if (self.callback) {
  248. self.callback(DELAYCHECK_CALLBACK_START);
  249. }
  250. });
  251. }
  252. }
  253. }
  254. - (void)checkHeadsetTips {
  255. if (self.skipFirst == NO) {
  256. [self stopTimer];
  257. if (self.status == DELAYCHECK_TYPE_PREPARE) {
  258. self.otherTipsView.hidden = NO;
  259. }
  260. }
  261. self.skipFirst = NO;
  262. }
  263. - (void)startTimer {
  264. self.skipFirst = YES;
  265. [self.timer setFireDate:[NSDate distantPast]];
  266. NSLog(@"-------- start timer");
  267. }
  268. - (void)stopTimer {
  269. [self.timer setFireDate:[NSDate distantFuture]];//暂停计时器
  270. }
  271. #pragma mark ------ timer
  272. - (NSTimer *)timer{
  273. if (!_timer) {
  274. MJWeakSelf;
  275. _timer = [NSTimer scheduledTimerWithTimeInterval:5.0f repeats:YES block:^(NSTimer * _Nonnull timer) {
  276. [weakSelf timerAction];
  277. }];
  278. [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
  279. [_timer setFireDate:[NSDate distantFuture]];
  280. }
  281. return _timer;
  282. }
  283. - (void)timerAction {
  284. NSLog(@"-------- checkHeadsetTips");
  285. [self checkHeadsetTips];
  286. }
  287. - (void)dealloc {
  288. NSLog(@"------ delay check View delloc");
  289. }
  290. - (float)getCurrentVolume {
  291. return [[AVAudioSession sharedInstance] outputVolume];
  292. }
  293. - (void)addMoveAnimation {
  294. CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
  295. animation.toValue= @(-12);
  296. animation.duration = 1;
  297. animation.removedOnCompletion = NO;
  298. animation.fillMode = kCAFillModeForwards; // 保持动画完成后的状态
  299. animation.repeatCount = CGFLOAT_MAX;
  300. animation.autoreverses = YES; // 动画完成后反向执行
  301. animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  302. [self.shipView.layer addAnimation:animation forKey:@"translationAnimation"];
  303. }
  304. - (void)removeMoveAnimation {
  305. [self.shipView.layer removeAllAnimations];
  306. }
  307. /*
  308. // Only override drawRect: if you perform custom drawing.
  309. // An empty implementation adversely affects performance during animation.
  310. - (void)drawRect:(CGRect)rect {
  311. // Drawing code
  312. }
  313. */
  314. @end