// // TFPromptView.m // TFReader // // Created by 谢腾飞 on 2020/12/10. // Copyright © 2020 xtfei_2011@126.com. All rights reserved. // #import "TFPromptView.h" @interface TFPromptView () @property (nonatomic ,strong) UIView *promptBottonView; @property (nonatomic ,strong) UILabel *promptTitleLabel; @property (nonatomic ,strong) UIImageView *promptImageView; @property (nonatomic ,strong) UIActivityIndicatorView *indicatorView; // 下滑返回手势 @property (nonatomic ,strong) UISwipeGestureRecognizer *recognizer; @end @implementation TFPromptView - (instancetype)init { if (self = [super init]) { self.userInteractionEnabled = YES; self.backgroundColor = kColorRGBA(0, 0, 0, 0.0); self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); [kMainWindow addSubview:self]; [self createSubViews]; self.recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(recognizerHandle:)]; [self.recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)]; [self addGestureRecognizer:self.recognizer]; } return self; } - (void)createSubViews { self.promptBottonView.frame = CGRectMake(0, - PUB_NAVBAR_HEIGHT, SCREEN_WIDTH, PUB_NAVBAR_HEIGHT); [self addSubview:self.promptBottonView]; [self.promptBottonView addSubview:self.promptImageView]; [self.promptImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(kMargin); make.bottom.mas_equalTo(- kMargin); make.width.mas_equalTo(20); make.height.mas_equalTo(20); }]; [self.promptBottonView addSubview:self.promptTitleLabel]; [self.promptTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.promptImageView.mas_right).with.offset(kHalfMargin); make.centerY.mas_equalTo(self.promptImageView.mas_centerY); make.right.mas_equalTo(self.mas_right).with.offset(- kHalfMargin); make.height.mas_equalTo(30); }]; self.indicatorView.hidden = YES; [self.promptBottonView addSubview:self.indicatorView]; [self.indicatorView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(kMargin); make.bottom.mas_equalTo(- kMargin); make.width.mas_equalTo(20); make.height.mas_equalTo(20); }]; } // 处理手势 - (void)recognizerHandle:(UISwipeGestureRecognizer *)recognizer { if(recognizer.direction == UISwipeGestureRecognizerDirectionUp) { [self hiddenPromptView:0]; } } - (void)showPromptView { [UIView animateWithDuration:kAnimatedDuration delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:0.0 options:UIViewAnimationOptionShowHideTransitionViews animations:^{ self.promptBottonView.frame = CGRectMake(0, 0, SCREEN_WIDTH, PUB_NAVBAR_HEIGHT); if (self.showMask) { self.backgroundColor = kBlackTransparentColor; } } completion:^(BOOL finished) { WS(weakSelf) self.isShowing = YES; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.alertDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [weakSelf hiddenPromptView]; }); }]; } - (void)hiddenPromptView { [self hiddenPromptView:1]; } - (void)hiddenPromptView:(CGFloat)delay { dispatch_async(dispatch_get_main_queue(), ^{ [UIView animateWithDuration:kAnimatedDuration delay:delay usingSpringWithDamping:0.8 initialSpringVelocity:0.0 options:UIViewAnimationOptionShowHideTransitionViews animations:^{ self.promptBottonView.frame = CGRectMake(0, - PUB_NAVBAR_HEIGHT, SCREEN_WIDTH, PUB_NAVBAR_HEIGHT); if (self.showMask) { self.backgroundColor = kColorRGBA(0, 0, 0, 0.01); } } completion:^(BOOL finished) { [self removePromptView]; self.isShowing = NO; }]; }); } - (void)removePromptView { if (self.alertDissmissBlock) { self.alertDissmissBlock(); } [self removeAllSubviews]; [self removeFromSuperview]; } - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { UIView *hitView = [super hitTest:point withEvent:event]; if (hitView == self && !self.showMask) { return nil; } return hitView; } - (void)setPromptTitle:(NSString *)promptTitle { _promptTitle = promptTitle; self.promptTitleLabel.text = [TFUtilsHelper formatStringWithObject:promptTitle]; } - (void)setPromptStatus:(TFPromptStatus)promptStatus { if (promptStatus == TFPromptStatusSuccess) { self.promptImageView.image = [UIImage imageNamed:@"tips_success"]; } if (promptStatus == TFPromptStatusError) { self.promptImageView.image = [UIImage imageNamed:@"tips_error"]; } if (promptStatus == TFPromptStatusLoading) { self.indicatorView.hidden = NO; [self.indicatorView startAnimating]; self.showMask = YES; } } - (UIView *)promptBottonView { if (!_promptBottonView) { _promptBottonView = [[UIView alloc] init]; _promptBottonView.backgroundColor = kWhiteColor; } return _promptBottonView; } - (UIImageView *)promptImageView { if (!_promptImageView) { _promptImageView = [[UIImageView alloc] init]; } return _promptImageView; } - (UILabel *)promptTitleLabel { if (!_promptTitleLabel) { _promptTitleLabel = [[UILabel alloc] init]; _promptTitleLabel.textAlignment = NSTextAlignmentLeft; _promptTitleLabel.textColor = kBlackColor; _promptTitleLabel.font = kMainFont; } return _promptTitleLabel; } - (UIActivityIndicatorView *)indicatorView { if (!_indicatorView) { _indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleGray)]; _indicatorView.color = kBlackColor; _indicatorView.hidesWhenStopped = YES; } return _indicatorView; } @end