VLCSiriRemoteGestureRecognizer.m 4.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
/*****************************************************************************
 * VLC for iOS
 *****************************************************************************
 * Copyright (c) 2015 VideoLAN. All rights reserved.
 * $Id$
 *
 * Authors: Tobias Conradi <videolan # tobias-conradi.de>
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/

#import "VLCSiriRemoteGestureRecognizer.h"
#import <UIKit/UIGestureRecognizerSubclass.h>

@interface UIEvent (VLCDigitizerLocation)
- (CGPoint)vlc_digitizerLocation;
@end

@interface VLCSiriRemoteGestureRecognizer ()
{
	NSTimer *_longPressTimer;
}
@end

@implementation VLCSiriRemoteGestureRecognizer
@dynamic delegate;

- (instancetype)initWithTarget:(id)target action:(SEL)action
{
    self = [super initWithTarget:target action:action];
    if (self) {
        self.allowedTouchTypes = @[@(UITouchTypeIndirect)];
        self.allowedPressTypes = @[@(UIPressTypeSelect)];
		self.minLongPressDuration = 0.5;
        self.cancelsTouchesInView = NO;
    }
    return self;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.state = UIGestureRecognizerStateBegan;
    [self updateTouchLocationWithEvent:event];
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self updateTouchLocationWithEvent:event];
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self updateTouchLocation:VLCSiriRemoteTouchLocationUnknown];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self updateTouchLocation:VLCSiriRemoteTouchLocationUnknown];
}

- (void)updateTouchLocationWithEvent:(UIEvent *)event
{
    CGPoint digitizerLocation = [event vlc_digitizerLocation];
    VLCSiriRemoteTouchLocation location = VLCSiriRemoteTouchLocationUnknown;
    if (digitizerLocation.x <= 0.2) {
        location = VLCSiriRemoteTouchLocationLeft;
    } else if (0.8 <= digitizerLocation.x) {
        location = VLCSiriRemoteTouchLocationRight;
    }
    [self updateTouchLocation:location];
}

- (void)updateTouchLocation:(VLCSiriRemoteTouchLocation)location
{
	if (_touchLocation == location) {
		return;
	}

	_touchLocation = location;

    self.state = UIGestureRecognizerStateChanged;
}

- (void)reset
{
	_click = NO;
	_touchLocation = VLCSiriRemoteTouchLocationUnknown;
	_longPress = NO;
	[_longPressTimer invalidate];
	_longPressTimer = nil;
    [super reset];
}

- (void)longPressTimerFired
{
	if (_click && (self.state == UIGestureRecognizerStateBegan || self.state == UIGestureRecognizerStateChanged)) {
		_longPress = YES;
        self.state = UIGestureRecognizerStateChanged;
    }
}

- (void)pressesBegan:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event
{
	if ([self.allowedPressTypes containsObject:@(presses.anyObject.type)]) {
		_click = YES;
		_longPressTimer = [NSTimer scheduledTimerWithTimeInterval:self.minLongPressDuration target:self selector:@selector(longPressTimerFired) userInfo:nil repeats:NO];
		self.state = UIGestureRecognizerStateChanged;
	}
}
- (void)pressesChanged:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event
{
	self.state = UIGestureRecognizerStateChanged;
}
- (void)pressesCancelled:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event
{
	self.state = UIGestureRecognizerStateCancelled;
}
- (void)pressesEnded:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event
{
	if (_click) {
		self.state = UIGestureRecognizerStateEnded;
	}
}
@end


@implementation UIEvent (VLCDigitizerLocation)

- (CGPoint)vlc_digitizerLocation
{
    /*
     * !!! Attention: We are using private API !!!
     * !!!  Might break in any future release  !!!
     *
     * The digitizer location is the absolut location of the touch on the touch pad.
     * The location is in a 0,0 (top left) to 1,1 (bottom right) coordinate system.
     */
    NSString *key = [@"digitiz" stringByAppendingString:@"erLocation"];
    NSNumber *value = [self valueForKey:key];
    if ([value isKindOfClass:[NSValue class]]) {
        return [value CGPointValue];
    }
    // default to center position as undefined position
    return CGPointMake(0.5,0.5);
}

@end