Created
November 21, 2014 12:40
-
-
Save Shilo/71a2e2e2cb679082f544 to your computer and use it in GitHub Desktop.
A Sparrow 2.x extension for handling shake/motion events.
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
// | |
// SHShakeEvent.h | |
// | |
// Created by Shilo White on 11/21/14. | |
// | |
// | |
#import <Sparrow/Sparrow.h> | |
SP_EXTERN NSString *const SHEventTypeShake; | |
typedef NS_ENUM(int, SHShakePhase) | |
{ | |
SHShakePhaseBegan, | |
SHShakePhaseEnded, | |
SHShakePhaseCancelled | |
}; | |
@interface SHShakeEvent : SPEvent | |
- (instancetype)initWithType:(NSString *)type phase:(SHShakePhase)phase; | |
- (instancetype)initWithType:(NSString *)type phase:(SHShakePhase)phase bubbles:(BOOL)bubbles; | |
+ (instancetype)shakeEventWithType:(NSString *)type phase:(SHShakePhase)phase; | |
+ (instancetype)shakeEventWithType:(NSString *)type phase:(SHShakePhase)phase bubbles:(BOOL)bubbles; | |
@property (nonatomic, readonly) SHShakePhase phase; | |
@end |
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
// | |
// SHShakeEvent.m | |
// Barebone | |
// | |
// Created by Shilo White on 11/21/14. | |
// | |
// | |
#import "SHShakeEvent.h" | |
NSString *const SHEventTypeShake = @"SHEventTypeShake"; | |
@implementation SHShakeEvent | |
- (instancetype)initWithType:(NSString *)type phase:(SHShakePhase)phase | |
{ | |
return [self initWithType:type phase:phase bubbles:YES]; | |
} | |
- (instancetype)initWithType:(NSString *)type phase:(SHShakePhase)phase bubbles:(BOOL)bubbles | |
{ | |
if ((self = [super initWithType:type bubbles:bubbles])) | |
{ | |
_phase = phase; | |
} | |
return self; | |
} | |
+ (instancetype)shakeEventWithType:(NSString *)type phase:(SHShakePhase)phase | |
{ | |
return [[self alloc] initWithType:type phase:phase]; | |
} | |
+ (instancetype)shakeEventWithType:(NSString *)type phase:(SHShakePhase)phase bubbles:(BOOL)bubbles | |
{ | |
return [[self alloc] initWithType:type phase:phase bubbles:bubbles]; | |
} | |
- (NSString *)description | |
{ | |
return [NSString stringWithFormat:@"[%@: type=\"%@\", bubbles=%@ phase:%@]", | |
NSStringFromClass([self class]), self.type, | |
self.bubbles ? @"YES" : @"NO", | |
_phase == SHShakePhaseBegan ? @"SHShakePhaseBegan" : _phase == SHShakePhaseEnded ? @"SHShakePhaseEnded" : @"SHShakePhaseCancelled"]; | |
} | |
@end | |
@implementation SPViewController (SHShakeEvent) | |
- (BOOL)canBecomeFirstResponder | |
{ | |
return YES; | |
} | |
- (void)viewDidAppear:(BOOL)animated | |
{ | |
[self becomeFirstResponder]; | |
[super viewDidAppear:animated]; | |
} | |
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event | |
{ | |
if (motion == UIEventSubtypeMotionShake) | |
{ | |
SHShakeEvent *beganEvent = [[SHShakeEvent alloc] initWithType:SHEventTypeShake phase:SHShakePhaseBegan]; | |
[self.root dispatchEvent:beganEvent]; | |
} | |
[super motionBegan:motion withEvent:event]; | |
} | |
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event | |
{ | |
if (motion == UIEventSubtypeMotionShake) | |
{ | |
SHShakeEvent *endedEvent = [[SHShakeEvent alloc] initWithType:SHEventTypeShake phase:SHShakePhaseEnded]; | |
[self.root dispatchEvent:endedEvent]; | |
} | |
[super motionEnded:motion withEvent:event]; | |
} | |
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event | |
{ | |
if (motion == UIEventSubtypeMotionShake) | |
{ | |
SHShakeEvent *cancelledEvent = [[SHShakeEvent alloc] initWithType:SHEventTypeShake phase:SHShakePhaseCancelled]; | |
[self.root dispatchEvent:cancelledEvent]; | |
} | |
[super motionCancelled:motion withEvent:event]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment