Skip to content

Commit 09b0c2c

Browse files
authored
Merge pull request #146 from tigerAndBull/develop
fix: take over UIScrollViewDelegate
2 parents 9d8c947 + b11fb5e commit 09b0c2c

6 files changed

Lines changed: 277 additions & 3 deletions

File tree

TABAnimated.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Pod::Spec.new do |s|
55

66
#tag方式:填tag名称
77
#commit方式:填commit的id
8-
s.version = "2.5.2"
8+
s.version = "2.5.3"
99
#库的简介
1010
s.summary = "TABAnimated是一个ios平台上的网络过渡动画(骨架屏)的封装"
1111

@@ -26,7 +26,7 @@ Pod::Spec.new do |s|
2626
s.platform = :ios, "8.0"
2727

2828
#库的地址
29-
s.source = { :git => "https://github.com/tigerAndBull/TABAnimated.git", :tag => "2.5.2" }
29+
s.source = { :git => "https://github.com/tigerAndBull/TABAnimated.git", :tag => "2.5.3" }
3030

3131
s.source_files = 'TABAnimatedDemo/TABAnimated/**/*.{h,m}'
3232

TABAnimatedDemo/TABAnimated/Control/TABCollectionAnimated.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ - (void)rebindDelegate:(UIView *)target {
208208
id <UICollectionViewDelegate> delegate = ((UICollectionView *)target).delegate;
209209
if (!self.isRebindDelegateIMP) {
210210
self.oldDelegate = delegate;
211+
[self updateScrollViewDelegateMethods:delegate target:target];
211212
[self updateDelegateMethods:delegate target:target];
212213
self.isRebindDelegateIMP = YES;
213214
}

TABAnimatedDemo/TABAnimated/Control/TABFormAnimated.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ NS_ASSUME_NONNULL_BEGIN
113113
- (void)endAnimation;
114114
- (BOOL)endAnimationWithIndex:(NSInteger)index;
115115

116+
- (void)updateScrollViewDelegateMethods:(id)delegate target:(id)target;
117+
116118
@end
117119

118120
NS_ASSUME_NONNULL_END

TABAnimatedDemo/TABAnimated/Control/TABFormAnimated.m

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#import "TABAnimated.h"
1212
#import <objc/runtime.h>
1313
#import "UIScrollView+TABExtension.h"
14+
#import <objc/runtime.h>
15+
#import <objc/message.h>
1416

1517
@interface TABFormAnimated()
1618

@@ -146,6 +148,93 @@ - (NSInteger)getIndexWithIndexPath:(NSIndexPath *)indexPath {
146148
return [self getIndexWithIndex:currentIndex];
147149
}
148150

151+
- (void)updateScrollViewDelegateMethods:(id)delegate target:(id)target {
152+
153+
SEL oldDidScroll = @selector(scrollViewDidScroll:);
154+
SEL newDidScroll = @selector(tab_scrollViewDidScroll:);
155+
if ([delegate respondsToSelector:oldDidScroll]) {
156+
[self addNewMethodWithSel:oldDidScroll newSel:newDidScroll];
157+
}
158+
159+
SEL oldDidZoom = @selector(scrollViewDidZoom:);
160+
SEL newDidZoom = @selector(tab_scrollViewDidZoom:);
161+
if ([delegate respondsToSelector:oldDidZoom]) {
162+
[self addNewMethodWithSel:oldDidZoom newSel:newDidZoom];
163+
}
164+
165+
SEL oldWillDeginDragging = @selector(scrollViewWillBeginDragging:);
166+
SEL newWillDeginDragging = @selector(tab_scrollViewWillBeginDragging:);
167+
if ([delegate respondsToSelector:oldWillDeginDragging]) {
168+
[self addNewMethodWithSel:oldWillDeginDragging newSel:newWillDeginDragging];
169+
}
170+
171+
SEL oldWillEndDragging = @selector(scrollViewWillEndDragging:withVelocity:targetContentOffset:);
172+
SEL newWillEndDragging = @selector(tab_scrollViewWillEndDragging:withVelocity:targetContentOffset:);
173+
if ([delegate respondsToSelector:oldWillEndDragging]) {
174+
[self addNewMethodWithSel:oldWillEndDragging newSel:newWillEndDragging];
175+
}
176+
177+
SEL oldDidEndDragging = @selector(scrollViewDidEndDragging:willDecelerate:);
178+
SEL newDidEndDragging = @selector(tab_scrollViewDidEndDragging:willDecelerate:);
179+
if ([delegate respondsToSelector:oldDidEndDragging]) {
180+
[self addNewMethodWithSel:oldDidEndDragging newSel:newDidEndDragging];
181+
}
182+
183+
SEL oldWillBeginDece = @selector(scrollViewWillBeginDecelerating:);
184+
SEL newWillBeginDece = @selector(tab_scrollViewWillBeginDecelerating:);
185+
if ([delegate respondsToSelector:oldWillBeginDece]) {
186+
[self addNewMethodWithSel:oldWillBeginDece newSel:newWillBeginDece];
187+
}
188+
189+
SEL oldEndBeginDece = @selector(scrollViewDidEndDecelerating:);
190+
SEL newEndBeginDece = @selector(tab_scrollViewDidEndDecelerating:);
191+
if ([delegate respondsToSelector:oldEndBeginDece]) {
192+
[self addNewMethodWithSel:oldEndBeginDece newSel:newEndBeginDece];
193+
}
194+
195+
SEL oldEndScrolling = @selector(scrollViewDidEndScrollingAnimation:);
196+
SEL newEndScrolling = @selector(tab_scrollViewDidEndScrollingAnimation:);
197+
if ([delegate respondsToSelector:oldEndScrolling]) {
198+
[self addNewMethodWithSel:oldEndScrolling newSel:newEndScrolling];
199+
}
200+
201+
SEL oldViewZooming = @selector(viewForZoomingInScrollView:);
202+
SEL newViewZooming = @selector(tab_viewForZoomingInScrollView:);
203+
if ([delegate respondsToSelector:oldViewZooming]) {
204+
[self addNewMethodWithSel:oldViewZooming newSel:newViewZooming];
205+
}
206+
207+
SEL oldWillBeginZooming = @selector(scrollViewWillBeginZooming:withView:);
208+
SEL newWillBeginZooming = @selector(tab_scrollViewWillBeginZooming:withView:);
209+
if ([delegate respondsToSelector:oldWillBeginZooming]) {
210+
[self addNewMethodWithSel:oldWillBeginZooming newSel:newWillBeginZooming];
211+
}
212+
213+
SEL oldDidEndZooming = @selector(scrollViewDidEndZooming:withView:atScale:);
214+
SEL newDidEndZooming = @selector(tab_scrollViewDidEndZooming:withView:atScale:);
215+
if ([delegate respondsToSelector:oldDidEndZooming]) {
216+
[self addNewMethodWithSel:oldDidEndZooming newSel:newDidEndZooming];
217+
}
218+
219+
SEL oldScrollToTop = @selector(scrollViewShouldScrollToTop:);
220+
SEL newScrollToTop = @selector(tab_scrollViewShouldScrollToTop:);
221+
if ([delegate respondsToSelector:oldScrollToTop]) {
222+
[self addNewMethodWithSel:oldScrollToTop newSel:newScrollToTop];
223+
}
224+
225+
SEL oldDidScrollToTop = @selector(scrollViewDidScrollToTop:);
226+
SEL newDidScrollToTop = @selector(tab_scrollViewDidScrollToTop:);
227+
if ([delegate respondsToSelector:oldDidScrollToTop]) {
228+
[self addNewMethodWithSel:oldDidScrollToTop newSel:newDidScrollToTop];
229+
}
230+
231+
SEL oldDidChangeAdjusted = @selector(scrollViewDidChangeAdjustedContentInset:);
232+
SEL newDidChangeAdjusted = @selector(tab_scrollViewDidChangeAdjustedContentInset:);
233+
if ([delegate respondsToSelector:oldDidChangeAdjusted]) {
234+
[self addNewMethodWithSel:oldDidChangeAdjusted newSel:newDidChangeAdjusted];
235+
}
236+
}
237+
149238
#pragma mark -
150239

151240
- (BOOL)getIndexIsRuning:(NSInteger)index {
@@ -275,4 +364,125 @@ - (BOOL)scrollEnabled {
275364
return YES;
276365
}
277366

367+
#pragma mark -
368+
369+
#pragma mark - UIScrollViewDelegate
370+
371+
- (void)tab_scrollViewDidScroll:(UIScrollView *)scrollView {
372+
TABFormAnimated *tabAnimated = (TABFormAnimated *)(scrollView.tabAnimated);
373+
if (tabAnimated.isAnimating) return;
374+
id oldDelegate = tabAnimated.oldDelegate;
375+
SEL sel = @selector(scrollViewDidScroll:);
376+
((void (*)(id, SEL, UIScrollView *))objc_msgSend)((id)oldDelegate, sel, scrollView);
377+
}
378+
379+
- (void)tab_scrollViewDidZoom:(UIScrollView *)scrollView {
380+
TABFormAnimated *tabAnimated = (TABFormAnimated *)(scrollView.tabAnimated);
381+
if (tabAnimated.isAnimating) return;
382+
id oldDelegate = tabAnimated.oldDelegate;
383+
SEL sel = @selector(scrollViewDidZoom:);
384+
((void (*)(id, SEL, UIScrollView *))objc_msgSend)((id)oldDelegate, sel, scrollView);
385+
}
386+
387+
// called on start of dragging (may require some time and or distance to move)
388+
- (void)tab_scrollViewWillBeginDragging:(UIScrollView *)scrollView {
389+
TABFormAnimated *tabAnimated = (TABFormAnimated *)(scrollView.tabAnimated);
390+
if (tabAnimated.isAnimating) return;
391+
id oldDelegate = tabAnimated.oldDelegate;
392+
SEL sel = @selector(scrollViewWillBeginDragging:);
393+
((void (*)(id, SEL, UIScrollView *))objc_msgSend)((id)oldDelegate, sel, scrollView);
394+
}
395+
396+
// called on finger up if the user dragged. velocity is in points/millisecond. targetContentOffset may be changed to adjust where the scroll view comes to rest
397+
- (void)tab_scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset API_AVAILABLE(ios(5.0)) {
398+
TABFormAnimated *tabAnimated = (TABFormAnimated *)(scrollView.tabAnimated);
399+
if (tabAnimated.isAnimating) return;
400+
id oldDelegate = tabAnimated.oldDelegate;
401+
SEL sel = @selector(scrollViewWillEndDragging:withVelocity:targetContentOffset:);
402+
((void (*)(id, SEL, UIScrollView *, CGPoint, CGPoint *))objc_msgSend)((id)oldDelegate, sel, scrollView, velocity, targetContentOffset);
403+
}
404+
405+
// called on finger up if the user dragged. decelerate is true if it will continue moving afterwards
406+
- (void)tab_scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
407+
TABFormAnimated *tabAnimated = (TABFormAnimated *)(scrollView.tabAnimated);
408+
if (tabAnimated.isAnimating) return;
409+
id oldDelegate = tabAnimated.oldDelegate;
410+
SEL sel = @selector(scrollViewDidEndDragging:willDecelerate:);
411+
((void (*)(id, SEL, UIScrollView *, BOOL))objc_msgSend)((id)oldDelegate, sel, scrollView, decelerate);
412+
}
413+
414+
- (void)tab_scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
415+
TABFormAnimated *tabAnimated = (TABFormAnimated *)(scrollView.tabAnimated);
416+
if (tabAnimated.isAnimating) return;
417+
id oldDelegate = tabAnimated.oldDelegate;
418+
SEL sel = @selector(scrollViewWillBeginDecelerating:);
419+
((void (*)(id, SEL, UIScrollView *))objc_msgSend)((id)oldDelegate, sel, scrollView);
420+
}
421+
422+
- (void)tab_scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
423+
TABFormAnimated *tabAnimated = (TABFormAnimated *)(scrollView.tabAnimated);
424+
if (tabAnimated.isAnimating) return;
425+
id oldDelegate = tabAnimated.oldDelegate;
426+
SEL sel = @selector(scrollViewDidEndDecelerating:);
427+
((void (*)(id, SEL, UIScrollView *))objc_msgSend)((id)oldDelegate, sel, scrollView);
428+
}
429+
430+
- (void)tab_scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
431+
TABFormAnimated *tabAnimated = (TABFormAnimated *)(scrollView.tabAnimated);
432+
if (tabAnimated.isAnimating) return;
433+
id oldDelegate = tabAnimated.oldDelegate;
434+
SEL sel = @selector(scrollViewDidEndScrollingAnimation:);
435+
((void (*)(id, SEL, UIScrollView *))objc_msgSend)((id)oldDelegate, sel, scrollView);
436+
}
437+
438+
- (nullable UIView *)tab_viewForZoomingInScrollView:(UIScrollView *)scrollView {
439+
TABFormAnimated *tabAnimated = (TABFormAnimated *)(scrollView.tabAnimated);
440+
if (tabAnimated.isAnimating) return nil;
441+
id oldDelegate = tabAnimated.oldDelegate;
442+
SEL sel = @selector(viewForZoomingInScrollView:);
443+
return ((UIView * (*)(id, SEL, UIScrollView *))objc_msgSend)((id)oldDelegate, sel, scrollView);
444+
}
445+
446+
- (void)tab_scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view API_AVAILABLE(ios(3.2)) {
447+
TABFormAnimated *tabAnimated = (TABFormAnimated *)(scrollView.tabAnimated);
448+
if (tabAnimated.isAnimating) return;
449+
id oldDelegate = tabAnimated.oldDelegate;
450+
SEL sel = @selector(scrollViewWillBeginZooming:withView:);
451+
((void (*)(id, SEL, UIScrollView *, UIView *))objc_msgSend)((id)oldDelegate, sel, scrollView, view);
452+
}
453+
454+
- (void)tab_scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale {
455+
TABFormAnimated *tabAnimated = (TABFormAnimated *)(scrollView.tabAnimated);
456+
if (tabAnimated.isAnimating) return;
457+
id oldDelegate = tabAnimated.oldDelegate;
458+
SEL sel = @selector(scrollViewDidEndZooming:withView:atScale:);
459+
((void (*)(id, SEL, UIScrollView *, UIView *, CGFloat))objc_msgSend)((id)oldDelegate, sel, scrollView, view, scale);
460+
}
461+
462+
- (BOOL)tab_scrollViewShouldScrollToTop:(UIScrollView *)scrollView {
463+
TABFormAnimated *tabAnimated = (TABFormAnimated *)(scrollView.tabAnimated);
464+
if (tabAnimated.isAnimating) return NO;
465+
id oldDelegate = tabAnimated.oldDelegate;
466+
SEL sel = @selector(scrollViewShouldScrollToTop:);
467+
return ((BOOL (*)(id, SEL, UIScrollView *))objc_msgSend)((id)oldDelegate, sel, scrollView);
468+
}
469+
470+
- (void)tab_scrollViewDidScrollToTop:(UIScrollView *)scrollView {
471+
TABFormAnimated *tabAnimated = (TABFormAnimated *)(scrollView.tabAnimated);
472+
if (tabAnimated.isAnimating) return;
473+
id oldDelegate = tabAnimated.oldDelegate;
474+
SEL sel = @selector(scrollViewShouldScrollToTop:);
475+
((void (*)(id, SEL, UIScrollView *))objc_msgSend)((id)oldDelegate, sel, scrollView);
476+
}
477+
478+
/* Also see -[UIScrollView adjustedContentInsetDidChange]
479+
*/
480+
- (void)tab_scrollViewDidChangeAdjustedContentInset:(UIScrollView *)scrollView API_AVAILABLE(ios(11.0), tvos(11.0)) {
481+
TABFormAnimated *tabAnimated = (TABFormAnimated *)(scrollView.tabAnimated);
482+
if (tabAnimated.isAnimating) return;
483+
id oldDelegate = tabAnimated.oldDelegate;
484+
SEL sel = @selector(scrollViewDidChangeAdjustedContentInset:);
485+
((void (*)(id, SEL, UIScrollView *))objc_msgSend)((id)oldDelegate, sel, scrollView);
486+
}
487+
278488
@end

TABAnimatedDemo/TABAnimated/Control/TABTableAnimated.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ - (void)rebindDelegate:(UIView *)target {
288288
if (self.isRebindDelegateIMP) return;
289289
id <UITableViewDelegate> delegate = ((UITableView *)target).delegate;
290290
self.oldDelegate = delegate;
291+
[self updateScrollViewDelegateMethods:delegate target:target];
291292
[self updateDelegateMethods:delegate target:target];
292293
self.isRebindDelegateIMP = YES;
293294
}

TABAnimatedDemo/TABAnimatedDemo/Base/Controller/BaseDemoViewController.m

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#import "TABAnimatedControllerUIInterface.h"
1111
#import "TABAnimatedControllerUIImpl.h"
1212

13-
@interface BaseDemoViewController ()
13+
@interface BaseDemoViewController ()<UIScrollViewDelegate>
1414

1515
@property (nonatomic, strong) id <TABAnimatedControllerUIInterface> rightButtonImpl;
1616

@@ -58,4 +58,64 @@ - (void)reloadViewAnimated {
5858

5959
}
6060

61+
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
62+
NSLog(@"%@", NSStringFromSelector(_cmd));
63+
}
64+
65+
- (void)scrollViewDidZoom:(UIScrollView *)scrollView API_AVAILABLE(ios(3.2)) {
66+
NSLog(@"%@", NSStringFromSelector(_cmd));
67+
}
68+
69+
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
70+
NSLog(@"%@", NSStringFromSelector(_cmd));
71+
}
72+
73+
// called on finger up if the user dragged. velocity is in points/millisecond. targetContentOffset may be changed to adjust where the scroll view comes to rest
74+
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
75+
NSLog(@"%@", NSStringFromSelector(_cmd));
76+
}
77+
78+
// called on finger up if the user dragged. decelerate is true if it will continue moving afterwards
79+
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
80+
NSLog(@"%@", NSStringFromSelector(_cmd));
81+
}
82+
83+
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
84+
NSLog(@"%@", NSStringFromSelector(_cmd));
85+
}
86+
87+
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
88+
NSLog(@"%@", NSStringFromSelector(_cmd));
89+
}
90+
91+
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
92+
NSLog(@"%@", NSStringFromSelector(_cmd));
93+
}
94+
95+
- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
96+
NSLog(@"%@", NSStringFromSelector(_cmd));
97+
return nil;
98+
}
99+
100+
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view API_AVAILABLE(ios(3.2)) {
101+
NSLog(@"%@", NSStringFromSelector(_cmd));
102+
}
103+
104+
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale {
105+
NSLog(@"%@", NSStringFromSelector(_cmd));
106+
}
107+
108+
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView {
109+
NSLog(@"%@", NSStringFromSelector(_cmd));
110+
return NO;
111+
}
112+
113+
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
114+
NSLog(@"%@", NSStringFromSelector(_cmd));
115+
}
116+
117+
- (void)scrollViewDidChangeAdjustedContentInset:(UIScrollView *)scrollView API_AVAILABLE(ios(11.0), tvos(11.0)) {
118+
NSLog(@"%@", NSStringFromSelector(_cmd));
119+
}
120+
61121
@end

0 commit comments

Comments
 (0)