Skip to content
Browse files

made lastUpdatedLabel, statusLabel, arrowImage, and activityView prop…

…erties to allow for customization
  • Loading branch information...
1 parent 0265286 commit 4339a363739d89cd7af94b64d77d1e29732f8f3c @chrispix chrispix committed Dec 2, 2011
Showing with 67 additions and 52 deletions.
  1. +4 −0 PullToRefreshView.h
  2. +53 −52 PullToRefreshView.m
  3. +10 −0 README
View
4 PullToRefreshView.h
@@ -53,6 +53,10 @@ typedef enum {
@property (nonatomic, readonly) UIScrollView *scrollView;
@property (nonatomic, assign) id<PullToRefreshViewDelegate> delegate;
@property (nonatomic, assign) NSTimeInterval timeout;
+@property (nonatomic, retain) UILabel *lastUpdatedLabel;
+@property (nonatomic, retain) UILabel *statusLabel;
+@property (nonatomic, retain) CALayer *arrowImage;
+@property (nonatomic, retain) UIActivityIndicatorView *activityView;
- (void)refreshLastUpdatedDate;
View
105 PullToRefreshView.m
@@ -43,75 +43,76 @@ - (void)dismissView;
@implementation PullToRefreshView
@synthesize delegate, scrollView;
+@synthesize lastUpdatedLabel, statusLabel, arrowImage, activityView;
@synthesize timeout;
- (void)showActivity:(BOOL)shouldShow animated:(BOOL)animated {
- if (shouldShow) [activityView startAnimating];
- else [activityView stopAnimating];
-
+ if (shouldShow) [self.activityView startAnimating];
+ else [self.activityView stopAnimating];
+
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:(animated ? 0.1f : 0.0)];
- arrowImage.opacity = (shouldShow ? 0.0 : 1.0);
+ self.arrowImage.opacity = (shouldShow ? 0.0 : 1.0);
[UIView commitAnimations];
}
- (void)setImageFlipped:(BOOL)flipped {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.1f];
- arrowImage.transform = (flipped ? CATransform3DMakeRotation(M_PI * 2, 0.0f, 0.0f, 1.0f) : CATransform3DMakeRotation(M_PI, 0.0f, 0.0f, 1.0f));
+ self.arrowImage.transform = (flipped ? CATransform3DMakeRotation(M_PI * 2, 0.0f, 0.0f, 1.0f) : CATransform3DMakeRotation(M_PI, 0.0f, 0.0f, 1.0f));
[UIView commitAnimations];
}
- (id)initWithScrollView:(UIScrollView *)scroll {
CGRect frame = CGRectMake(0.0f, 0.0f - scroll.bounds.size.height, scroll.bounds.size.width, scroll.bounds.size.height);
-
+
if ((self = [super initWithFrame:frame])) {
scrollView = scroll;
[scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:NULL];
-
+
self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.backgroundColor = [UIColor colorWithRed:226.0/255.0 green:231.0/255.0 blue:237.0/255.0 alpha:1.0];
-
- lastUpdatedLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, frame.size.height - 30.0f, self.frame.size.width, 20.0f)];
- lastUpdatedLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
- lastUpdatedLabel.font = [UIFont systemFontOfSize:12.0f];
- lastUpdatedLabel.textColor = TEXT_COLOR;
- lastUpdatedLabel.shadowColor = [UIColor colorWithWhite:0.9f alpha:1.0f];
- lastUpdatedLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
- lastUpdatedLabel.backgroundColor = [UIColor clearColor];
- lastUpdatedLabel.textAlignment = UITextAlignmentCenter;
- [self addSubview:lastUpdatedLabel];
-
- statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, frame.size.height - 48.0f, self.frame.size.width, 20.0f)];
- statusLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
- statusLabel.font = [UIFont boldSystemFontOfSize:13.0f];
- statusLabel.textColor = TEXT_COLOR;
- statusLabel.shadowColor = [UIColor colorWithWhite:0.9f alpha:1.0f];
- statusLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
- statusLabel.backgroundColor = [UIColor clearColor];
- statusLabel.textAlignment = UITextAlignmentCenter;
- [self addSubview:statusLabel];
-
- arrowImage = [[CALayer alloc] init];
- arrowImage.frame = CGRectMake(25.0f, frame.size.height - 60.0f, 24.0f, 52.0f);
- arrowImage.contentsGravity = kCAGravityResizeAspect;
- arrowImage.contents = (id) [UIImage imageNamed:@"arrow"].CGImage;
-
+
+ self.lastUpdatedLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0f, frame.size.height - 30.0f, self.frame.size.width, 20.0f)] autorelease];
+ self.lastUpdatedLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
+ self.lastUpdatedLabel.font = [UIFont systemFontOfSize:12.0f];
+ self.lastUpdatedLabel.textColor = TEXT_COLOR;
+ self.lastUpdatedLabel.shadowColor = [UIColor colorWithWhite:0.9f alpha:1.0f];
+ self.lastUpdatedLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
+ self.lastUpdatedLabel.backgroundColor = [UIColor clearColor];
+ self.lastUpdatedLabel.textAlignment = UITextAlignmentCenter;
+ [self addSubview:self.lastUpdatedLabel];
+
+ self.statusLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0f, frame.size.height - 48.0f, self.frame.size.width, 20.0f)] autorelease];
+ self.statusLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
+ self.statusLabel.font = [UIFont boldSystemFontOfSize:13.0f];
+ self.statusLabel.textColor = TEXT_COLOR;
+ self.statusLabel.shadowColor = [UIColor colorWithWhite:0.9f alpha:1.0f];
+ self.statusLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
+ self.statusLabel.backgroundColor = [UIColor clearColor];
+ self.statusLabel.textAlignment = UITextAlignmentCenter;
+ [self addSubview:self.statusLabel];
+
+ self.arrowImage = [[[CALayer alloc] init] autorelease];
+ self.arrowImage.frame = CGRectMake(25.0f, frame.size.height - 60.0f, 24.0f, 52.0f);
+ self.arrowImage.contentsGravity = kCAGravityResizeAspect;
+ self.arrowImage.contents = (id) [UIImage imageNamed:@"arrow"].CGImage;
+
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
- arrowImage.contentsScale = [[UIScreen mainScreen] scale];
+ self.arrowImage.contentsScale = [[UIScreen mainScreen] scale];
}
#endif
-
- [self.layer addSublayer:arrowImage];
-
- activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
- activityView.frame = CGRectMake(30.0f, frame.size.height - 38.0f, 20.0f, 20.0f);
- [self addSubview:activityView];
-
+
+ [self.layer addSublayer:self.arrowImage];
+
+ self.activityView = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease];
+ self.activityView.frame = CGRectMake(30.0f, frame.size.height - 38.0f, 20.0f, 20.0f);
+ [self addSubview:self.activityView];
+
[self setState:PullToRefreshViewStateNormal];
}
-
+
return self;
}
@@ -131,39 +132,39 @@ - (id)initWithWebView:(UIWebView *)webView {
- (void)refreshLastUpdatedDate {
NSDate *date = [NSDate date];
-
+
if ([delegate respondsToSelector:@selector(pullToRefreshViewLastUpdated:)])
date = [delegate pullToRefreshViewLastUpdated:self];
-
+
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setAMSymbol:@"AM"];
[formatter setPMSymbol:@"PM"];
[formatter setDateFormat:@"MM/dd/yy hh:mm a"];
- lastUpdatedLabel.text = [NSString stringWithFormat:@"Last Updated: %@", [formatter stringFromDate:date]];
+ self.lastUpdatedLabel.text = [NSString stringWithFormat:@"Last Updated: %@", [formatter stringFromDate:date]];
[formatter release];
}
- (void)setState:(PullToRefreshViewState)state_ {
state = state_;
-
+
switch (state) {
case PullToRefreshViewStateReady:
- statusLabel.text = @"Release to refresh...";
+ self.statusLabel.text = @"Release to refresh...";
[self showActivity:NO animated:NO];
[self setImageFlipped:YES];
scrollView.contentInset = UIEdgeInsetsZero;
break;
-
+
case PullToRefreshViewStateNormal:
- statusLabel.text = @"Pull down to refresh...";
+ self.statusLabel.text = @"Pull down to refresh...";
[self showActivity:NO animated:NO];
[self setImageFlipped:NO];
[self refreshLastUpdatedDate];
scrollView.contentInset = UIEdgeInsetsZero;
break;
-
+
case PullToRefreshViewStateLoading:
- statusLabel.text = @"Loading...";
+ self.statusLabel.text = @"Loading...";
[self showActivity:YES animated:YES];
[self setImageFlipped:NO];
scrollView.contentInset = UIEdgeInsetsMake(60.0f, 0.0f, 0.0f, 0.0f);
@@ -199,7 +200,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
[UIView setAnimationDuration:0.2f];
[self setState:PullToRefreshViewStateLoading];
[UIView commitAnimations];
-
+
if ([delegate respondsToSelector:@selector(pullToRefreshViewShouldRefresh:)])
[delegate pullToRefreshViewShouldRefresh:self];
}
View
10 README
@@ -30,3 +30,13 @@ To use with a web view:
To dismiss the view after a certain period, even if finishedLoading isn't called:
pullToRefreshView.timeout = 5; // 5 seconds
+To customize the appearance, edit the statusLabel, lastUpdatedLabel, activityView, and arrowImage properties:
+ pullToRefreshView.statusLabel.textColor = [UIColor whiteColor];
+ pullToRefreshView.statusLabel.shadowColor = [UIColor lightGrayColor];
+ pullToRefreshView.lastUpdatedLabel.textColor = [UIColor whiteColor];
+ pullToRefreshView.lastUpdatedLabel.shadowColor = [UIColor lightGrayColor];
+
+ pullToRefreshView.activityView.color = [UIColor lightGrayColor];
+
+ pullToRefreshView.arrowImage.contents = (id) [UIImage imageNamed:@"my_arrow"].CGImage;
+

0 comments on commit 4339a36

Please sign in to comment.
Something went wrong with that request. Please try again.