KSDelayCheckView.m 11 KB

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