Skip to content

Instantly share code, notes, and snippets.

@ninjinkun
Created June 12, 2014 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ninjinkun/4e61c61cd9f5c90970a4 to your computer and use it in GitHub Desktop.
Save ninjinkun/4e61c61cd9f5c90970a4 to your computer and use it in GitHub Desktop.
manage scrollsToTop
#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