標籤:
下拉重新整理對象RefreshObject
效果
說明
1. 分離了動畫實現與重新整理邏輯
2. 你可以根據自己的需要,設計自己的動畫效果,你的動畫只需要繼承協議,實現協議裡面的方法即可
3. 本設計方案是用的組件方式,代碼複用率很高,靈活性很強
源碼
https://github.com/YouXianMing/RefreshObject
//// RefreshObjectAnimationProtocal.h// TableViewRefresh//// Created by YouXianMing on 15/6/25.// Copyright (c) 2015年 YouXianMing. All rights reserved.//#import <Foundation/Foundation.h>@protocol RefreshObjectAnimationProtocal <NSObject>@required- (void)animationWithPercent:(CGFloat)percent;- (void)startRefreshAnimation;- (void)endRefreshAnimation;@end
//// RefreshObject.h// UIScrollView//// Created by YouXianMing on 15/6/24.// Copyright (c) 2015年 YouXianMing. All rights reserved.//#import <Foundation/Foundation.h>#import <UIKit/UIKit.h>@class RefreshObject;typedef enum : NSUInteger { NORMAL_STATE, // 正常狀態 REFRESH_STATE, // 重新整理狀態 } ERefreshState;@protocol RefreshObjectDelegate <NSObject>@required/** * 開始重新整理 * * @param refreshObject */- (void)startRefreshing:(RefreshObject *)refreshObject;/** * 結束重新整理 * * @param refreshObject */- (void)endRefresh:(RefreshObject *)refreshObject;- (void)moving:(RefreshObject *)refreshObject offset:(CGFloat)offset percent:(CGFloat)percent;@end@interface RefreshObject : NSObject/** * 代理 */@property (nonatomic, weak) id <RefreshObjectDelegate> delegate;/** * 目前狀態 */@property (nonatomic, readonly) ERefreshState state;@property (nonatomic) CGFloat height;@property (nonatomic, weak) UIScrollView *scrollView;/** * === 子類可以重寫該方法實現新的重新整理效果 === * * 開始重新整理 */- (void)beginRefreshing;/** * === 子類可以重寫該方法實現新的重新整理效果 === * * 結束重新整理 */- (void)endRefresh;@end
//// RefreshObject.m// UIScrollView//// Created by YouXianMing on 15/6/24.// Copyright (c) 2015年 YouXianMing. All rights reserved.//#import "RefreshObject.h"@interface RefreshObject ()@end@implementation RefreshObject- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { // 當前位置 float currentPostion = _scrollView.contentOffset.y; if (_scrollView.isDragging) { // 拖拽中 if (_state == NORMAL_STATE) { // 擷取位移的資訊 if (_delegate) { CGFloat percent = 0; if (currentPostion <= 0) { percent = -currentPostion / _height; } [_delegate moving:self offset:currentPostion percent:percent]; } } } else { // 停止拖拽 if (currentPostion < -_height) { [self beginRefreshing]; } } }- (void)beginRefreshing { if (_state == NORMAL_STATE) { _state = REFRESH_STATE; if (_delegate) { [_delegate startRefreshing:self]; } [UIView animateWithDuration:0.3 animations:^{ _scrollView.contentInset = UIEdgeInsetsMake(_height, 0, 0, 0); } completion:^(BOOL finished) { }]; }}- (void)endRefresh { if (_delegate) { [_delegate endRefresh:self]; } [UIView animateWithDuration:0.3f animations:^{ _scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0); } completion:^(BOOL finished) { _state = NORMAL_STATE; }];}@end
//// UIScrollView+RefreshObject.h// UIScrollView//// Created by YouXianMing on 15/6/24.// Copyright (c) 2015年 YouXianMing. All rights reserved.//#import <UIKit/UIKit.h>#import "RefreshObject.h"@interface UIScrollView (RefreshObject)/** * 需要主動賦值 */@property (nonatomic, strong) RefreshObject *refreshObject;/** * 添加觀察者 */- (void)addObserver;/** * 移除觀察者 */- (void)removeObserver;@end
//// UIScrollView+RefreshObject.m// UIScrollView//// Created by YouXianMing on 15/6/24.// Copyright (c) 2015年 YouXianMing. All rights reserved.//#import "UIScrollView+RefreshObject.h"#import <objc/runtime.h>@implementation UIScrollView (RefreshObject)#pragma mark - 添加屬性@dynamic refreshObject;NSString * const _recognizerRefreshObject = @"recognizerRefreshObject";- (void)setRefreshObject:(RefreshObject *)refreshObject { objc_setAssociatedObject(self, (__bridge const void *)(_recognizerRefreshObject), refreshObject, OBJC_ASSOCIATION_RETAIN_NONATOMIC);}- (RefreshObject *)refreshObject { return objc_getAssociatedObject(self, (__bridge const void *)(_recognizerRefreshObject));}#pragma mark -- (void)addObserver { if (self.refreshObject && self.refreshObject.scrollView == nil) { // 擷取scrollView self.refreshObject.scrollView = self; // 添加監聽 [self addObserver:self.refreshObject forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil]; }}- (void)removeObserver { if (self.refreshObject) { // 移除監聽 [self removeObserver:self.refreshObject forKeyPath:@"contentOffset"]; self.refreshObject.scrollView = nil; self.refreshObject = nil; }}@end
細節
繼承協議 RefreshObjectAnimationProtocal 並實現協議方法即可,使用的話,如下所示
下拉重新整理對象RefreshObject