一、需求
之前遇到一個需求是,要求在scrollview在上下滑動時,scrollview顯示區域高度變化。向上滑動時——拉高,向下滑動時——恢復。
二、項目中的實現
由于項目中要實現的幾個頁面都用到了自定義的SITableView,剛好就在自定義的SITableView中實現了
1.向外傳遞滑動
有以下兩種方案
- 1)協議 如果是多級或者是跨層的,不好要拿到響應者,同時如果視圖層級改變的話,也需要改變賦值響應者的代碼。可以精準的傳遞事件給需要改變的視圖,也可以自定義滑動距離,雖然實際用處不大。本次實現用的是協議。
還有一種思路是,定義一個BOOL值,標識是否開啟滑動改變傳遞,然后向上查找第一個能響應協議的responder,把它記錄為委托者。
- 2)通知
傳遞數據方便,但不能自定義滑動距離。并且如果多個界面都注冊了的話,接受到通知要進行判斷,判斷要調整大小的視圖是不是在屏幕上。如果頁面復用過程中,導致某個視圖加載完成后,視圖層級中有父視圖和子視圖都能響應通知,會出現問題,雖然出現的可能性不大。
協議的代碼如下:
@class SITableView;
@protocol SITableViewUpDownScrollProtocol <NSObject>
//告訴外部對象,是向上還是向下滑動
- (void)tableView:(SITableView *)tableView updownScroll:(BOOL)isUp;
@optional
// 是否要自定義判斷移動的距離
- (CGFloat)tableViewMinMoveDistance:(SITableView *)tableView;
@end
滑動方向是向上還是向下,應該用枚舉的,偷懶了
2.SITableView中的主要變動
在scrollViewDidScroll :
方法中,判斷contentOffset.y的變化,與前一刻的差值作為上下的依據。
要考慮以下幾個問題:
1.只有當用戶手動滑動時,才改變視圖高度。需要記錄是不是手動拖拽,雖然,scrollview有dragging,但不夠精確,在手松開減速時依然是YES,不符合要求
2.需要記錄初始值,來做參考
3.要移動一定距離,才能判斷是否執行回調,避免有時手觸碰屏幕引起的誤操作
4.攔截的方法,不能影響原方法的調用
- 1.增加私有屬性,協助判斷
//是不是手動移動
@property (nonatomic, assign, getter=isManuallyMoving) BOOL manuallyMoving;
//開始手動移動時contentOffset.y值
@property (nonatomic, assign) CGFloat startOffsetY;
//tableview的新的delegate,用來判斷是否要攔截
@property (nonatomic, strong) SITableViewWeakProxy *weakProxy;
//默認最小移動距離 5
@property (nonatomic, assign) CGFloat minMoveDistance;
- 2.實現
#pragma mark - 上下滑動回調
//調用有參無返回值的方法
- (void)callTableViewUpDownScrollProtocol:(BOOL)isUp {
if (self.upDownScrollDelegate == nil) {
return;
}
// 1. 根據方法創建簽名對象sig
NSMethodSignature *sig = [self.upDownScrollDelegate methodSignatureForSelector:@selector(tableView:updownScroll:)];
// 2. 根據簽名對象創建調用對象invocation
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];
// 3. 設置調用對象的相關信息
invocation.target = self.upDownScrollDelegate;
invocation.selector = @selector(tableView:updownScroll:);
SITableView *tempSelf = self;
// 參數必須從第2個索引開始,因為前兩個已經被target和selector使用
[invocation setArgument:&tempSelf atIndex:2];
[invocation setArgument:&isUp atIndex:3];
// 4. 調用方法
[invocation invoke];
}
#pragma mark - 攔截的協議方法
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
self.manuallyMoving = NO;
//不影響原有的邏輯,回調原來delegate的方法
if ([self.weakProxy.originTarget respondsToSelector:@selector(scrollViewDidEndDragging:willDecelerate:)]) {
[self.weakProxy.originTarget scrollViewDidEndDragging:scrollView willDecelerate:decelerate];
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
self.manuallyMoving = YES;
self.startOffsetY = scrollView.contentOffset.y;
//不影響原有的邏輯,回調原來delegate的方法
if ([self.weakProxy.originTarget respondsToSelector:@selector(scrollViewWillBeginDragging:)]) {
[self.weakProxy.originTarget scrollViewWillBeginDragging:scrollView];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (self.isManuallyMoving) {
if (self.startOffsetY < scrollView.contentOffset.y - self.minMoveDistance) {
[self callTableViewUpDownScrollProtocol:YES];
}
if (self.startOffsetY > scrollView.contentOffset.y + self.minMoveDistance) {
[self callTableViewUpDownScrollProtocol:NO];
}
}
self.startOffsetY = scrollView.contentOffset.y;
//不影響原有的邏輯,回調原來delegate的方法
if ([self.weakProxy.originTarget respondsToSelector:@selector(scrollViewDidScroll:)]) {
[self.weakProxy.originTarget scrollViewDidScroll:scrollView];
}
}
#pragma mark - setter與getter
- (void)setDelegate:(id<UITableViewDelegate>)delegate {
self.weakProxy.originTarget = delegate;
[super setDelegate:self.weakProxy];
}
- (void)setUpDownScrollDelegate:(id<SITableViewUpDownScrollProtocol>)upDownScrollDelegate {
if (upDownScrollDelegate && [upDownScrollDelegate conformsToProtocol:@protocol(SITableViewUpDownScrollProtocol)] && [upDownScrollDelegate respondsToSelector:@selector(tableView:updownScroll:)]) {
_upDownScrollDelegate = upDownScrollDelegate;
if ([upDownScrollDelegate respondsToSelector:@selector(tableViewMinMoveDistance:)]) {
self.minMoveDistance = [upDownScrollDelegate tableViewMinMoveDistance:self];
}
}
if (upDownScrollDelegate == nil) {
_upDownScrollDelegate = upDownScrollDelegate;
}
}
- (SITableViewWeakProxy *)weakProxy {
if (_weakProxy == nil) {
_weakProxy = [SITableViewWeakProxy alloc];
_weakProxy.interceptionTarget = self;
}
return _weakProxy;
}
注意 [SITableViewWeakProxy alloc];
這樣寫沒有錯,它沒有init方法。
3.SITableViewWeakProxy的實現
為什么要做的這樣復雜,
不直接把delegate設為自己,用一個屬性記錄原始的delegate呢?如果這樣做了,tableview的UITableViewDelegate協議中的其他方法呢,怎么把協議中的方法傳遞給原始的delegate呢。實現所有的方法,在里面判斷原始的delegate是否實現了,原始未實現的但方法需要返回值的你怎么操作。如果里面后面新增了方法怎么辦,一個個版本維護更新?
走消息轉發,UITableViewDelegate協議中的很多方法是optional,會調用respondsToSelector來判斷是否協議中某個方法,這個地方的響應者是SITableView的實例,它明顯沒有實現協議中的其他方法,就無法調用了。當然也可以重寫respondsToSelector,但怎么判斷這個sel是UITableViewDelegate協議中的方法,一個個列出來
使用SITableViewWeakProxy,是實例不會在方法列表中查找,而是直接走消息轉發,效率高,也安全,不用擔心其他的影響。包括respondsToSelector方法也是走的消息轉發,所以在具體的實現中,要特殊處理,判斷這個方法的參數,如果是要攔截的三個方法,就要攔截。
@interface SITableViewWeakProxy : NSProxy <UITableViewDelegate>
@property (nonatomic, weak) NSObject<UITableViewDelegate> *originTarget;
@property (nonatomic, weak) NSObject *interceptionTarget;
@end
@implementation SITableViewWeakProxy
//- (id)forwardingTargetForSelector:(SEL)selector {
// NSLog(@"%@...%@", self, NSStringFromSelector(selector));
// for (NSString *interceptionSEL in self.interceptionSELS) {
// if (NSSelectorFromString(interceptionSEL) == selector) {
// return _interceptionTarget;
// }
// }
// return _originTarget;
//}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel {
return [self.originTarget methodSignatureForSelector:sel];
}
- (void)forwardInvocation:(NSInvocation *)invocation {
//這個很重要,SITableViewWeakProxy不能響應respondsToSelector方法,只是做轉發,所以需要特殊判斷下
if (self.interceptionTarget && invocation.selector == @selector(respondsToSelector:)) {
SEL parameterSel;
[invocation getArgument:¶meterSel atIndex:2];
if ([self interceptionSelector:parameterSel]) {
[invocation invokeWithTarget:self.interceptionTarget];
return;
}
}else if (self.interceptionTarget && [self interceptionSelector:invocation.selector]) {
[invocation invokeWithTarget:self.interceptionTarget];
return;
}
//不需要攔截,直接調用原來的delegate
[invocation invokeWithTarget:self.originTarget];
}
//只需要攔截這三個方法,不需其他方法
- (BOOL)interceptionSelector:(SEL)sel {
return sel == @selector(scrollViewDidScroll:) || sel == @selector(scrollViewDidEndDragging:willDecelerate:) || sel == @selector(scrollViewWillBeginDragging:);
}
@end
三、scrollview分類的實現
ps:以下來自5月1日補充
@selector(setDelegate:)
@selector(delegate)
是一個屬性的set與get方法,它們是一個整體,不能拆分開來,需要都hook,之前思慮不周全,沒考慮到這一點。比如說,不斷的調用get方法然后再重新賦值給set方法,之前的實現就會有問題,改變了原有的實現,雖然一般不會這么做,但程序要嚴謹,不留漏洞。
分類方式的實現沒有采用協議的方式,主要是考慮到幾點:
如果有協議回調、又有通知可以選,那么在開啟監聽方法設計不夠優雅
這樣在組件化使用中更加方便,耦合性比協議小
不在實現中統一判斷最小滑動距離,而是直接傳遞,由使用者自行判斷,靈活性更大;之前的最小滑動距離設定不好操作也是一方面
實現方案說明:
通知的userInfo中,有兩個key,一直是滑動的距離(當前位置減去上一次的位置),還有一個就是哪一個scrollView滑動發出的通知,來解決使用通知引起的多點觸發,不知道該不該響應的問題。
消息轉發者與攔截方法判斷分別在兩個類實現,雖然職責分開了,但是之間互相耦合,沒有通過接口(協議)編程。消息轉發類的實現參考了YYKit里面的實現。
-
兩種實現方式,實際上大同小異
通過函數指針的方式,hook方法的實現。這里替換的是
UIScrollView
這個類的delegate
屬性對應的兩個方法,使用GCD確保只會進行一次通過派生一個子類,類似KVO模式。調用方法使用的是編譯后的方法
objc_msgSendSuper
,還要處理如果之前這個類添加過KVO的情況,并且處理的用的是KVC,如果有變動,不會知道。如果有其他類也使用這種方案,將互相沖突抵消掉。思路與實現參考了IMYAOPTableView測試中分了兩種情況:在開啟監聽之前delegate有值;開啟監聽之后才設置delegate。通過宏來進行不同情況測試。兩種實現方式也是通過宏來控制切換。
具體代碼實現參見:WeakProxy
對于參考與借鑒的源碼在這里一并表示感謝!歡迎斧正!