NewsBlur/clients/ios/Classes/EventWindow.m

91 lines
3.3 KiB
Mathematica
Raw Normal View History

//
// EventWindow.m
// NewsBlur
//
// Created by Samuel Clay on 9/17/14.
// Copyright (c) 2014 NewsBlur. All rights reserved.
//
#import "EventWindow.h"
@implementation EventWindow
2014-09-18 16:55:37 -07:00
@synthesize tapDetectingView;
- (void)tapAndHoldAction:(NSTimer*)timer {
contextualMenuTimer = nil;
NSDictionary *coord = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:tapLocation.x],@"x",
[NSNumber numberWithFloat:tapLocation.y],@"y",nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"TapAndHoldNotification" object:coord];
}
2014-09-18 16:55:37 -07:00
- (void)tapAction {
contextualMenuTimer = nil;
NSDictionary *coord = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:tapLocation.x],@"x",
[NSNumber numberWithFloat:tapLocation.y],@"y",nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"TapNotification" object:coord];
}
- (void)sendEvent:(UIEvent *)event {
[super sendEvent:event]; // Call super to make sure the event is processed as usual
2014-09-18 16:55:37 -07:00
if (!tapDetectingView) return;
NSSet *touches = [event touchesForWindow:self];
2014-09-18 16:55:37 -07:00
if ([touches count] == 1) { // We're only interested in one-finger events
UITouch *touch = [touches anyObject];
2014-09-18 16:55:37 -07:00
if (touch.view != nil && ![touch.view isDescendantOfView:tapDetectingView]) {
return;
}
switch ([touch phase]) {
case UITouchPhaseBegan: // A finger touched the screen
tapLocation = [touch locationInView:self];
[contextualMenuTimer invalidate];
2014-09-18 16:55:37 -07:00
unmoved = YES;
contextualMenuTimer = [NSTimer scheduledTimerWithTimeInterval:0.7
target:self selector:@selector(tapAndHoldAction:)
userInfo:nil repeats:NO];
break;
case UITouchPhaseStationary:
break;
case UITouchPhaseEnded:
2014-09-18 16:55:37 -07:00
[contextualMenuTimer invalidate];
contextualMenuTimer = nil;
2014-09-18 16:55:37 -07:00
if (unmoved) {
[self tapAction];
}
break;
case UITouchPhaseMoved: // Changes in force are also "moves"
if (CGPointEqualToPoint([touch locationInView:self], tapLocation)) {
if ([touch respondsToSelector:@selector(force)] && (touch.force / touch.maximumPossibleForce) > 0.75) {
[contextualMenuTimer invalidate];
contextualMenuTimer = nil;
[self tapAndHoldAction:nil];
}
break;
}
case UITouchPhaseCancelled:
2014-09-18 16:55:37 -07:00
unmoved = NO;
[contextualMenuTimer invalidate];
contextualMenuTimer = nil;
break;
default:
break;
}
} else { // Multiple fingers are touching the screen
2014-09-18 16:55:37 -07:00
unmoved = NO;
[contextualMenuTimer invalidate];
contextualMenuTimer = nil;
}
}
2019-01-16 19:15:07 -08:00
@end