Skip to content

Instantly share code, notes, and snippets.

@Shilo
Created November 21, 2014 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shilo/71a2e2e2cb679082f544 to your computer and use it in GitHub Desktop.
Save Shilo/71a2e2e2cb679082f544 to your computer and use it in GitHub Desktop.
A Sparrow 2.x extension for handling shake/motion events.
//
// 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
//
// 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