Created
June 12, 2014 08:38
-
-
Save ninjinkun/4e61c61cd9f5c90970a4 to your computer and use it in GitHub Desktop.
manage scrollsToTop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ScrollsToTopManager.h" | |
@interface ScrollsToTopManager() | |
@property (nonatomic) NSHashTable *scrollViews; | |
@end | |
@implementation ScrollsToTopManager | |
+ (instancetype)sharedManager | |
{ | |
static id instance; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
instance = [[self alloc] init]; | |
}); | |
return instance; | |
} | |
- (id)init | |
{ | |
self = [super init]; | |
if (self) { | |
self.scrollViews = [NSHashTable weakObjectsHashTable]; | |
} | |
return self; | |
} | |
- (void)registerScrollView:(UIScrollView *)scrollView | |
{ | |
[self.scrollViews addObject:scrollView]; | |
} | |
- (void)becomeScrollsToTop:(UIScrollView *)scrollView | |
{ | |
if ([scrollView respondsToSelector:@selector(setScrollsToTop:)]) { | |
scrollView.scrollsToTop = YES; | |
} | |
NSMutableSet *otherScrollViews = [[self.scrollViews setRepresentation] mutableCopy]; | |
[otherScrollViews minusSet:[NSSet setWithObject:scrollView]]; | |
for (UIScrollView *otherScrollView in otherScrollViews) { | |
if ([otherScrollView respondsToSelector:@selector(setScrollsToTop:)]) { | |
otherScrollView.scrollsToTop = NO; | |
} | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment