MJRefreshRighter
由于產(chǎn)品設計了一個可橫向滾動狱窘,在滾動末還要顯示加載更多的設計,看了MJRefresh沒有支持這種格式的财搁,昨天研究一番MJRefresh的源碼后依葫蘆畫瓢的寫了一個支持水平滑動加載更多的刷新組件,組件依賴于MJRefresh
MJRefreshRighter.h
@interface MJRefreshRighter : MJRefreshComponent
/** 忽略多少scrollView的contentInset的right */
@property (assign, nonatomic) CGFloat ignoredScrollViewContentInsetRight;
+ (instancetype)righterWithRefreshingBlock:(MJRefreshComponentRefreshingBlock)refreshingBlock;
/** 創(chuàng)建righter */
+ (instancetype)righterWithRefreshingTarget:(id)target refreshingAction:(SEL)action;
/** 提示沒有更多的數(shù)據(jù) */
- (void)endRefreshingWithNoMoreData;
@end
MJRefreshRighter.m
#import "MJRefreshRighter.h"
@interface MJRefreshRighter()
@property (assign, nonatomic) NSInteger lastRefreshCount;
@property (assign, nonatomic) CGFloat lastRightDelta;
@property (nonatomic,strong) UILabel *statelbl;
@property (nonatomic,strong) UIImageView *arrowImageView;
@property (nonatomic,strong) UIActivityIndicatorView * indicator;
@property (nonatomic,strong) NSMutableDictionary * stateLblTextDictM;
@end
@implementation MJRefreshRighter
#pragma mark - 構造方法
+ (instancetype)righterWithRefreshingBlock:(MJRefreshComponentRefreshingBlock)refreshingBlock
{
MJRefreshRighter *cmp = [[self alloc] init];
cmp.refreshingBlock = refreshingBlock;
return cmp;
}
+ (instancetype)righterWithRefreshingTarget:(id)target refreshingAction:(SEL)action
{
MJRefreshRighter *cmp = [[self alloc] init];
[cmp setRefreshingTarget:target refreshingAction:action];
return cmp;
}
#pragma mark - 初始化
- (void)willMoveToSuperview:(UIView *)newSuperview
{
[super willMoveToSuperview:newSuperview];
if (newSuperview) {
// 監(jiān)聽scrollView數(shù)據(jù)的變化
if ([self.scrollView isKindOfClass:[UITableView class]] || [self.scrollView isKindOfClass:[UICollectionView class]]) {
[self.scrollView setMj_reloadDataBlock:^(NSInteger totalDataCount) {
self.hidden = (totalDataCount == 0);
}];
}
}
[self addSubview:self.statelbl];
// 設置文字
[self updateStateAttrTextWithState:self.state];
[self addSubview:self.arrowImageView];
[self addSubview:self.indicator];
self.scrollView.alwaysBounceVertical = NO;
self.scrollView.alwaysBounceHorizontal = YES;
[self scrollViewContentSizeDidChange:nil];
}
#pragma mark - 實現(xiàn)父類的方法
- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change
{
[super scrollViewContentOffsetDidChange:change];
// 如果正在刷新躬络,直接返回
if (self.state == MJRefreshStateRefreshing) return;
_scrollViewOriginalInset = self.scrollView.contentInset;
// 當前的contentOffset
CGFloat currentOffsetX = self.scrollView.mj_offsetX;
// 尾部控件剛好出現(xiàn)的offsetX
CGFloat happenOffsetX = [self happenOffsetX];
// 如果是向右滾動到看不見尾部控件尖奔,直接返回
if (currentOffsetX <= happenOffsetX) return;
CGFloat pullingPercent = (currentOffsetX - happenOffsetX) / self.mj_w;
// 如果已全部加載,僅設置pullingPercent穷当,然后返回
if (self.state == MJRefreshStateNoMoreData) {
self.pullingPercent = pullingPercent;
return;
}
CGFloat normal2pullingOffsetX = happenOffsetX + self.mj_w;
if (self.scrollView.isDragging) { // 拖拽過程中的處理
self.pullingPercent = pullingPercent;
// 普通 和 即將刷新 的臨界點提茁,臨界點位happenOffsetX剛好可見,happenOffsetX + self.mj_w全部可見馁菜,當全部可見時就為臨界點
// 超過臨界點
if (self.state == MJRefreshStateIdle && currentOffsetX > normal2pullingOffsetX) {
// 轉(zhuǎn)為即將刷新狀態(tài)
self.state = MJRefreshStatePulling;
} else if (self.state == MJRefreshStatePulling && currentOffsetX <= normal2pullingOffsetX) {
// 轉(zhuǎn)為普通狀態(tài)
self.state = MJRefreshStateIdle;
}
}else if (self.state == MJRefreshStatePulling) {// 即將刷新 && 手松開
// 開始刷新
[self beginRefreshing];
} else if (pullingPercent < 1) {
self.pullingPercent = pullingPercent;
}
}
- (void)scrollViewContentSizeDidChange:(NSDictionary *)change
{
[super scrollViewContentSizeDidChange:change];
// 內(nèi)容的寬度度
CGFloat contentWidth = self.scrollView.mj_contentW + self.ignoredScrollViewContentInsetRight;
// 表格的高度
CGFloat scrollWidth = self.scrollView.mj_w - self.scrollViewOriginalInset.left - self.scrollViewOriginalInset.right + self.ignoredScrollViewContentInsetRight;
// 設置位置和尺寸
self.mj_x = MAX(contentWidth, scrollWidth);
self.mj_y = 0;
self.mj_h = self.scrollView.mj_h;
self.arrowImageView.mj_x = 0;
self.arrowImageView.mj_y = (self.mj_h-self.arrowImageView.image.size.height)/2.0;
self.statelbl.mj_x = self.arrowImageView.mj_w+5;
self.statelbl.mj_h = self.mj_h;
self.statelbl.mj_w = 20;
self.indicator.frame = self.arrowImageView.frame;
self.mj_w = self.arrowImageView.mj_w+5+self.statelbl.mj_w+5;
}
- (void)setState:(MJRefreshState)state
{
MJRefreshCheckState
// 根據(jù)狀態(tài)來設置屬性
if (state == MJRefreshStateNoMoreData || state == MJRefreshStateIdle) {
// 刷新完畢
if (MJRefreshStateRefreshing == oldState) {
[UIView animateWithDuration:MJRefreshSlowAnimationDuration animations:^{
self.scrollView.mj_insetR -= self.lastRightDelta;
// 自動調(diào)整透明度
if (self.isAutomaticallyChangeAlpha) self.alpha = 0.0;
} completion:^(BOOL finished) {
self.pullingPercent = 0.0;
if (self.endRefreshingCompletionBlock) {
self.endRefreshingCompletionBlock();
}
}];
}
self.indicator.hidden = YES;
[self.indicator stopAnimating];
self.arrowImageView.hidden = NO;
if (state == MJRefreshStateNoMoreData) {
self.arrowImageView.hidden = YES;
}else{
self.arrowImageView.hidden = NO;
}
[self updateStateAttrTextWithState:state];
if (oldState == MJRefreshStateRefreshing) {
self.arrowImageView.transform = CGAffineTransformMakeRotation(0.000001 - M_PI);
}else{
[UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
self.arrowImageView.transform = CGAffineTransformMakeRotation(0.000001 - M_PI);
}];
}
CGFloat deltaW = [self widthForContentBreakView];
// 剛刷新完畢
if (MJRefreshStateRefreshing == oldState && deltaW > 0 && self.scrollView.mj_totalDataCount != self.lastRefreshCount) {
self.scrollView.mj_offsetX = self.scrollView.mj_offsetX;
}
} else if (state == MJRefreshStateRefreshing) {
[self updateStateAttrTextWithState:state];
self.indicator.hidden = NO;
[self.indicator startAnimating];
self.arrowImageView.hidden = YES;
// 記錄刷新前的數(shù)量
self.lastRefreshCount = self.scrollView.mj_totalDataCount;
// MJRefreshFastAnimationDuration
[UIView animateWithDuration:0.1 animations:^{
CGFloat right = self.mj_w + self.scrollViewOriginalInset.right;
CGFloat deltaW = [self widthForContentBreakView];
if (deltaW < 0) { // 如果內(nèi)容寬度小于view的寬度
right -= deltaW;
}
self.lastRightDelta = right - self.scrollView.mj_insetR;
self.scrollView.mj_insetR = right;
self.scrollView.mj_offsetX = [self happenOffsetX] + self.mj_w;
} completion:^(BOOL finished) {
[self executeRefreshingCallback];
}];
}else if (state == MJRefreshStatePulling) {
// 記錄刷新前的數(shù)量
[self updateStateAttrTextWithState:state];
self.indicator.hidden = YES;
[self.indicator stopAnimating];
self.arrowImageView.hidden = NO;
[UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
self.arrowImageView.transform = CGAffineTransformIdentity;
}];
}
}
- (void)endRefreshing
{
dispatch_async(dispatch_get_main_queue(), ^{
self.state = MJRefreshStateIdle;
});
}
- (void)endRefreshingWithNoMoreData
{
dispatch_async(dispatch_get_main_queue(), ^{
self.state = MJRefreshStateNoMoreData;
});
}
#pragma mark - 私有方法
#pragma mark 獲得scrollView的內(nèi)容 超出 view 的寬度
- (CGFloat)widthForContentBreakView
{
// 一屏內(nèi)可真實顯示內(nèi)容的寬度茴扁,也就是減去前后的padding
CGFloat w = self.scrollView.frame.size.width - self.scrollViewOriginalInset.left - self.scrollViewOriginalInset.right;
return self.scrollView.contentSize.width - w;
}
#pragma mark 剛好看到上拉刷新控件時的contentOffset.x
- (CGFloat)happenOffsetX
{
// 算出從初始化時要滾動多久才能看見刷新控件
// 全部內(nèi)容區(qū)域-顯示屏區(qū)域
CGFloat deltaW = [self widthForContentBreakView];
// 顯示內(nèi)容超過一屏
if (deltaW > 0) {
return deltaW - self.scrollViewOriginalInset.left;
} else {
// 顯示內(nèi)容不足一屏
return - self.scrollViewOriginalInset.left;
}
}
#pragma mark - getter & setter
- (UILabel *)statelbl
{
if (_statelbl == nil) {
_statelbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 40)];
_statelbl.font = [UIFont systemFontOfSize:10];
_statelbl.textAlignment = NSTextAlignmentCenter;
_statelbl.numberOfLines = 0;
}
return _statelbl;
}
- (void)updateStateAttrTextWithState:(MJRefreshState)state
{
NSString *text = self.stateLblTextDictM[@(state)];
NSRange range = NSRangeFromString(text);
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:text];
[attr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:range];
[attr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"929292"] range:range];
[LYUtil addLineSpaceWithMutableAttbuteString:attr lineSpace:3];
self.statelbl.attributedText = attr;
}
- (UIImageView *)arrowImageView
{
if (_arrowImageView == nil) {
_arrowImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"back_circle_arrow"]];
}
return _arrowImageView;
}
- (UIActivityIndicatorView *)indicator
{
if (_indicator == nil) {
_indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
_indicator.hidden = YES;
[_indicator stopAnimating];
}
return _indicator;
}
- (NSMutableDictionary *)stateLblTextDictM
{
if (_stateLblTextDictM == nil) {
_stateLblTextDictM = [NSMutableDictionary dictionary];
_stateLblTextDictM[@(MJRefreshStateIdle)] = @"左滑加載更多";
_stateLblTextDictM[@(MJRefreshStatePulling)] = @"釋放加載更多";
_stateLblTextDictM[@(MJRefreshStateRefreshing)] = @"正在加載";
_stateLblTextDictM[@(MJRefreshStateWillRefresh)] = @"即將加載更多";
_stateLblTextDictM[@(MJRefreshStateNoMoreData)] = @"全部加載完畢";
}
return _stateLblTextDictM;
}
@end
用分類的方式讓UIScrollView有righter
UIScrollView+ZQQMJRefresh.h
#import <UIKit/UIKit.h>
#import "MJRefreshConst.h"
#import "MJRefreshRighter.h"
@interface UIScrollView (ZQQMJRefresh)
/** 右拉刷新控件 */
@property (strong, nonatomic) MJRefreshRighter *mj_righter;
@end
UIScrollView+ZQQMJRefresh.m
#import "UIScrollView+ZQQMJRefresh.h"
@implementation UIScrollView (ZQQMJRefresh)
#pragma mark - righter
static const char LYMJRefreshRighterKey = '\0';
- (void)setMj_righter:(MJRefreshRighter *)mj_righter
{
if (mj_righter != self.mj_righter) {
// 刪除舊的,添加新的
[self.mj_righter removeFromSuperview];
[self insertSubview:mj_righter atIndex:0];
// 存儲新的
[self willChangeValueForKey:@"mj_righter"]; // KVO
objc_setAssociatedObject(self, &LYMJRefreshRighterKey, mj_righter, OBJC_ASSOCIATION_ASSIGN);
[self didChangeValueForKey:@"mj_righter"];
}
}
- (MJRefreshRighter *)mj_righter
{
return objc_getAssociatedObject(self, &LYMJRefreshRighterKey);
}
@end
總結(jié):通過對你中狀態(tài)的修改進行不同的顯示
UIScrollView.contentinSet汪疮、contentOffset以及對各個距離長度的計算和以狀態(tài)變化為數(shù)據(jù)流管理