// // GHRefreshHeader.m // GameHelper // // Created by 青秀斌 on 16/12/29. // Copyright © 2016年 kylincc. All rights reserved. // #import "GHRefreshHeader.h" #import "UILabel+GHConfig.h" #import "UIView+GHTheme.h" #import "XLDotLoading.h" @interface GHRefreshHeader() @property (strong, nonatomic) UIImageView *loadingView; @property (strong, nonatomic) UILabel *stateLabel; @property (nonatomic, assign) CGFloat anmitationRotate; @property (nonatomic, assign) BOOL isAnimation; @end @implementation GHRefreshHeader - (void)dealloc{ [self unObserveThemeChange]; } - (instancetype)init { self = [super init]; self.textForIdle = @"下拉即可刷新..."; self.textForPulling = @"释放即可刷新..."; self.textForRefreshing = @"正在刷新..."; self.backgroundColor = [UIColor clearColor]; return self; } + (instancetype)headerWithRefreshingBlock:(MJRefreshComponentRefreshingBlock)refreshingBlock carRefreshBackgroundType:(CarRefreshBackgroundType)carRefreshBackgroundType { GHRefreshHeader *cmp = [super headerWithRefreshingBlock:refreshingBlock]; switch (carRefreshBackgroundType) { case kCarRefreshBackgroundTypeBlack:{ cmp.textForIdle = @""; cmp.textForPulling = @""; cmp.textForRefreshing = @""; [cmp addSubview:cmp.carRefreshHeadView]; [cmp.carRefreshHeadView startAnimationCarRefreshBackgroundType:kCarRefreshBackgroundTypeBlack]; }break; case kCarRefreshBackgroundTypeWhite:{ cmp.textForIdle = @""; cmp.textForPulling = @""; cmp.textForRefreshing = @""; [cmp addSubview:cmp.carRefreshHeadView]; [cmp.carRefreshHeadView startAnimationCarRefreshBackgroundType:kCarRefreshBackgroundTypeWhite]; }break; case kCarRefreshBackgroundTypeDoubleCircle:{ cmp.textForIdle = @""; cmp.textForPulling = @""; cmp.textForRefreshing = @""; cmp.width = kScreenWidth; cmp.height = cmp.height + 20; [XLDotLoading showInView:cmp]; }break; default:{ }break; } return cmp; } - (CarRefreshHeadView *)carRefreshHeadView{ if(!_carRefreshHeadView){ CarRefreshHeadView *carRefreshHeadView = [[[NSBundle mainBundle] loadNibNamed:@"CarRefreshHeadView" owner:nil options:nil] objectAtIndex:0]; _carRefreshHeadView = carRefreshHeadView; } return _carRefreshHeadView; } /**********************************************************************/ #pragma mark - OverWrite /**********************************************************************/ - (void)themeChangeAction{ self.loadingView.image = [UIImage imageNamed:@"下拉刷新"]; self.stateLabel.configText = @"T1"; } /**********************************************************************/ #pragma mark - Private /**********************************************************************/ - (void)startAnimation { if (self.isAnimation) { return; } if (self.state == MJRefreshStateRefreshing) { self.isAnimation = YES; __weak __typeof(self) weakSelf = self; CGAffineTransform angle = CGAffineTransformMakeRotation(fabs(self.anmitationRotate) *(M_PI)/45); [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{ weakSelf.loadingView.transform = angle; } completion:^(BOOL finished) { weakSelf.anmitationRotate -= 30; weakSelf.isAnimation = NO; [weakSelf startAnimation]; }]; } } /**********************************************************************/ #pragma mark - OverWrite /**********************************************************************/ - (void)prepare { [super prepare]; self.mj_h = 50; self.automaticallyChangeAlpha = YES; if (self.loadingView == nil) { self.loadingView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"下拉刷新"]]; [self addSubview:self.loadingView]; } if (self.stateLabel == nil) { self.stateLabel = [[UILabel alloc] init]; self.stateLabel.textAlignment = NSTextAlignmentCenter; self.stateLabel.font = [UIFont boldSystemFontOfSize:12]; self.stateLabel.backgroundColor = [UIColor clearColor]; [self addSubview:self.stateLabel]; } [self themeChangeAction]; [self observeThemeChange]; } - (void)placeSubviews { [super placeSubviews]; CGRect frame = self.bounds; frame.origin.x = 20; frame.size.width -= frame.origin.x; self.loadingView.center = CGPointMake(self.mj_w * 0.5 - 50, self.mj_h * 0.5); self.stateLabel.frame = frame; self.carRefreshHeadView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.height); } - (void)scrollViewContentOffsetDidChange:(NSDictionary *)change{ [super scrollViewContentOffsetDidChange:change]; if (self.scrollView.isDragging && self.state != MJRefreshStateRefreshing) { self.anmitationRotate = self.scrollView.mj_offsetY; CGAffineTransform angle = CGAffineTransformMakeRotation(fabs(self.anmitationRotate) *(M_PI)/45); self.loadingView.transform = angle; } } - (void)setState:(MJRefreshState)state { MJRefreshCheckState switch (state) { case MJRefreshStateIdle:{ self.stateLabel.text = self.textForIdle; }break; case MJRefreshStatePulling:{ self.stateLabel.text = self.textForPulling; }break; case MJRefreshStateRefreshing:{ self.stateLabel.text = self.textForRefreshing; [self startAnimation]; }break; default:break; } } @end