Permalink
Browse files

Overall reshuffling

  • Loading branch information...
1 parent a0fc33b commit ff12f8f475c9b591c1161801f02b7e49d60b6d3e @peyton committed Feb 22, 2012
@@ -42,7 +42,6 @@
9C36418E14F41CA2005DE008 /* PullToCreateViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = PullToCreateViewController.m; sourceTree = "<group>"; };
9C3641B714F43947005DE008 /* Icon.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Icon.png; sourceTree = "<group>"; };
9C3641B814F43947005DE008 /* Icon@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Icon@2x.png"; sourceTree = "<group>"; };
- 9CB3D2B214F45631006283F0 /* ARCHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ARCHelper.h; sourceTree = "<group>"; };
9CB3D2B814F45631006283F0 /* Circle-Arrow.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Circle-Arrow.png"; sourceTree = "<group>"; };
9CB3D2B914F45631006283F0 /* Circle-Arrow@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Circle-Arrow@2x.png"; sourceTree = "<group>"; };
9CB3D2C314F45701006283F0 /* MOOPullGestureRecognizer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MOOPullGestureRecognizer.h; sourceTree = "<group>"; };
@@ -51,6 +50,9 @@
9CB3D2C614F45701006283F0 /* MOORefreshView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MOORefreshView.m; sourceTree = "<group>"; };
9CB3D2CD14F45E9D006283F0 /* Arrow-Bucket.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Arrow-Bucket.png"; sourceTree = "<group>"; };
9CB3D2CE14F45E9D006283F0 /* Arrow-Bucket@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Arrow-Bucket@2x.png"; sourceTree = "<group>"; };
+ 9CB3D2EF14F479EA006283F0 /* MOOTriggerView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MOOTriggerView.h; sourceTree = "<group>"; };
+ 9CB3D2F414F48A37006283F0 /* MOOPullGestureRecognizerSubclass.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MOOPullGestureRecognizerSubclass.h; sourceTree = "<group>"; };
+ 9CB3D2F514F48D8F006283F0 /* ARCHelper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = ARCHelper.h; path = Support/ARCHelper.h; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -165,11 +167,13 @@
9CB3D2B114F45631006283F0 /* MOOPullGesture */ = {
isa = PBXGroup;
children = (
- 9CB3D2B214F45631006283F0 /* ARCHelper.h */,
9CB3D2C314F45701006283F0 /* MOOPullGestureRecognizer.h */,
+ 9CB3D2F414F48A37006283F0 /* MOOPullGestureRecognizerSubclass.h */,
9CB3D2C414F45701006283F0 /* MOOPullGestureRecognizer.m */,
+ 9CB3D2EF14F479EA006283F0 /* MOOTriggerView.h */,
9CB3D2C514F45701006283F0 /* MOORefreshView.h */,
9CB3D2C614F45701006283F0 /* MOORefreshView.m */,
+ 9CB3D2ED14F4779F006283F0 /* Support */,
9CB3D2B714F45631006283F0 /* Resources */,
);
name = MOOPullGesture;
@@ -187,6 +191,14 @@
path = Resources;
sourceTree = "<group>";
};
+ 9CB3D2ED14F4779F006283F0 /* Support */ = {
+ isa = PBXGroup;
+ children = (
+ 9CB3D2F514F48D8F006283F0 /* ARCHelper.h */,
+ );
+ name = Support;
+ sourceTree = "<group>";
+ };
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
@@ -11,8 +11,8 @@
@interface PullToRefreshViewController ()
-- (void)_stateChanged:(UIGestureRecognizer *)gestureRecognizer;
-- (void)_resetPullRecognizer:(MOOPullGestureRecognizer *)gestureRecognizer;
+- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer;
+- (void)_resetPullRecognizer:(UIGestureRecognizer<MOOPullGestureRecognizer> *)gestureRecognizer;
@end
@@ -39,7 +39,7 @@ - (void)viewDidLoad
[super viewDidLoad];
// Add pull gesture recognizer
- [self.tableView addGestureRecognizer:[[MOOPullGestureRecognizer alloc] initWithTarget:self action:@selector(_stateChanged:)]];
+ [self.tableView addGestureRecognizer:[[MOOPullGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
@@ -49,17 +49,17 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface
#pragma mark - MOOPullGestureRecognizer targets
-- (void)_stateChanged:(UIGestureRecognizer *)gestureRecognizer;
+- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer;
{
if (gestureRecognizer.state == UIGestureRecognizerStateRecognized) {
- if ([gestureRecognizer isKindOfClass:[MOOPullGestureRecognizer class]])
+ if ([gestureRecognizer conformsToProtocol:@protocol(MOOPullGestureRecognizer)])
[self performSelector:@selector(_resetPullRecognizer:) withObject:gestureRecognizer afterDelay:2.0];
}
}
-- (void)_resetPullRecognizer:(MOOPullGestureRecognizer *)gestureRecognizer;
+- (void)_resetPullRecognizer:(UIGestureRecognizer<MOOPullGestureRecognizer> *)pullGestureRecognizer;
{
- [gestureRecognizer setRefreshState:MOORefreshIdle];
+ pullGestureRecognizer.pullState = MOOPullIdle;
}
@end
@@ -8,34 +8,40 @@
#import <UIKit/UIKit.h>
-#import "ARCHelper.h"
+#import "Support/ARCHelper.h"
typedef enum {
- MOORefreshIdle = 0,
- MOORefreshTriggered,
- MOORefreshLoading
-} MOORefreshState;
+ MOOPullIdle = 0,
+ MOOPullActive,
+ MOOPullTriggered
+} MOOPullState;
-@protocol MOORefreshView;
+@protocol MOOTriggerView;
-@interface MOOPullGestureRecognizer : UIGestureRecognizer {
- MOORefreshState _refreshState;
- UIView<MOORefreshView> *_triggerView;
+@protocol MOOPullGestureRecognizer <NSObject>
+
+@property (nonatomic, assign) MOOPullState pullState;
+@property (nonatomic, strong, readonly) UIScrollView *scrollView;
+@property (nonatomic, strong) UIView<MOOTriggerView> *triggerView;
+
+@end
+@interface MOOPullGestureRecognizer : UIGestureRecognizer <MOOPullGestureRecognizer> {
+ UIView<MOOTriggerView> *_triggerView;
+
+ BOOL _failed;
+ MOOPullState _pullState;
struct {
BOOL isBoundToScrollView:1;
- } _triggerFlags;
+ } _pullGestureFlags;
}
-@property (nonatomic, assign) MOORefreshState refreshState;
-@property (nonatomic, strong, readonly) UIScrollView *scrollView;
-
-@property (nonatomic, strong) UIView<MOORefreshView> *triggerView;
+@property (nonatomic, assign, getter = isFailed, readonly) BOOL failed;
@end
@interface UIScrollView (MOOPullGestureRecognizer)
-- (MOOPullGestureRecognizer *)refreshGestureRecognizer;
+@property (nonatomic, strong, readonly) UIGestureRecognizer<MOOPullGestureRecognizer> *pullGestureRecognizer;
-@end
+@end
@@ -7,80 +7,98 @@
//
#import "MOOPullGestureRecognizer.h"
+#import "MOOPullGestureRecognizerSubclass.h"
#import <UIKit/UIGestureRecognizerSubclass.h>
+#import "Support/ARCHelper.h"
#import "MOORefreshView.h"
-static NSString * const MOORefreshViewKeyPath = @"view";
+static NSString * const MOOAttachedViewKeyPath = @"view";
@implementation MOOPullGestureRecognizer
+@synthesize pullState = _pullState;
@synthesize triggerView = _triggerView;
-@synthesize refreshState = _refreshState;
+
+@dynamic failed;
+@dynamic scrollView;
- (id)initWithTarget:(id)target action:(SEL)action;
{
if (!(self = [super initWithTarget:target action:action]))
return nil;
+ // Configure KVO
+ [self addObserver:self forKeyPath:MOOAttachedViewKeyPath options:NSKeyValueObservingOptionNew context:NULL];
+
// Create trigger view
self.triggerView = [[MOORefreshView alloc] initWithFrame:CGRectZero];
- // Configure KVO
- [self addObserver:self forKeyPath:MOORefreshViewKeyPath options:NSKeyValueObservingOptionNew context:NULL];
-
return self;
}
- (void)dealloc;
{
- [self removeObserver:self forKeyPath:MOORefreshViewKeyPath];
+ // Clean up KVO
+ [self removeObserver:self forKeyPath:MOOAttachedViewKeyPath];
+
+ // Clean up memory
self.triggerView = nil;
}
-#pragma mark - Getters and setters
+#pragma mark - MOOPullGestureRecognizer methods
-- (void)setRefreshState:(MOORefreshState)refreshState;
+- (BOOL)shouldFail;
{
- if (refreshState == self.refreshState)
- return;
-
- _refreshState = refreshState;
- [self.triggerView transitionToRefreshState:refreshState];
+ return self.pullState == MOOPullTriggered ||
+ self.state == UIGestureRecognizerStateFailed ||
+ !_pullGestureFlags.isBoundToScrollView;
}
-- (UIScrollView *)scrollView;
+#pragma mark - UIGestureRecognizer methods
+
+- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
{
- return (UIScrollView *)self.view;
+ [super touchesBegan:touches withEvent:event];
+
+ if (self.failed = [self shouldFail])
+ return;
}
-- (void)setTriggerView:(UIView<MOORefreshView> *)triggerView;
+- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
{
- if (triggerView == self.triggerView)
+ [super touchesMoved:touches withEvent:event];
+
+ if (self.failed = [self shouldFail])
return;
- [_triggerView removeFromSuperview];
- _triggerView = triggerView;
- [_triggerView transitionToRefreshState:self.refreshState];
+ if (self.scrollView.contentOffset.y < CGRectGetMinY(self.triggerView.frame))
+ self.pullState = MOOPullActive;
+ else if (self.state != UIGestureRecognizerStateRecognized)
+ self.pullState = MOOPullIdle;
}
-#pragma mark - KVO methods
-
-- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
+- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
{
- id newValue = [change valueForKey:NSKeyValueChangeNewKey];
+ [super touchesMoved:touches withEvent:event];
- if ([keyPath isEqualToString:MOORefreshViewKeyPath])
- if ([newValue isKindOfClass:[UIScrollView class]])
- {
- _triggerFlags.isBoundToScrollView = YES;
- [newValue addSubview:self.triggerView];
- [self.triggerView positionInScrollView:newValue];
- } else
- _triggerFlags.isBoundToScrollView = NO;
+ if (self.failed = [self shouldFail])
+ return;
+
+ if (self.pullState == MOOPullActive)
+ {
+ self.pullState = MOOPullTriggered;
+ self.state = UIGestureRecognizerStateRecognized;
+ } else {
+ self.pullState = MOOPullIdle;
+ self.state = UIGestureRecognizerStateFailed;
+ }
+}
+- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
+{
+ self.failed = YES;
}
-#pragma mark - UIGestureRecognizer methods
- (BOOL)canPreventGestureRecognizer:(UIGestureRecognizer *)preventedGestureRecognizer;
{
return NO;
@@ -90,67 +108,74 @@ - (BOOL)canBePreventedByGestureRecognizer:(UIGestureRecognizer *)preventingGestu
return NO;
}
-- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
+#pragma mark - Getters and setters
+
+- (BOOL)isFailed;
{
- if (self.refreshState == MOORefreshLoading || !_triggerFlags.isBoundToScrollView)
- {
- self.state = UIGestureRecognizerStateFailed;
+ return self.state == UIGestureRecognizerStateFailed;
+}
+
+- (void)setFailed:(BOOL)failed;
+{
+ if (failed == self.isFailed)
return;
- }
- [super touchesBegan:touches withEvent:event];
+ if (failed)
+ self.state = UIGestureRecognizerStateFailed;
}
-- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
+- (void)setPullState:(MOOPullState)pullState;
{
- if (self.refreshState == MOORefreshLoading)
- {
- self.state = UIGestureRecognizerStateFailed;
+ if (pullState == self.pullState)
return;
- }
- if (_triggerFlags.isBoundToScrollView)
- if (self.scrollView.contentOffset.y < CGRectGetMinY(self.triggerView.frame))
- self.refreshState = MOORefreshTriggered;
- else if (self.state != UIGestureRecognizerStateRecognized)
- self.refreshState = MOORefreshIdle;
+ _pullState = pullState;
+ [self.triggerView transitionToPullState:pullState];
}
-- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
+- (UIScrollView *)scrollView;
{
- if (self.refreshState == MOORefreshLoading)
- {
- self.state = UIGestureRecognizerStateFailed;
+ return (UIScrollView *)self.view;
+}
+
+- (void)setTriggerView:(UIView<MOOTriggerView> *)triggerView;
+{
+ if (triggerView == self.triggerView)
return;
- }
- if (_triggerFlags.isBoundToScrollView)
- if (self.refreshState == MOORefreshTriggered)
- {
- self.refreshState = MOORefreshLoading;
- self.state = UIGestureRecognizerStateRecognized;
- } else {
- self.refreshState = MOORefreshIdle;
- self.state = UIGestureRecognizerStateFailed;
- }
+ [self.triggerView removeFromSuperview];
+ _triggerView = triggerView;
+ [self.triggerView transitionToPullState:self.pullState];
}
-- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
+#pragma mark - KVO methods
+
+- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
{
- self.state = UIGestureRecognizerStateFailed;
+ id newValue = [change valueForKey:NSKeyValueChangeNewKey];
+
+ if ([keyPath isEqualToString:MOOAttachedViewKeyPath])
+ if ([newValue isKindOfClass:[UIScrollView class]])
+ {
+ _pullGestureFlags.isBoundToScrollView = YES;
+ [newValue addSubview:self.triggerView];
+ [self.triggerView positionInScrollView:newValue];
+ } else {
+ _pullGestureFlags.isBoundToScrollView = NO;
+ }
}
@end
-#pragma mark - UIScrollView category
+#pragma mark - UIScrollView accessory
@implementation UIScrollView (MOOPullGestureRecognizer)
-- (MOOPullGestureRecognizer *)refreshGestureRecognizer;
+- (UIGestureRecognizer<MOOPullGestureRecognizer> *)pullGestureRecognizer;
{
for (UIGestureRecognizer *recognizer in self.gestureRecognizers)
- if ([recognizer isKindOfClass:[MOOPullGestureRecognizer class]])
- return (MOOPullGestureRecognizer *)recognizer;
+ if ([recognizer conformsToProtocol:@protocol(MOOPullGestureRecognizer)])
+ return (UIGestureRecognizer<MOOPullGestureRecognizer> *)recognizer;
return nil;
}
Oops, something went wrong.

0 comments on commit ff12f8f

Please sign in to comment.