|
11 | 11 | #import "TABAnimated.h" |
12 | 12 | #import <objc/runtime.h> |
13 | 13 | #import "UIScrollView+TABExtension.h" |
| 14 | +#import <objc/runtime.h> |
| 15 | +#import <objc/message.h> |
14 | 16 |
|
15 | 17 | @interface TABFormAnimated() |
16 | 18 |
|
@@ -146,6 +148,93 @@ - (NSInteger)getIndexWithIndexPath:(NSIndexPath *)indexPath { |
146 | 148 | return [self getIndexWithIndex:currentIndex]; |
147 | 149 | } |
148 | 150 |
|
| 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 | + |
149 | 238 | #pragma mark - |
150 | 239 |
|
151 | 240 | - (BOOL)getIndexIsRuning:(NSInteger)index { |
@@ -275,4 +364,125 @@ - (BOOL)scrollEnabled { |
275 | 364 | return YES; |
276 | 365 | } |
277 | 366 |
|
| 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 | + |
278 | 488 | @end |
0 commit comments