Skip to content
This repository has been archived by the owner on May 4, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2 from bdbergeron/development
Browse files Browse the repository at this point in the history
Version 1.0.1
  • Loading branch information
bdbergeron committed Mar 29, 2014
2 parents cdb4f10 + 22bf4bd commit 745e9ee
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 29 deletions.
2 changes: 1 addition & 1 deletion BDBSpinKitRefreshControl.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'BDBSpinKitRefreshControl'
s.version = '1.0.0'
s.version = '1.0.1'
s.license = 'MIT'
s.summary = 'Easily use SpinKit with UIRefreshControl!'
s.homepage = 'https://github.com/bdbergeron/BDBSpinKitRefreshControl'
Expand Down
15 changes: 14 additions & 1 deletion BDBSpinKitRefreshControl/BDBSpinKitRefreshControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,26 @@

#import "RTSpinKitView.h"

#pragma mark -
@protocol BDBSpinKitRefreshControlDelegate
<NSObject>

@optional
- (void)didShowRefreshControl;
- (void)didHideRefreshControl;

@end

#pragma mark -
@interface BDBSpinKitRefreshControl : UIRefreshControl

@property (nonatomic, weak) id <BDBSpinKitRefreshControlDelegate> delegate;

@property (nonatomic, readonly) RTSpinKitView *spinner;

+ (instancetype)refreshControlWithStyle:(RTSpinKitViewStyle)style color:(UIColor *)color;
@property (nonatomic, assign) BOOL shouldChangeColorInstantly;

+ (instancetype)refreshControlWithStyle:(RTSpinKitViewStyle)style color:(UIColor *)color;
- (id)initWithStyle:(RTSpinKitViewStyle)style color:(UIColor *)color;

@end
81 changes: 57 additions & 24 deletions BDBSpinKitRefreshControl/BDBSpinKitRefreshControl.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
#import "BDBSpinKitRefreshControl.h"

static void * const kBDBSpinKitRefreshControlKVOContext = (void *)&kBDBSpinKitRefreshControlKVOContext;
static NSString * const kHidden = @"hidden";

#pragma mark -
@interface BDBSpinKitRefreshControl ()

@property (nonatomic) UIColor *color;

@end

#pragma mark -
@implementation BDBSpinKitRefreshControl
Expand All @@ -38,15 +46,13 @@ - (id)initWithStyle:(RTSpinKitViewStyle)style color:(UIColor *)color {
super.tintColor = [UIColor clearColor];

_spinner = [[RTSpinKitView alloc] initWithStyle:style color:color];
_spinner.autoresizingMask =
UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin;

_color = color;
_shouldChangeColorInstantly = false;

[super addObserver:self
forKeyPath:@"hidden"
options:NSKeyValueObservingOptionNew
forKeyPath:kHidden
options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew
context:kBDBSpinKitRefreshControlKVOContext];
}

Expand All @@ -55,46 +61,73 @@ - (id)initWithStyle:(RTSpinKitViewStyle)style color:(UIColor *)color {

- (void)dealloc
{
[super removeObserver:self
forKeyPath:@"hidden"
context:kBDBSpinKitRefreshControlKVOContext];
@try {
[super removeObserver:self
forKeyPath:kHidden
context:kBDBSpinKitRefreshControlKVOContext];
}
@catch (NSException * __unused exception) {}
}

- (void)willMoveToSuperview:(UIView *)newSuperview {
[super willMoveToSuperview:newSuperview];

if (self.spinner.superview) {
[self.spinner removeFromSuperview];
}
[self.spinner removeFromSuperview];
}

- (void)didMoveToSuperview {
[super didMoveToSuperview];

if (!self.spinner.superview) {
UIView *parentView = self.subviews.lastObject;
[parentView addSubview:self.spinner];
self.spinner.center = parentView.center;
UIView *modernContentView = self.subviews.lastObject;
UIView *containerView = modernContentView.subviews.firstObject;

if (containerView) {
[containerView removeFromSuperview];
}

[modernContentView addSubview:self.spinner];
self.spinner.center = modernContentView.center;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (context == kBDBSpinKitRefreshControlKVOContext) {
if ([keyPath isEqualToString:@"hidden"]) {
if ([change[NSKeyValueChangeNewKey] boolValue] == NO) {
[self.spinner startAnimating];
} else {
[self.spinner stopAnimating];
if ([object isKindOfClass:[self class]]) {
if ([keyPath isEqualToString:kHidden]) {
BOOL isHidden = [change[NSKeyValueChangeOldKey] boolValue];
BOOL willHide = [change[NSKeyValueChangeNewKey] boolValue];

if (isHidden && !willHide) {
self.spinner.color = self.color;
[self.spinner startAnimating];

if ([self.delegate respondsToSelector:@selector(didShowRefreshControl)]) {
[self.delegate didShowRefreshControl];
}
} else if (!isHidden && willHide) {
[self.spinner stopAnimating];

if ([self.delegate respondsToSelector:@selector(didHideRefreshControl)]) {
[self.delegate didHideRefreshControl];
}
}
}
}
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}

- (void)setTintColor:(UIColor *)tintColor {
self.spinner.color = tintColor;
- (void)setTintColor:(UIColor *)color {
_color = color;

if (self.shouldChangeColorInstantly || self.spinner.hidden) {
self.spinner.color = color;
}
}

- (UIColor *)tintColor {
return self.color;
}

@end
3 changes: 3 additions & 0 deletions Demo/Demo/View Controllers/MainViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@

#import <UIKit/UIKit.h>

#import "BDBSpinKitRefreshControl.h"

#pragma mark -
@interface MainViewController : UITableViewController
<BDBSpinKitRefreshControlDelegate>

@end
25 changes: 24 additions & 1 deletion Demo/Demo/View Controllers/MainViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#import "BDBSpinKitRefreshControl.h"
#import "MainViewController.h"

static void * const kKVOContext = (void *)&kKVOContext;

#pragma mark -
@interface MainViewController ()

@property (nonatomic) NSMutableArray *objects;

@property (nonatomic) BDBSpinKitRefreshControl *refreshControl;
@property (nonatomic) NSTimer *colorTimer;

@end

Expand All @@ -49,6 +51,8 @@ - (void)viewDidLoad {
UIColor *color = [UIColor colorWithRed:0.937f green:0.263f blue:0.157f alpha:1.0f];
self.refreshControl = [BDBSpinKitRefreshControl refreshControlWithStyle:RTSpinKitViewStyleBounce
color:color];
self.refreshControl.delegate = self;
self.refreshControl.shouldChangeColorInstantly = YES;
[self.refreshControl addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged];

self.tableView.rowHeight = (self.tableView.bounds.size.height - 44.0f) / self.objects.count;
Expand All @@ -60,6 +64,25 @@ - (void)refresh {
});
}

- (void)didShowRefreshControl {
self.colorTimer = [NSTimer scheduledTimerWithTimeInterval:0.1f
target:self
selector:@selector(doubleRainbow)
userInfo:nil
repeats:YES];
}

- (void)didHideRefreshControl {
[self.colorTimer invalidate];
}

- (void)doubleRainbow {
CGFloat h, s, v, a;
[self.refreshControl.tintColor getHue:&h saturation:&s brightness:&v alpha:&a];
h = fmodf((h + 0.025f), 1.0f);
self.refreshControl.tintColor = [UIColor colorWithHue:h saturation:s brightness:v alpha:a];
}

#pragma mark TableView Data Source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# BDBSpinKitRefreshControl

BDBSpinKitRefreshControl is a drop-in replacement for`UIRefreshControl that utilizes the fantastic [SpinKit](https://github.com/tobiasahlin/SpinKit) progress indicators.
BDBSpinKitRefreshControl is a drop-in replacement for UIRefreshControl that utilizes the fantastic [SpinKit](https://github.com/tobiasahlin/SpinKit) progress indicators.

![image](https://dl.dropboxusercontent.com/u/6225/GitHub/BDBSpinKitRefreshControl/bounce.gif)
![image](https://dl.dropboxusercontent.com/u/6225/GitHub/BDBSpinKitRefreshControl/wave.gif)
Expand All @@ -18,7 +18,12 @@ self.refreshControl =

```
For supported styles, please take a look at the documentation for [SpinKit-ObjC](https://github.com/raymondjavaxx/SpinKit-ObjC).
## Styles and Color
BDBSpinKitRefreshControl supports all of the currently implemented styles in the [SpinKit-ObjC](https://github.com/raymondjavaxx/SpinKit-ObjC) project. For full documentation, please take a look at its project page.
In order to mimic as closely as possible the behavior of a standard UIRefreshControl, I've added a `shouldChangeColorInstantly` property that defaults to NO. With a standard UIRefreshControl, if you attempt to change the `tintColor` property while it is visible, nothing will happen. On the next pull, however, the color change will have taken effect. If you wish to allow changing the color while the refresh control is visible, simply set `shouldChangeColorInstantly` to YES when instantiating.
## Credits
Expand Down

0 comments on commit 745e9ee

Please sign in to comment.