Skip to content

Commit

Permalink
Gestures cleanup
Browse files Browse the repository at this point in the history
- iOS10 changes.
- Gesture functions cleanup.
- Improved tap functionality, got rid of long press.
  • Loading branch information
Carlos Cabanero committed Sep 19, 2016
1 parent 93b2097 commit 13f43f2
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 85 deletions.
38 changes: 25 additions & 13 deletions Blink/SpaceController.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
@interface SpaceController () <UIPageViewControllerDataSource, UIPageViewControllerDelegate,
UIGestureRecognizerDelegate, TermControlDelegate>

@property UITapGestureRecognizer *twoFingersTap;
@property UIPanGestureRecognizer *twoFingersDrag;
@property (nonatomic, readonly) UIPageViewController *viewportsController;
@property (nonatomic, readonly) NSMutableArray *viewports;
@property (readonly) TermController *currentTerm;
Expand Down Expand Up @@ -124,25 +126,35 @@ - (void)registerForKeyboardNotifications

- (void)addGestures
{
UITapGestureRecognizer *twoFingersTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTwoFingersTap:)];
[twoFingersTap setNumberOfTouchesRequired:2];
[twoFingersTap setNumberOfTapsRequired:1];
[self.view addGestureRecognizer:twoFingersTap];

UIPanGestureRecognizer *twoFingersDrag = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleTwoFingersDrag:)];
[twoFingersDrag setMinimumNumberOfTouches:2];
[twoFingersDrag setMaximumNumberOfTouches:2];
twoFingersDrag.delegate = self;
[self.view addGestureRecognizer:twoFingersDrag];
_twoFingersTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTwoFingersTap:)];
[_twoFingersTap setNumberOfTouchesRequired:2];
[_twoFingersTap setNumberOfTapsRequired:1];
_twoFingersTap.delegate = self;
[self.view addGestureRecognizer:_twoFingersTap];

_twoFingersDrag = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleTwoFingersDrag:)];
[_twoFingersDrag setMinimumNumberOfTouches:2];
[_twoFingersDrag setMaximumNumberOfTouches:2];
_twoFingersDrag.delegate = self;
[self.view addGestureRecognizer:_twoFingersDrag];
}

#pragma mark Events
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(nonnull UIGestureRecognizer *)otherGestureRecognizer
{
if (gestureRecognizer == self.twoFingersTap && [otherGestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
return YES;
}
return NO;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]]) {
return NO;
if (gestureRecognizer == _twoFingersDrag && ![otherGestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]]) {
return YES;
}
return YES;

return NO;
}

// The Space will be responsible to accommodate the work environment for widgets, adjusting the size, making sure it doesn't overlap content,
Expand Down
129 changes: 57 additions & 72 deletions Blink/TermView.m
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ + (NSString *)FKEY:(NSInteger)number
@end

@interface TerminalView () <UIKeyInput, UIGestureRecognizerDelegate, WKScriptMessageHandler>
@property UITapGestureRecognizer *tapBackground;
@property UILongPressGestureRecognizer *longPressBackground;
@property UIPinchGestureRecognizer *pinchGesture;
@end

@implementation TerminalView {
Expand All @@ -203,7 +206,6 @@ @implementation TerminalView {
NSMutableArray *_kbdCommands;
SmartKeys *_smartKeys;
UIView *cover;
UIPinchGestureRecognizer *_pinchGesture;
NSTimer *_pinchSamplingTimer;
BOOL _raw;
BOOL _inputEnabled;
Expand All @@ -218,46 +220,51 @@ - (id)initWithFrame:(CGRect)frame
self = [super initWithFrame:frame];

if (self) {
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
[configuration.userContentController addScriptMessageHandler:self name:@"interOp"];

_webView = [[WKWebView alloc] initWithFrame:self.frame configuration:configuration];
_webView.opaque = NO;
_inputEnabled = YES;
self.inputAssistantItem.leadingBarButtonGroups = @[];
self.inputAssistantItem.trailingBarButtonGroups = @[];

[self addWebView];
[self resetDefaultControlKeys];
[self addGestures];
[self configureNotifications];
}

[self addSubview:_webView];
return self;
}

UITapGestureRecognizer *tapBackground = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(activeControl:)];
[tapBackground setNumberOfTapsRequired:1];
tapBackground.delegate = self;
_dismissInput = YES;
_inputEnabled = YES;
[self addGestureRecognizer:tapBackground];
- (void)addWebView
{
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
[configuration.userContentController addScriptMessageHandler:self name:@"interOp"];

_webView = [[WKWebView alloc] initWithFrame:self.frame configuration:configuration];
[self addSubview:_webView];

UILongPressGestureRecognizer *longPressBackground = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:nil];
[longPressBackground setNumberOfTapsRequired:1];
longPressBackground.delegate = self;
// _dismissInput = YES;
[self addGestureRecognizer:tapBackground];
_webView.opaque = NO;
_webView.translatesAutoresizingMaskIntoConstraints = NO;
[_webView.topAnchor constraintEqualToAnchor:self.topAnchor].active = YES;
[_webView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor].active = YES;
[_webView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor].active = YES;
[_webView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor].active = YES;
}

- (void)addGestures
{
_tapBackground = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(activeControl:)];
[_tapBackground setNumberOfTapsRequired:1];
_tapBackground.delegate = self;
[self addGestureRecognizer:_tapBackground];

_pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
_pinchGesture.delegate = self;
[self addGestureRecognizer:_pinchGesture];
}

self.inputAssistantItem.leadingBarButtonGroups = @[];
self.inputAssistantItem.trailingBarButtonGroups = @[];

_webView.translatesAutoresizingMaskIntoConstraints = NO;

[_webView.topAnchor constraintEqualToAnchor:self.topAnchor].active = YES;
[_webView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor].active = YES;
[_webView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor].active = YES;
[_webView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor].active = YES;

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
return self;
- (void)configureNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)resetDefaultControlKeys
Expand Down Expand Up @@ -398,24 +405,20 @@ - (void)keyboardWillShow:(NSNotification *)sender
}
}

// WKWebView has its own gestures going on, so we have to handle them properly and only use ours when those fail.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(nonnull UIGestureRecognizer *)otherGestureRecognizer
{
if ([otherGestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
// Tap should always enable this control too.
[otherGestureRecognizer requireGestureRecognizerToFail:gestureRecognizer];
_dismissInput = NO;
}
if ([otherGestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) {
[_webView becomeFirstResponder];
[gestureRecognizer requireGestureRecognizerToFail:otherGestureRecognizer];
if (gestureRecognizer == self.pinchGesture && [otherGestureRecognizer isKindOfClass:[UISwipeGestureRecognizer class]]) {
return YES;
}
return NO;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
if ([otherGestureRecognizer isKindOfClass:[UISwipeGestureRecognizer class]] &&
[gestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]]) {
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
if (gestureRecognizer == self.tapBackground && [otherGestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
// We cancel the one from the WebView from executing, as it will wait for this one to fail.
// We return yes, to make sure that is understood.
[otherGestureRecognizer requireGestureRecognizerToFail:gestureRecognizer];
return YES;
}

Expand Down Expand Up @@ -463,19 +466,6 @@ - (void)handlePinch:(UIPinchGestureRecognizer *)gestureRecognizer
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
[_pinchSamplingTimer invalidate];
}
//[_webView evaluateJavaScript:[NSString stringWithFormat:@"scaleTerm(%f);", scale] completionHandler:nil];


// _webView.transform = CGAffineTransformScale(gestureRecognizer.view.transform, 1/gestureRecognizer.scale, 1/gestureRecognizer.scale);
// CGFloat scale = round(1.0 - (0.04 * (_lastPinchScale - gestureRecognizer.velocity)) * 100) / 100.0;
//
// if (gestureRecognizer.scale < 0.5 || gestureRecognizer.scale > 2.0)
// return;
// if (scale != _lastPinchScale) {
// NSLog(@"%f", scale);
// [_webView evaluateJavaScript:[NSString stringWithFormat:@"scaleTerm(%f);", scale] completionHandler:nil];
// _lastPinchScale = scale;
// }
}

- (void)pinchSampling:(NSTimer *)timer
Expand All @@ -485,25 +475,20 @@ - (void)pinchSampling:(NSTimer *)timer

- (BOOL)canBecomeFirstResponder
{
if (!_inputEnabled) {
return NO;
}

return YES;
}

- (BOOL)canResignFirstResponder
{
// Make sure this control cannot resign in favor of the WKWebView during a tap.
// if (!_dismissInput) {
// _dismissInput = YES;
// return NO;
// }
return YES;
}

- (BOOL)becomeFirstResponder
{
if (!_inputEnabled) {
return NO;
}

if (!_smartKeys) {
_smartKeys = [[SmartKeys alloc] init];
}
Expand Down Expand Up @@ -816,11 +801,11 @@ - (void)fkeySeq:(UIKeyCommand *)cmd
// Cmd+c
- (void)copy:(id)sender
{
if ([sender isKindOfClass:[UIMenuController class]]) {
[_webView copy:sender];
} else {
// if ([sender isKindOfClass:[UIMenuController class]]) {
// [_webView copy:sender];
// } else {
[_delegate write:[CC CTRL:@"c"]];
}
// }
}
// Cmd+x
- (void)cut:(id)sender
Expand Down Expand Up @@ -860,7 +845,7 @@ - (void)toggleUnderline:(id)sender
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if ([sender isKindOfClass:[UIMenuController class]]) {
// The menu can only perform copy and paste methods
// The menu can only perform paste methods
if (action == @selector(paste:)) {
return YES;
}
Expand Down

0 comments on commit 13f43f2

Please sign in to comment.