VLCSiriRemoteGestureRecognizer.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*****************************************************************************
  2. * VLC for iOS
  3. *****************************************************************************
  4. * Copyright (c) 2015 VideoLAN. All rights reserved.
  5. * $Id$
  6. *
  7. * Authors: Tobias Conradi <videolan # tobias-conradi.de>
  8. *
  9. * Refer to the COPYING file of the official project for license.
  10. *****************************************************************************/
  11. #import "VLCSiriRemoteGestureRecognizer.h"
  12. #import <UIKit/UIGestureRecognizerSubclass.h>
  13. @interface UIEvent (VLCDigitizerLocation)
  14. - (CGPoint)vlc_digitizerLocation;
  15. @end
  16. @interface VLCSiriRemoteGestureRecognizer ()
  17. {
  18. NSTimer *_longPressTimer;
  19. }
  20. @end
  21. @implementation VLCSiriRemoteGestureRecognizer
  22. @dynamic delegate;
  23. - (instancetype)initWithTarget:(id)target action:(SEL)action
  24. {
  25. self = [super initWithTarget:target action:action];
  26. if (self) {
  27. self.allowedTouchTypes = @[@(UITouchTypeIndirect)];
  28. self.allowedPressTypes = @[@(UIPressTypeSelect)];
  29. self.minLongPressDuration = 0.5;
  30. self.cancelsTouchesInView = NO;
  31. }
  32. return self;
  33. }
  34. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
  35. {
  36. self.state = UIGestureRecognizerStateBegan;
  37. [self updateTouchLocationWithEvent:event];
  38. }
  39. - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
  40. {
  41. [self updateTouchLocationWithEvent:event];
  42. }
  43. - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
  44. {
  45. [self updateTouchLocation:VLCSiriRemoteTouchLocationUnknown];
  46. }
  47. - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
  48. {
  49. [self updateTouchLocation:VLCSiriRemoteTouchLocationUnknown];
  50. }
  51. - (void)updateTouchLocationWithEvent:(UIEvent *)event
  52. {
  53. CGPoint digitizerLocation = [event vlc_digitizerLocation];
  54. VLCSiriRemoteTouchLocation location = VLCSiriRemoteTouchLocationUnknown;
  55. if (digitizerLocation.x <= 0.2) {
  56. location = VLCSiriRemoteTouchLocationLeft;
  57. } else if (0.8 <= digitizerLocation.x) {
  58. location = VLCSiriRemoteTouchLocationRight;
  59. }
  60. [self updateTouchLocation:location];
  61. }
  62. - (void)updateTouchLocation:(VLCSiriRemoteTouchLocation)location
  63. {
  64. if (_touchLocation == location) {
  65. return;
  66. }
  67. _touchLocation = location;
  68. self.state = UIGestureRecognizerStateChanged;
  69. }
  70. - (void)reset
  71. {
  72. _click = NO;
  73. _touchLocation = VLCSiriRemoteTouchLocationUnknown;
  74. _longPress = NO;
  75. [_longPressTimer invalidate];
  76. _longPressTimer = nil;
  77. [super reset];
  78. }
  79. - (void)longPressTimerFired
  80. {
  81. if (_click && (self.state == UIGestureRecognizerStateBegan || self.state == UIGestureRecognizerStateChanged)) {
  82. _longPress = YES;
  83. self.state = UIGestureRecognizerStateChanged;
  84. }
  85. }
  86. - (void)pressesBegan:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event
  87. {
  88. if ([self.allowedPressTypes containsObject:@(presses.anyObject.type)]) {
  89. _click = YES;
  90. _longPressTimer = [NSTimer scheduledTimerWithTimeInterval:self.minLongPressDuration target:self selector:@selector(longPressTimerFired) userInfo:nil repeats:NO];
  91. self.state = UIGestureRecognizerStateChanged;
  92. }
  93. }
  94. - (void)pressesChanged:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event
  95. {
  96. self.state = UIGestureRecognizerStateChanged;
  97. }
  98. - (void)pressesCancelled:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event
  99. {
  100. self.state = UIGestureRecognizerStateCancelled;
  101. }
  102. - (void)pressesEnded:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event
  103. {
  104. if (_click) {
  105. self.state = UIGestureRecognizerStateEnded;
  106. }
  107. }
  108. @end
  109. @implementation UIEvent (VLCDigitizerLocation)
  110. - (CGPoint)vlc_digitizerLocation
  111. {
  112. /*
  113. * !!! Attention: We are using private API !!!
  114. * !!! Might break in any future release !!!
  115. *
  116. * The digitizer location is the absolut location of the touch on the touch pad.
  117. * The location is in a 0,0 (top left) to 1,1 (bottom right) coordinate system.
  118. */
  119. NSString *key = [@"digitiz" stringByAppendingString:@"erLocation"];
  120. NSNumber *value = [self valueForKey:key];
  121. if ([value isKindOfClass:[NSValue class]]) {
  122. return [value CGPointValue];
  123. }
  124. // default to center position as undefined position
  125. return CGPointMake(0.5,0.5);
  126. }
  127. @end