123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- //
- // KSDelayCheckView.m
- // KulexiuSchoolStudent
- //
- // Created by 王智 on 2024/7/8.
- //
- #import "KSDelayCheckView.h"
- #import <Lottie/Lottie.h>
- #import <AVFoundation/AVFoundation.h>
- #import <MediaPlayer/MediaPlayer.h>
- @interface KSDelayCheckView ()
- @property (weak, nonatomic) IBOutlet NSLayoutConstraint *shipboardWidth;
- @property (weak, nonatomic) IBOutlet UIImageView *shipView;
- @property (weak, nonatomic) IBOutlet UIImageView *tipsImage; // 提示
- @property (weak, nonatomic) IBOutlet UIView *mainView; // 主动画
- @property (nonatomic, assign) DELAYCHECK_TYPE status;
- @property (weak, nonatomic) IBOutlet NSLayoutConstraint *backButtonLeft;
- @property (weak, nonatomic) IBOutlet UIButton *retryButton;
- @property (weak, nonatomic) IBOutlet NSLayoutConstraint *retryButtonRight;
- @property (nonatomic, copy) DelayCheckCallback callback;
- @property (weak, nonatomic) IBOutlet UIView *otherTipsView;
- @property (weak, nonatomic) IBOutlet NSLayoutConstraint *tipsWidth;
- @property (nonatomic, strong) NSTimer *timer;
- @property (nonatomic, assign) NSInteger timeSecond;
- @property (nonatomic, assign) BOOL hasSendStart;
- @property (nonatomic, assign) BOOL skipFirst;
- @property (nonatomic, assign) CGFloat systemVolume;
- @property (weak, nonatomic) IBOutlet UIView *lightBgView;
- @property (nonatomic, strong) LOTAnimationView *ligntAniView;
- @property (weak, nonatomic) IBOutlet UIView *sandbeachAniBgView;
- @property (nonatomic, strong) LOTAnimationView *sandbeachAniView;
- @property (weak, nonatomic) IBOutlet UIView *seaGrassLeft;
- @property (nonatomic, strong) LOTAnimationView *seaGrassLeftAniView;
- @property (weak, nonatomic) IBOutlet UIView *seaGrassRight;
- @property (nonatomic, strong) LOTAnimationView *seaGrassRightAniView;
- @property (weak, nonatomic) IBOutlet UIView *bubbleBgView;
- @property (nonatomic, strong) LOTAnimationView *bubbleAniView;
- @end
- @implementation KSDelayCheckView
- - (void)awakeFromNib {
- [super awakeFromNib];
- self.otherTipsView.hidden = YES;
- [self prepareAnimation];
- self.backButtonLeft.constant = 25;
- self.systemVolume = [self getCurrentVolume];
- if (IS_IPAD) {
- self.shipboardWidth.constant = 298 * 1.2;
- self.tipsWidth.constant = 340 * 1.2;
- // self.tipsBottomSpace.constant = 40.0f;
- }
- self.retryButtonRight.constant = self.tipsWidth.constant / 340 * 54;
-
- }
- // 设置音量
- - (void)setCurrentVolume {
- if (self.systemVolume < 1.0) {
- MPMusicPlayerController *mp = [MPMusicPlayerController applicationMusicPlayer];
- mp.volume = 1;//0为最小1为最大
- }
- }
- + (instancetype)shareInstance {
- KSDelayCheckView *view = [[[NSBundle mainBundle] loadNibNamed:@"KSDelayCheckView" owner:nil options:nil] firstObject];
- return view;
- }
- - (void)delayCheckCallback:(DelayCheckCallback)callback {
- if (callback) {
- self.callback = callback;
- }
- }
- - (void)changeCheckType:(DELAYCHECK_TYPE)status {
- if (status == self.status) {
- return;
- }
- self.status = status;
- }
- - (void)cancelDelayCheck {
- [self removeCheckView];
- if (self.callback) {
- self.callback(DELAYCHECK_CALLBACK_CANCEL);
- }
- }
- // 取消检测
- - (IBAction)cancelAction:(id)sender {
- [self cancelDelayCheck];
- }
- - (void)stopPlayAllAnimation {
- if (self.ligntAniView.isAnimationPlaying) {
- [self.ligntAniView stop];
- }
- if (self.sandbeachAniView.isAnimationPlaying) {
- [self.sandbeachAniView stop];
- }
- if (self.seaGrassLeftAniView.isAnimationPlaying) {
- [self.seaGrassLeftAniView stop];
- }
- if (self.seaGrassRightAniView.isAnimationPlaying) {
- [self.seaGrassRightAniView stop];
- }
- if (self.bubbleAniView.isAnimationPlaying) {
- [self.bubbleAniView stop];
- }
- [self removeMoveAnimation];
- }
- - (void)finishAction {
- [UIView animateWithDuration:0.5f delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
- [self layoutIfNeeded];
- } completion:^(BOOL finished) {
- self.layer.opacity = 0.0f;
- if (finished) {
- [self removeCheckView];
- if (self.callback) {
- self.callback(DELAYCHECK_CALLBACK_FINISH);
- }
- }
- }];
- }
- - (void)removeCheckView {
- [self stopPlayAllAnimation];
- [self removeFromSuperview];
- [self.timer setFireDate:[NSDate distantPast]];
- [_timer invalidate];
- _timer = nil;
- }
- - (IBAction)retryCheck:(id)sender {
- if (self.status == DELAYCHECK_TYPE_FAIL) {
- [self changeCheckType:DELAYCHECK_TYPE_PREPARE];
- }
- }
- - (void)prepareAnimation {
- self.status = DELAYCHECK_TYPE_PREPARE;
-
- self.ligntAniView = [self configAnimation:@"delay_check_ light" attachInView:self.lightBgView];
- [self.ligntAniView play];
-
- self.sandbeachAniView = [self configAnimation:@"delay_check_sandbeach" attachInView:self.sandbeachAniBgView];
- [self.sandbeachAniView play];
-
- self.seaGrassLeftAniView = [self configAnimation:@"delay_check_seaGrass_left" attachInView:self.seaGrassLeft];
- [self.seaGrassLeftAniView play];
-
- self.seaGrassRightAniView = [self configAnimation:@"delay_check_seaGrass_right" attachInView:self.seaGrassRight];
- [self.seaGrassRightAniView play];
-
- self.bubbleAniView = [self configAnimation:@"delay_check_ bubble" attachInView:self.bubbleBgView];
- [self.bubbleAniView play];
- [self addMoveAnimation];
- }
- - (LOTAnimationView *)configAnimation:(NSString *)aniName attachInView:(UIView *)view {
- LOTAnimationView *aniView = [LOTAnimationView animationWithFilePath:[[NSBundle mainBundle] pathForResource:aniName ofType:@"json"]];
- aniView.loopAnimation = YES;
- aniView.contentMode = UIViewContentModeScaleAspectFill;
- aniView.animationSpeed = 1.0f;
- aniView.userInteractionEnabled = YES;
- [view addSubview:aniView];
- [aniView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.right.top.bottom.mas_equalTo(view);
- }];
- return aniView;
- }
- - (void)setStatus:(DELAYCHECK_TYPE)status {
- _status = status;
- switch (status) {
- case DELAYCHECK_TYPE_PREPARE:
- {
- self.hasSendStart = NO;
- self.retryButton.hidden = YES;
- [self.tipsImage setImage:[UIImage imageNamed:@"check_prepareTips"]];
- [self.shipView setImage:[UIImage imageNamed:@"delayCheck_ship_prepare"]];
- if (self.isHeadsetOff == NO) {
- [self startTimer];
- }
- [self startDelayCheck];
-
- }
- break;
- case DELAYCHECK_TYPE_ING:
- {
- self.retryButton.hidden = YES;
- [self.tipsImage setImage:[UIImage imageNamed:@"check_ingTips"]];
- [self hideTips];
- [self.shipView setImage:[UIImage imageNamed:@"delayCheck_ship_checking"]];
- }
- break;
- case DELAYCHECK_TYPE_SUCCESS:
- {
- self.retryButton.hidden = YES;
- [self.tipsImage setImage:[UIImage imageNamed:@"check_finishTips"]];
- [self.shipView setImage:[UIImage imageNamed:@"delayCheck_ship_success"]];
-
- @weakObj(self);
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- @strongObj(self);
- [self finishAction];
- });
- [self hideTips];
- }
- break;
- case DELAYCHECK_TYPE_FAIL:
- {
- self.retryButton.hidden = NO;
- [self.tipsImage setImage:[UIImage imageNamed:@"check_failedTips"]];
- [self.shipView setImage:[UIImage imageNamed:@"delayCheck_ship_failer"]];
- [self hideTips];
- }
- break;
- default:
- break;
- }
- }
- - (IBAction)tipsCloseAction:(id)sender {
- [self hideTips];
- }
- - (void)hideTips {
- self.otherTipsView.hidden = YES;
- }
- #pragma mark ----- setter
- - (void)setIsSocketConnected:(BOOL)isSocketConnected {
- _isSocketConnected = isSocketConnected;
- [self startDelayCheck];
- }
- - (void)setIsHeadsetOff:(BOOL)isHeadsetOff {
- _isHeadsetOff = isHeadsetOff;
- if (isHeadsetOff) {
- [self setCurrentVolume];
- }
- if (self.status == DELAYCHECK_TYPE_ING && isHeadsetOff == NO) {
- if (self.callback) {
- self.callback(DELAYCHECK_CALLBACK_FAILED);
- }
- }
- else if (self.status == DELAYCHECK_TYPE_PREPARE) {
- if (isHeadsetOff == NO) {
- [self startTimer];
- }
- else if (isHeadsetOff == YES) {
- [self stopTimer];
- [self hideTips];
- }
- }
- [self startDelayCheck];
- }
- - (void)startDelayCheck {
- NSLog(@"-----isHeadsetOff :%d, isSocketConnected : %d",_isHeadsetOff, _isSocketConnected);
- if (self.isHeadsetOff && self.isSocketConnected) {
- // 直接开始下一步
- if (self.status == DELAYCHECK_TYPE_PREPARE && self.hasSendStart == NO) {
- self.hasSendStart = YES;
- @weakObj(self);
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- @strongObj(self);
- if (self.callback) {
- self.callback(DELAYCHECK_CALLBACK_START);
- }
- });
- }
- }
- }
- - (void)checkHeadsetTips {
- if (self.skipFirst == NO) {
- [self stopTimer];
- if (self.status == DELAYCHECK_TYPE_PREPARE) {
- self.otherTipsView.hidden = NO;
- }
- }
- self.skipFirst = NO;
- }
- - (void)startTimer {
- self.skipFirst = YES;
- [self.timer setFireDate:[NSDate distantPast]];
- NSLog(@"-------- start timer");
- }
- - (void)stopTimer {
- [self.timer setFireDate:[NSDate distantFuture]];//暂停计时器
- }
- #pragma mark ------ timer
- - (NSTimer *)timer{
-
- if (!_timer) {
- MJWeakSelf;
- _timer = [NSTimer scheduledTimerWithTimeInterval:5.0f repeats:YES block:^(NSTimer * _Nonnull timer) {
- [weakSelf timerAction];
- }];
- [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
- [_timer setFireDate:[NSDate distantFuture]];
- }
- return _timer;
- }
- - (void)timerAction {
- NSLog(@"-------- checkHeadsetTips");
- [self checkHeadsetTips];
- }
- - (void)dealloc {
- NSLog(@"------ delay check View delloc");
- }
- - (float)getCurrentVolume {
- return [[AVAudioSession sharedInstance] outputVolume];
- }
- - (void)addMoveAnimation {
- CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
- animation.toValue= @(-12);
- animation.duration = 1;
- animation.removedOnCompletion = NO;
- animation.fillMode = kCAFillModeForwards; // 保持动画完成后的状态
- animation.repeatCount = CGFLOAT_MAX;
- animation.autoreverses = YES; // 动画完成后反向执行
- animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
- [self.shipView.layer addAnimation:animation forKey:@"translationAnimation"];
- }
- - (void)removeMoveAnimation {
- [self.shipView.layer removeAllAnimations];
- }
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect {
- // Drawing code
- }
- */
- @end
|