Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions GHContextMenu/GHContextMenuView.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ typedef NS_ENUM(NSInteger, GHContextMenuActionType){
@property (nonatomic, assign) GHContextMenuActionType menuActionType;

- (void) longPressDetected:(UIGestureRecognizer*) gestureRecognizer;
- (void) deepPressDetected:(UIGestureRecognizer*) gestureRecognizer;

@end

Expand All @@ -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
62 changes: 62 additions & 0 deletions GHContextMenu/GHContextMenuView.m
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ - (void) longPressDetected:(UIGestureRecognizer*) gestureRecognizer
}
}

- (void)deepPressDetected:(UIGestureRecognizer*) gestureRecognizer
{
[self longPressDetected:gestureRecognizer];
}

- (void) showMenu
{

Expand Down Expand Up @@ -487,3 +492,60 @@ - (void)drawRect:(CGRect)rect
}
}
@end


/*
* 3D touch GestureRecognizer
*/
#import <AudioToolbox/AudioToolbox.h>
#import <UIKit/UIGestureRecognizerSubclass.h>

@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<UITouch *> *)touches withEvent:(UIEvent *)event {
[self handleTouch:[[touches allObjects] objectAtIndex:0]];
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self handleTouch:[[touches allObjects] objectAtIndex:0]];
}
- (void)touchesEnded:(NSSet<UITouch *> *)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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down