123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- //
- // UIView+SPSBLoadingTipsView.m
- // 我的社保
- //
- // Created by shanp on 2021/4/21.
- //
- #import "UIView+SPSBLoadingTipsView.h"
- #import <objc/runtime.h>
- #import <JXHSystemShortcut.h>
- #import "SPSBUIGeneralHeader.h"
- #import <CALayer+JXHShadow.h>
- static UIView *spsb_loadingToast = nil;
- static bool spsb_isLoading = false;
- @interface UIView ()
- @property UIView *spsb_toast;
- @end
- @implementation UIView (SPSBLoadingTipsView)
- #pragma mark - Loading
- - (void)showLoadingToastWithTitle:(NSString *)title userClick:(bool)userClick {
- jxh_windowUserInteractionEnabled(userClick);
- [self _spsb_createLoadingToastWithTitle:title];
- }
- #pragma mark - Normal
- - (void)showToastWithTitle:(NSString *)title {
- [self showToastWithTitle:title duration:SPSBDismissToastDefaultTime];
- }
- - (void)showToastWithTitle:(NSString *)title duration:(NSTimeInterval)duration {
- [self showToastWithTitle:title duration:duration completion:nil];
- }
- - (void)showToastWithTitle:(NSString *)title duration:(NSTimeInterval)duration completion:(void (^ _Nullable)(void))completion {
- [self dismissToast];
-
- UIView *toast = self.spsb_toast = UIView.new;
- toast.alpha = 0;
- [toast setLayerCornerRadius:4 clipToBounds:false];
- toast.backgroundColor = spsb_000000_color(0.8);
- toast.alpha = 0.9;
- UIWindow *window = spsb_keyWindow();
- if (!window) {
- return;
- }
- [window addSubview:toast];
- [toast makeConstraints:^(JXHConstraintMaker *make) {
- make.width.lessThanOrEqualTo(jxh_screenWidth() - 80);
- make.center.equalTo(window);
- }];
-
- UILabel *titleLabel = [UILabel convenienceWithFont:spsb_font(16) textColor:spsb_FFFFFF_color(1.f) limitWidth:jxh_screenWidth() - 80 - 48];
- [titleLabel setAttributedStringWithText:title textAlignment:NSTextAlignmentCenter lineSpacing:5 lineHeight:0 paragraphSpacing:0 lineBreakMode:0];
- [toast addSubview:titleLabel];
- [titleLabel makeConstraints:^(JXHConstraintMaker *make) {
- make.edges.equalTo((UIEdgeInsets){12, 24, 12, 24});
- }];
-
- [toast setNeedsLayout];
- [toast layoutIfNeeded];
-
- CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
- animation.fromValue = [NSNumber numberWithFloat:1.8];
- animation.toValue = [NSNumber numberWithFloat:1.0];
- animation.duration = 0.2;
- animation.removedOnCompletion = true;
- [toast.layer addAnimation:animation forKey:nil];
- [UIView animateWithDuration:0.2 animations:^{
- toast.alpha = 1.f;
- }];
-
- jxh_delayAction(duration, dispatch_get_main_queue(), ^{
- [self _spsb_dismissToast:toast completion:completion];
- });
- }
- - (void)_spsb_dismissToast:(UIView *)toast completion:(void(^_Nullable)(void))completion {
- if (!toast) return;
- CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
- animation.fromValue = [NSNumber numberWithFloat:1.0];
- animation.toValue = [NSNumber numberWithFloat:0];
- animation.duration = 0.2;
- animation.removedOnCompletion = true;
- [toast.layer addAnimation:animation forKey:nil];
- [UIView animateWithDuration:0.2 animations:^{
- toast.alpha = 0.f;
- } completion:^(BOOL finished) {
- [toast removeFromSuperview];
- if (completion) {
- completion();
- }
- }];
- }
- #define spsb_loadingImageViewTag 3000
- - (void)_spsb_createLoadingToastWithTitle:(NSString *)title {
- [self _spsb_dismissToast];
-
- UIView *toast = spsb_loadingToast = UIView.new;
- toast.backgroundColor = spsb_000000_color(0.8);
- [toast setLayerCornerRadius:10 clipToBounds:false];
-
- UIWindow *window = spsb_keyWindow();
- if (!window) return;
- [window addSubview:toast];
- [toast makeConstraints:^(JXHConstraintMaker *make) {
- make.width.greaterThanOrEqualTo(110);
- make.width.lessThanOrEqualTo(215);
- make.center.equalTo(window);
- }];
-
- UIView *backView = [[UIView alloc] init];
- [backView setLayerCornerRadius:20 clipToBounds:false];
- [backView setLayerBorderWidth:3 borderColor:spsb_FFFFFF_color(1.f)];
- [toast addSubview:backView];
- [backView makeConstraints:^(JXHConstraintMaker *make) {
- make.top.equalTo(15);
- make.size.equalTo(CGSizeMake(40, 40));
- make.centerX.equalTo(toast);
- }];
-
- UIView *frontView = [[UIView alloc] init];
- [frontView setLayerCornerRadius:20 clipToBounds:false];
- [frontView setLayerBorderWidth:3 borderColor:spsb_3296FB_color()];
- [toast addSubview:frontView];
- [frontView makeConstraints:^(JXHConstraintMaker *make) {
- make.top.equalTo(15);
- make.size.equalTo(CGSizeMake(40, 40));
- make.centerX.equalTo(toast);
- }];
-
- UILabel *titleLabel = [UILabel convenienceWithFont:spsb_font(16) text:title textColor:spsb_FFFFFF_color(1.f) textAlignment:NSTextAlignmentCenter];
- [toast addSubview:titleLabel];
- [titleLabel makeConstraints:^(JXHConstraintMaker *make) {
- make.top.equalTo(backView.bottom).offset(10);
- make.leading.equalTo(10);
- make.trailing.equalTo(-10);
- make.bottom.equalTo(-15);
- }];
- spsb_isLoading = true;
- [self loading:frontView p:0.15];
- }
- - (void)loading:(UIView *)view p:(CGFloat)p {
- if (!spsb_isLoading) {
- return;
- }
- if (p >= 1) {
- p = 0;
- }
- CGFloat begin = M_PI * 2 * p - M_PI * 2 * 0.15;
- CGFloat startA = -M_PI_2 + begin;
- CGFloat endA = -M_PI_2 + M_PI * 2 * p;
- CGRect rect = CGRectMake(0, 0, 40, 40);
- CGPoint center = CGPointMake(rect.size.width / 2, rect.size.height / 2);
- CGFloat radius = rect.size.width / 2;
- UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:true];
- [path addLineToPoint:center];
- CAShapeLayer *maskLayer = [CAShapeLayer layer];
- maskLayer.path = path.CGPath;
- view.layer.mask = maskLayer;
- jxh_delayAction(0.02, dispatch_get_main_queue(), ^{
- [self loading:view p:p + 0.025];
- });
- }
- - (void)_spsb_dismissLoadingToast {
- if (spsb_loadingToast) {
- spsb_isLoading = false;
- spsb_loadingToast.hidden = true;
- [spsb_loadingToast removeFromSuperview];
- spsb_loadingToast = nil;
- }
- }
- #undef spsb_loadingImageViewTag
- - (void)_spsb_dismissToast {
- [self _spsb_dismissLoadingToast];
- if (self.spsb_toast) {
- self.spsb_toast.hidden = true;
- [self.spsb_toast removeFromSuperview];
- self.spsb_toast = nil;
- }
- }
- - (void)dismissToast {
- [self _spsb_dismissToast];
- jxh_windowUserInteractionEnabled(true);
- }
- - (UIView *)spsb_toast {
- UIView *object = objc_getAssociatedObject(self, @selector(setSpsb_toast:));
- return object;
- }
- - (void)setSpsb_toast:(UIView *)spsb_toast {
- objc_setAssociatedObject(self, _cmd, spsb_toast, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- }
- @end
|