Skip to content

Instantly share code, notes, and snippets.

@Shilo Shilo/SHShakeEvent.h
Created Nov 21, 2014

Embed
What would you like to do?
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
You can’t perform that action at this time.