Last active
August 29, 2015 14:12
-
-
Save onmyway133/510deac69c6f71f1b8b0 to your computer and use it in GitHub Desktop.
FTGViewPool
This file contains 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
// UIView+FTGAdditions.h | |
- (NSString *)ftg_identifier; | |
- (void)ftg_setIdentifier:(NSString *)identifier; | |
// UIVIew+FTGAdditions.m | |
- (NSString *)ftg_identifier { | |
return (NSString *)objc_getAssociatedObject(self, @selector(ftg_identifier)); | |
} | |
- (void)ftg_setIdentifier:(NSString *)identifier { | |
objc_setAssociatedObject(self, @selector(ftg_identifier), identifier, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
} | |
// FTGViewPool.h | |
@protocol FTGViewPoolDelegate <NSObject> | |
- (UIView *)viewPoolRequestViewWithIdentifier:(NSString *)identifier; | |
@end | |
@interface FTGViewPool : NSObject | |
@property (nonatomic, weak) id<FTGViewPoolDelegate> delegate; | |
- (UIView *)dequeueView; | |
@end | |
// FTGViewPool.m | |
@interface FTGViewPool () | |
@property (nonatomic, strong) NSMutableArray *views; | |
@end | |
@implementation FTGViewPool | |
#pragma mark - Life Cycle | |
- (instancetype)init { | |
self = [super init]; | |
if (self) { | |
_views = [NSMutableArray array]; | |
} | |
return self; | |
} | |
#pragma mark - Public Interface | |
- (UIView *)dequeueViewWithIdentifier:(NSString *)identifier { | |
// Find the first view that is not in any hierarchy | |
for (UIView *view in self.views) { | |
if ([self isViewAvailable:view identifier:identifier]) { | |
return view; | |
} | |
} | |
// Else request new view | |
UIView *view = [self requestViewWithIdentifier:identifier]; | |
[view ftg_setIdentifier:identifier]; | |
[self.views addObject:view]; | |
return view; | |
} | |
#pragma mark - Helper | |
- (BOOL)isViewAvailable:(UIView *)view identifier:(NSString *)identifier { | |
return YES; | |
} | |
#pragma mark - Delegate | |
- (UIView *)requestViewWithIdentifier:(NSString *)identifier { | |
if ([self.delegate respondsToSelector:@selector(viewPoolRequestViewWithIdentifier:)]) { | |
return [self.delegate viewPoolRequestViewWithIdentifier:identifier]; | |
} | |
return nil; | |
} | |
@end | |
// Usage | |
#pragma mark - FTGViewPoolDelegate | |
- (UIView *)viewPoolRequestViewWithIdentifier:(NSString *)identifier { | |
if ([identifier isEqualToString:kSetPoint]) { | |
return [self verticalLineImageView]; | |
} | |
return [self blankImageView]; | |
} | |
#pragma mark - ImageView Factory | |
- (UIImageView *)blankImageView { | |
return [[UIImageView alloc] init]; | |
} | |
- (UIImageView *)verticalLineImageView { | |
UIImageView *imageView = [[UIImageView alloc] init]; | |
[imageView setImage:[UIImage imageNamed:@"verticle_line"]]; | |
return imageView; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment