Skip to content
Browse files

added custom activity indicator support

  • Loading branch information...
1 parent cd74d25 commit 0ff2267e0441108938d6c8158732d0110f6fc36c Josh Chung committed Sep 11, 2012
Showing with 33 additions and 41 deletions.
  1. +0 −19 ODRefreshControl.podspec
  2. +4 −1 ODRefreshControl/ODRefreshControl.h
  3. +29 −21 ODRefreshControl/ODRefreshControl.m
View
19 ODRefreshControl.podspec
@@ -1,19 +0,0 @@
-Pod::Spec.new do |s|
- s.name = 'ODRefreshControl'
- s.version = '1.0.0'
- s.license = 'MIT'
- s.summary = "A pull down to refresh control like the one in Apple's iOS6 Mail App."
- s.homepage = 'https://github.com/Sephiroth87/ODRefreshControl'
- s.author = { 'Fabio Ritrovato' => 'fabio@orangeinaday.com' }
- s.source = { :git => 'https://github.com/Sephiroth87/ODRefreshControl.git', :tag => '1.0.0' }
-
- s.description = 'ODRefreshControl is a "pull down to refresh" control for UIScrollView,' \
- 'like the one Apple introduced in iOS6, but available to anyone from iOS4 and up.'
- s.platform = :ios
-
- s.source_files = 'ODRefreshControl/ODRefreshControl*.{h,m}'
- s.clean_path = 'Demo'
- s.framework = 'QuartzCore'
-
- s.requires_arc = true
-end
View
5 ODRefreshControl/ODRefreshControl.h
@@ -15,7 +15,7 @@
CAShapeLayer *_shapeLayer;
CAShapeLayer *_arrowLayer;
CAShapeLayer *_highlightLayer;
- UIActivityIndicatorView *_activity;
+ id _activity;
BOOL _refreshing;
BOOL _canRefresh;
BOOL _ignoreInset;
@@ -30,6 +30,9 @@
- (id)initInScrollView:(UIScrollView *)scrollView;
+// use custom activity indicator
+- (id)initInScrollView:(UIScrollView *)scrollView withActivityIndicator:(id)activity;
+
// Tells the control that a refresh operation was started programmatically
- (void)beginRefreshing;
View
50 ODRefreshControl/ODRefreshControl.m
@@ -35,7 +35,6 @@ @interface ODRefreshControl ()
@end
@implementation ODRefreshControl
-
@synthesize refreshing = _refreshing;
@synthesize tintColor = _tintColor;
@@ -47,7 +46,11 @@ static inline CGFloat lerp(CGFloat a, CGFloat b, CGFloat p)
return a + (b - a) * p;
}
-- (id)initInScrollView:(UIScrollView *)scrollView
+- (id)initInScrollView:(UIScrollView *)scrollView {
+ return [self initInScrollView:scrollView withActivityIndicator:nil];
+}
+
+- (id)initInScrollView:(UIScrollView *)scrollView withActivityIndicator:(id)activity
{
self = [super initWithFrame:CGRectMake(0, -(kTotalViewHeight + scrollView.contentInset.top), scrollView.frame.size.width, kTotalViewHeight)];
@@ -60,11 +63,10 @@ - (id)initInScrollView:(UIScrollView *)scrollView
[scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
[scrollView addObserver:self forKeyPath:@"contentInset" options:NSKeyValueObservingOptionNew context:nil];
- _activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
- _activity.center = CGPointMake(floor(self.frame.size.width / 2), floor(self.frame.size.height / 2));
- _activity.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
- _activity.alpha = 0;
- [_activity startAnimating];
+ _activity = activity ? activity : [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
+ [_activity setCenter:CGPointMake(floor(self.frame.size.width / 2), floor(self.frame.size.height / 2))];
+ [_activity setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin];
+ [_activity setAlpha:0];
[self addSubview:_activity];
_refreshing = NO;
@@ -129,12 +131,16 @@ - (void)setTintColor:(UIColor *)tintColor
- (void)setActivityIndicatorViewStyle:(UIActivityIndicatorViewStyle)activityIndicatorViewStyle
{
- _activity.activityIndicatorViewStyle = activityIndicatorViewStyle;
+ if ([_activity isMemberOfClass:[UIActivityIndicatorView class]])
+ [_activity setActivityIndicatorViewStyle:activityIndicatorViewStyle];
}
- (UIActivityIndicatorViewStyle)activityIndicatorViewStyle
{
- return _activity.activityIndicatorViewStyle;
+ if ([_activity isMemberOfClass:[UIActivityIndicatorView class]])
+ return [_activity activityIndicatorViewStyle];
+
+ return 0;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
@@ -150,7 +156,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
if (!self.enabled || _ignoreOffset) {
return;
}
-
+
CGFloat offset = [[change objectForKey:@"new"] CGPointValue].y + self.originalContentInset.top;
if (_refreshing) {
@@ -161,8 +167,9 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
_shapeLayer.position = CGPointMake(0, kMaxDistance + offset + kOpenedViewHeight);
[CATransaction commit];
- _activity.center = CGPointMake(floor(self.frame.size.width / 2), MIN(offset + self.frame.size.height + floor(kOpenedViewHeight / 2), self.frame.size.height - kOpenedViewHeight/ 2));
-
+
+ [_activity setCenter:CGPointMake(floor(self.frame.size.width / 2), MIN(offset + self.frame.size.height + floor(kOpenedViewHeight / 2), self.frame.size.height - kOpenedViewHeight/ 2))];
+
_ignoreInset = YES;
_ignoreOffset = YES;
@@ -333,15 +340,16 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
- _activity.layer.transform = CATransform3DMakeScale(0.1, 0.1, 1);
+ [_activity layer].transform = CATransform3DMakeScale(0.1, 0.1, 1);
[CATransaction commit];
[UIView animateWithDuration:0.2 delay:0.15 options:UIViewAnimationOptionCurveLinear animations:^{
- _activity.alpha = 1;
- _activity.layer.transform = CATransform3DMakeScale(1, 1, 1);
+ [_activity setAlpha:1];
+ [_activity layer].transform = CATransform3DMakeScale(1, 1, 1);
} completion:nil];
self.refreshing = YES;
_canRefresh = NO;
+ [_activity startAnimating];
[self sendActionsForControlEvents:UIControlEventValueChanged];
}
@@ -360,15 +368,15 @@ - (void)beginRefreshing
[_arrowLayer addAnimation:alphaAnimation forKey:nil];
[_highlightLayer addAnimation:alphaAnimation forKey:nil];
- _activity.alpha = 1;
- _activity.layer.transform = CATransform3DMakeScale(1, 1, 1);
-
+ [_activity setAlpha:1];
+ [_activity layer].transform = CATransform3DMakeScale(1, 1, 1);
+
CGPoint offset = self.scrollView.contentOffset;
_ignoreInset = YES;
[self.scrollView setContentInset:UIEdgeInsetsMake(kOpenedViewHeight + self.originalContentInset.top, self.originalContentInset.left, self.originalContentInset.bottom, self.originalContentInset.right)];
_ignoreInset = NO;
[self.scrollView setContentOffset:offset animated:NO];
-
+
self.refreshing = YES;
_canRefresh = NO;
}
@@ -387,8 +395,8 @@ - (void)endRefreshing
_ignoreInset = YES;
[blockScrollView setContentInset:self.originalContentInset];
_ignoreInset = NO;
- _activity.alpha = 0;
- _activity.layer.transform = CATransform3DMakeScale(0.1, 0.1, 1);
+ [_activity setAlpha:0];
+ [_activity layer].transform = CATransform3DMakeScale(0.1, 0.1, 1);
} completion:^(BOOL finished) {
[_shapeLayer removeAllAnimations];
_shapeLayer.path = nil;

0 comments on commit 0ff2267

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