Skip to content

Commit

Permalink
Merge pull request #146 from tigerAndBull/develop
Browse files Browse the repository at this point in the history
fix: take over UIScrollViewDelegate
  • Loading branch information
tigerAndBull authored Apr 14, 2021
2 parents 9d8c947 + b11fb5e commit 09b0c2c
Show file tree
Hide file tree
Showing 6 changed files with 277 additions and 3 deletions.
4 changes: 2 additions & 2 deletions TABAnimated.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Pod::Spec.new do |s|

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

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ - (void)rebindDelegate:(UIView *)target {
id <UICollectionViewDelegate> delegate = ((UICollectionView *)target).delegate;
if (!self.isRebindDelegateIMP) {
self.oldDelegate = delegate;
[self updateScrollViewDelegateMethods:delegate target:target];
[self updateDelegateMethods:delegate target:target];
self.isRebindDelegateIMP = YES;
}
Expand Down
2 changes: 2 additions & 0 deletions TABAnimatedDemo/TABAnimated/Control/TABFormAnimated.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ NS_ASSUME_NONNULL_BEGIN
- (void)endAnimation;
- (BOOL)endAnimationWithIndex:(NSInteger)index;

- (void)updateScrollViewDelegateMethods:(id)delegate target:(id)target;

@end

NS_ASSUME_NONNULL_END
210 changes: 210 additions & 0 deletions TABAnimatedDemo/TABAnimated/Control/TABFormAnimated.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#import "TABAnimated.h"
#import <objc/runtime.h>
#import "UIScrollView+TABExtension.h"
#import <objc/runtime.h>
#import <objc/message.h>

@interface TABFormAnimated()

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

- (void)updateScrollViewDelegateMethods:(id)delegate target:(id)target {

SEL oldDidScroll = @selector(scrollViewDidScroll:);
SEL newDidScroll = @selector(tab_scrollViewDidScroll:);
if ([delegate respondsToSelector:oldDidScroll]) {
[self addNewMethodWithSel:oldDidScroll newSel:newDidScroll];
}

SEL oldDidZoom = @selector(scrollViewDidZoom:);
SEL newDidZoom = @selector(tab_scrollViewDidZoom:);
if ([delegate respondsToSelector:oldDidZoom]) {
[self addNewMethodWithSel:oldDidZoom newSel:newDidZoom];
}

SEL oldWillDeginDragging = @selector(scrollViewWillBeginDragging:);
SEL newWillDeginDragging = @selector(tab_scrollViewWillBeginDragging:);
if ([delegate respondsToSelector:oldWillDeginDragging]) {
[self addNewMethodWithSel:oldWillDeginDragging newSel:newWillDeginDragging];
}

SEL oldWillEndDragging = @selector(scrollViewWillEndDragging:withVelocity:targetContentOffset:);
SEL newWillEndDragging = @selector(tab_scrollViewWillEndDragging:withVelocity:targetContentOffset:);
if ([delegate respondsToSelector:oldWillEndDragging]) {
[self addNewMethodWithSel:oldWillEndDragging newSel:newWillEndDragging];
}

SEL oldDidEndDragging = @selector(scrollViewDidEndDragging:willDecelerate:);
SEL newDidEndDragging = @selector(tab_scrollViewDidEndDragging:willDecelerate:);
if ([delegate respondsToSelector:oldDidEndDragging]) {
[self addNewMethodWithSel:oldDidEndDragging newSel:newDidEndDragging];
}

SEL oldWillBeginDece = @selector(scrollViewWillBeginDecelerating:);
SEL newWillBeginDece = @selector(tab_scrollViewWillBeginDecelerating:);
if ([delegate respondsToSelector:oldWillBeginDece]) {
[self addNewMethodWithSel:oldWillBeginDece newSel:newWillBeginDece];
}

SEL oldEndBeginDece = @selector(scrollViewDidEndDecelerating:);
SEL newEndBeginDece = @selector(tab_scrollViewDidEndDecelerating:);
if ([delegate respondsToSelector:oldEndBeginDece]) {
[self addNewMethodWithSel:oldEndBeginDece newSel:newEndBeginDece];
}

SEL oldEndScrolling = @selector(scrollViewDidEndScrollingAnimation:);
SEL newEndScrolling = @selector(tab_scrollViewDidEndScrollingAnimation:);
if ([delegate respondsToSelector:oldEndScrolling]) {
[self addNewMethodWithSel:oldEndScrolling newSel:newEndScrolling];
}

SEL oldViewZooming = @selector(viewForZoomingInScrollView:);
SEL newViewZooming = @selector(tab_viewForZoomingInScrollView:);
if ([delegate respondsToSelector:oldViewZooming]) {
[self addNewMethodWithSel:oldViewZooming newSel:newViewZooming];
}

SEL oldWillBeginZooming = @selector(scrollViewWillBeginZooming:withView:);
SEL newWillBeginZooming = @selector(tab_scrollViewWillBeginZooming:withView:);
if ([delegate respondsToSelector:oldWillBeginZooming]) {
[self addNewMethodWithSel:oldWillBeginZooming newSel:newWillBeginZooming];
}

SEL oldDidEndZooming = @selector(scrollViewDidEndZooming:withView:atScale:);
SEL newDidEndZooming = @selector(tab_scrollViewDidEndZooming:withView:atScale:);
if ([delegate respondsToSelector:oldDidEndZooming]) {
[self addNewMethodWithSel:oldDidEndZooming newSel:newDidEndZooming];
}

SEL oldScrollToTop = @selector(scrollViewShouldScrollToTop:);
SEL newScrollToTop = @selector(tab_scrollViewShouldScrollToTop:);
if ([delegate respondsToSelector:oldScrollToTop]) {
[self addNewMethodWithSel:oldScrollToTop newSel:newScrollToTop];
}

SEL oldDidScrollToTop = @selector(scrollViewDidScrollToTop:);
SEL newDidScrollToTop = @selector(tab_scrollViewDidScrollToTop:);
if ([delegate respondsToSelector:oldDidScrollToTop]) {
[self addNewMethodWithSel:oldDidScrollToTop newSel:newDidScrollToTop];
}

SEL oldDidChangeAdjusted = @selector(scrollViewDidChangeAdjustedContentInset:);
SEL newDidChangeAdjusted = @selector(tab_scrollViewDidChangeAdjustedContentInset:);
if ([delegate respondsToSelector:oldDidChangeAdjusted]) {
[self addNewMethodWithSel:oldDidChangeAdjusted newSel:newDidChangeAdjusted];
}
}

#pragma mark -

- (BOOL)getIndexIsRuning:(NSInteger)index {
Expand Down Expand Up @@ -275,4 +364,125 @@ - (BOOL)scrollEnabled {
return YES;
}

#pragma mark -

#pragma mark - UIScrollViewDelegate

- (void)tab_scrollViewDidScroll:(UIScrollView *)scrollView {
TABFormAnimated *tabAnimated = (TABFormAnimated *)(scrollView.tabAnimated);
if (tabAnimated.isAnimating) return;
id oldDelegate = tabAnimated.oldDelegate;
SEL sel = @selector(scrollViewDidScroll:);
((void (*)(id, SEL, UIScrollView *))objc_msgSend)((id)oldDelegate, sel, scrollView);
}

- (void)tab_scrollViewDidZoom:(UIScrollView *)scrollView {
TABFormAnimated *tabAnimated = (TABFormAnimated *)(scrollView.tabAnimated);
if (tabAnimated.isAnimating) return;
id oldDelegate = tabAnimated.oldDelegate;
SEL sel = @selector(scrollViewDidZoom:);
((void (*)(id, SEL, UIScrollView *))objc_msgSend)((id)oldDelegate, sel, scrollView);
}

// called on start of dragging (may require some time and or distance to move)
- (void)tab_scrollViewWillBeginDragging:(UIScrollView *)scrollView {
TABFormAnimated *tabAnimated = (TABFormAnimated *)(scrollView.tabAnimated);
if (tabAnimated.isAnimating) return;
id oldDelegate = tabAnimated.oldDelegate;
SEL sel = @selector(scrollViewWillBeginDragging:);
((void (*)(id, SEL, UIScrollView *))objc_msgSend)((id)oldDelegate, sel, scrollView);
}

// 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
- (void)tab_scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset API_AVAILABLE(ios(5.0)) {
TABFormAnimated *tabAnimated = (TABFormAnimated *)(scrollView.tabAnimated);
if (tabAnimated.isAnimating) return;
id oldDelegate = tabAnimated.oldDelegate;
SEL sel = @selector(scrollViewWillEndDragging:withVelocity:targetContentOffset:);
((void (*)(id, SEL, UIScrollView *, CGPoint, CGPoint *))objc_msgSend)((id)oldDelegate, sel, scrollView, velocity, targetContentOffset);
}

// called on finger up if the user dragged. decelerate is true if it will continue moving afterwards
- (void)tab_scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
TABFormAnimated *tabAnimated = (TABFormAnimated *)(scrollView.tabAnimated);
if (tabAnimated.isAnimating) return;
id oldDelegate = tabAnimated.oldDelegate;
SEL sel = @selector(scrollViewDidEndDragging:willDecelerate:);
((void (*)(id, SEL, UIScrollView *, BOOL))objc_msgSend)((id)oldDelegate, sel, scrollView, decelerate);
}

- (void)tab_scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
TABFormAnimated *tabAnimated = (TABFormAnimated *)(scrollView.tabAnimated);
if (tabAnimated.isAnimating) return;
id oldDelegate = tabAnimated.oldDelegate;
SEL sel = @selector(scrollViewWillBeginDecelerating:);
((void (*)(id, SEL, UIScrollView *))objc_msgSend)((id)oldDelegate, sel, scrollView);
}

- (void)tab_scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
TABFormAnimated *tabAnimated = (TABFormAnimated *)(scrollView.tabAnimated);
if (tabAnimated.isAnimating) return;
id oldDelegate = tabAnimated.oldDelegate;
SEL sel = @selector(scrollViewDidEndDecelerating:);
((void (*)(id, SEL, UIScrollView *))objc_msgSend)((id)oldDelegate, sel, scrollView);
}

- (void)tab_scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
TABFormAnimated *tabAnimated = (TABFormAnimated *)(scrollView.tabAnimated);
if (tabAnimated.isAnimating) return;
id oldDelegate = tabAnimated.oldDelegate;
SEL sel = @selector(scrollViewDidEndScrollingAnimation:);
((void (*)(id, SEL, UIScrollView *))objc_msgSend)((id)oldDelegate, sel, scrollView);
}

- (nullable UIView *)tab_viewForZoomingInScrollView:(UIScrollView *)scrollView {
TABFormAnimated *tabAnimated = (TABFormAnimated *)(scrollView.tabAnimated);
if (tabAnimated.isAnimating) return nil;
id oldDelegate = tabAnimated.oldDelegate;
SEL sel = @selector(viewForZoomingInScrollView:);
return ((UIView * (*)(id, SEL, UIScrollView *))objc_msgSend)((id)oldDelegate, sel, scrollView);
}

- (void)tab_scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view API_AVAILABLE(ios(3.2)) {
TABFormAnimated *tabAnimated = (TABFormAnimated *)(scrollView.tabAnimated);
if (tabAnimated.isAnimating) return;
id oldDelegate = tabAnimated.oldDelegate;
SEL sel = @selector(scrollViewWillBeginZooming:withView:);
((void (*)(id, SEL, UIScrollView *, UIView *))objc_msgSend)((id)oldDelegate, sel, scrollView, view);
}

- (void)tab_scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale {
TABFormAnimated *tabAnimated = (TABFormAnimated *)(scrollView.tabAnimated);
if (tabAnimated.isAnimating) return;
id oldDelegate = tabAnimated.oldDelegate;
SEL sel = @selector(scrollViewDidEndZooming:withView:atScale:);
((void (*)(id, SEL, UIScrollView *, UIView *, CGFloat))objc_msgSend)((id)oldDelegate, sel, scrollView, view, scale);
}

- (BOOL)tab_scrollViewShouldScrollToTop:(UIScrollView *)scrollView {
TABFormAnimated *tabAnimated = (TABFormAnimated *)(scrollView.tabAnimated);
if (tabAnimated.isAnimating) return NO;
id oldDelegate = tabAnimated.oldDelegate;
SEL sel = @selector(scrollViewShouldScrollToTop:);
return ((BOOL (*)(id, SEL, UIScrollView *))objc_msgSend)((id)oldDelegate, sel, scrollView);
}

- (void)tab_scrollViewDidScrollToTop:(UIScrollView *)scrollView {
TABFormAnimated *tabAnimated = (TABFormAnimated *)(scrollView.tabAnimated);
if (tabAnimated.isAnimating) return;
id oldDelegate = tabAnimated.oldDelegate;
SEL sel = @selector(scrollViewShouldScrollToTop:);
((void (*)(id, SEL, UIScrollView *))objc_msgSend)((id)oldDelegate, sel, scrollView);
}

/* Also see -[UIScrollView adjustedContentInsetDidChange]
*/
- (void)tab_scrollViewDidChangeAdjustedContentInset:(UIScrollView *)scrollView API_AVAILABLE(ios(11.0), tvos(11.0)) {
TABFormAnimated *tabAnimated = (TABFormAnimated *)(scrollView.tabAnimated);
if (tabAnimated.isAnimating) return;
id oldDelegate = tabAnimated.oldDelegate;
SEL sel = @selector(scrollViewDidChangeAdjustedContentInset:);
((void (*)(id, SEL, UIScrollView *))objc_msgSend)((id)oldDelegate, sel, scrollView);
}

@end
1 change: 1 addition & 0 deletions TABAnimatedDemo/TABAnimated/Control/TABTableAnimated.m
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ - (void)rebindDelegate:(UIView *)target {
if (self.isRebindDelegateIMP) return;
id <UITableViewDelegate> delegate = ((UITableView *)target).delegate;
self.oldDelegate = delegate;
[self updateScrollViewDelegateMethods:delegate target:target];
[self updateDelegateMethods:delegate target:target];
self.isRebindDelegateIMP = YES;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#import "TABAnimatedControllerUIInterface.h"
#import "TABAnimatedControllerUIImpl.h"

@interface BaseDemoViewController ()
@interface BaseDemoViewController ()<UIScrollViewDelegate>

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

Expand Down Expand Up @@ -58,4 +58,64 @@ - (void)reloadViewAnimated {

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSLog(@"%@", NSStringFromSelector(_cmd));
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView API_AVAILABLE(ios(3.2)) {
NSLog(@"%@", NSStringFromSelector(_cmd));
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
NSLog(@"%@", NSStringFromSelector(_cmd));
}

// 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
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
NSLog(@"%@", NSStringFromSelector(_cmd));
}

// called on finger up if the user dragged. decelerate is true if it will continue moving afterwards
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
NSLog(@"%@", NSStringFromSelector(_cmd));
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
NSLog(@"%@", NSStringFromSelector(_cmd));
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
NSLog(@"%@", NSStringFromSelector(_cmd));
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
NSLog(@"%@", NSStringFromSelector(_cmd));
}

- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
NSLog(@"%@", NSStringFromSelector(_cmd));
return nil;
}

- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view API_AVAILABLE(ios(3.2)) {
NSLog(@"%@", NSStringFromSelector(_cmd));
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale {
NSLog(@"%@", NSStringFromSelector(_cmd));
}

- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView {
NSLog(@"%@", NSStringFromSelector(_cmd));
return NO;
}

- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
NSLog(@"%@", NSStringFromSelector(_cmd));
}

- (void)scrollViewDidChangeAdjustedContentInset:(UIScrollView *)scrollView API_AVAILABLE(ios(11.0), tvos(11.0)) {
NSLog(@"%@", NSStringFromSelector(_cmd));
}

@end

0 comments on commit 09b0c2c

Please sign in to comment.