diff --git a/GHContextMenu/GHContextMenuView.h b/GHContextMenu/GHContextMenuView.h index 51975b6..4afe3d1 100644 --- a/GHContextMenu/GHContextMenuView.h +++ b/GHContextMenu/GHContextMenuView.h @@ -26,6 +26,7 @@ typedef NS_ENUM(NSInteger, GHContextMenuActionType){ @property (nonatomic, assign) GHContextMenuActionType menuActionType; - (void) longPressDetected:(UIGestureRecognizer*) gestureRecognizer; +- (void) deepPressDetected:(UIGestureRecognizer*) gestureRecognizer; @end @@ -45,3 +46,14 @@ typedef NS_ENUM(NSInteger, GHContextMenuActionType){ - (void) didSelectItemAtIndex:(NSInteger) selectedIndex forMenuAtPoint:(CGPoint) point; @end + + +/* + * 3D touch GestureRecognizer + */ +@interface DeepGestureRecognizer : UIGestureRecognizer +@property (nonatomic, readonly) CGFloat threshold; +@property (nonatomic) BOOL vibrateOn3DPress; +@property (nonatomic) BOOL dragMode; +- (instancetype)initWithTarget:(id)target action:(SEL)action threshold:(CGFloat)threshold; +@end diff --git a/GHContextMenu/GHContextMenuView.m b/GHContextMenu/GHContextMenuView.m index bf7c93a..33e91c4 100644 --- a/GHContextMenu/GHContextMenuView.m +++ b/GHContextMenu/GHContextMenuView.m @@ -178,6 +178,11 @@ - (void) longPressDetected:(UIGestureRecognizer*) gestureRecognizer } } +- (void)deepPressDetected:(UIGestureRecognizer*) gestureRecognizer +{ + [self longPressDetected:gestureRecognizer]; +} + - (void) showMenu { @@ -487,3 +492,60 @@ - (void)drawRect:(CGRect)rect } } @end + + +/* + * 3D touch GestureRecognizer + */ +#import +#import + +@interface DeepGestureRecognizer () { + BOOL is3DPress; +} + +@end + +@implementation DeepGestureRecognizer +- (instancetype)initWithTarget:(id)target action:(SEL)action threshold:(CGFloat)threshold { + if (self == [super initWithTarget:target action:action]) { + _threshold = threshold; + _vibrateOn3DPress = YES; + _dragMode = YES; + is3DPress = NO; + } + return self; +} +- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { + [self handleTouch:[[touches allObjects] objectAtIndex:0]]; +} +- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { + [self handleTouch:[[touches allObjects] objectAtIndex:0]]; +} +- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { + [super touchesEnded:touches withEvent:event]; + if (_dragMode == YES) { + self.state = UIGestureRecognizerStateEnded; + } else { + self.state = (is3DPress ? UIGestureRecognizerStateEnded : UIGestureRecognizerStateFailed); + } + is3DPress = NO; +} +- (void)handleTouch:(UITouch*)touch { + // if (touch.force != 0 && touch.maximumPossibleForce != 0) { + // return; + // } + if (!is3DPress && (touch.force/touch.maximumPossibleForce) >= _threshold) { + + if (_vibrateOn3DPress == YES) { + AudioServicesPlayAlertSound(kSystemSoundID_Vibrate); + } + + self.state = UIGestureRecognizerStateBegan; + is3DPress = YES; + } else if (is3DPress && (touch.force/touch.maximumPossibleForce) < _threshold && _dragMode == NO) { + self.state = UIGestureRecognizerStateEnded; + is3DPress = NO; + } +} +@end diff --git a/README.md b/README.md index 6ef172f..1cddee1 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,15 @@ Sample app contains examples of how to add context menu for UIView and UICollect UILongPressGestureRecognizer* _longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:overlay action:@selector(longPressDetected:)]; [self.view addGestureRecognizer:_longPressRecognizer]; +// 3D touch + GHContextMenuView* overlay = [[GHContextMenuView alloc] init]; + overlay.dataSource = self; + overlay.delegate = self; + + DeepGestureRecognizer *_deepGestureRecognizer = [[DeepGestureRecognizer alloc] initWithTarget:overlay action:@selector(deepPressDetected:) threshold:1.0]; + [self.view addGestureRecognizer:_deepGestureRecognizer]; + + // Implementing data source methods - (NSInteger) numberOfMenuItems {