mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			133 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			Matlab
		
	
		
		
			
		
	
	
			133 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			Matlab
		
	
| 
											9 years ago
										 | // | ||
| 
											7 years ago
										 | //  Copyright (c) 2018 Open Whisper Systems. All rights reserved. | ||
| 
											9 years ago
										 | // | ||
|  | 
 | ||
|  | #import "OWSAnyTouchGestureRecognizer.h" | ||
|  | #import <UIKit/UIGestureRecognizerSubclass.h> | ||
|  | 
 | ||
| 
											7 years ago
										 | NS_ASSUME_NONNULL_BEGIN | ||
|  | 
 | ||
|  | NSString *NSStringForUIGestureRecognizerState(UIGestureRecognizerState state) | ||
|  | { | ||
|  |     switch (state) { | ||
|  |         case UIGestureRecognizerStatePossible: | ||
|  |             return @"UIGestureRecognizerStatePossible"; | ||
|  |         case UIGestureRecognizerStateBegan: | ||
|  |             return @"UIGestureRecognizerStateBegan"; | ||
|  |         case UIGestureRecognizerStateChanged: | ||
|  |             return @"UIGestureRecognizerStateChanged"; | ||
|  |         case UIGestureRecognizerStateEnded: | ||
|  |             return @"UIGestureRecognizerStateEnded"; | ||
|  |         case UIGestureRecognizerStateCancelled: | ||
|  |             return @"UIGestureRecognizerStateCancelled"; | ||
|  |         case UIGestureRecognizerStateFailed: | ||
|  |             return @"UIGestureRecognizerStateFailed"; | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
| 
											9 years ago
										 | @implementation OWSAnyTouchGestureRecognizer | ||
|  | 
 | ||
|  | - (BOOL)canPreventGestureRecognizer:(UIGestureRecognizer *)preventedGestureRecognizer | ||
|  | { | ||
|  |     return NO; | ||
|  | } | ||
|  | 
 | ||
|  | - (BOOL)canBePreventedByGestureRecognizer:(UIGestureRecognizer *)preventedGestureRecognizer | ||
|  | { | ||
|  |     return NO; | ||
|  | } | ||
|  | 
 | ||
|  | - (BOOL)shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer | ||
|  | { | ||
|  |     return NO; | ||
|  | } | ||
|  | 
 | ||
|  | - (BOOL)shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer | ||
|  | { | ||
|  |     return YES; | ||
|  | } | ||
|  | 
 | ||
|  | - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event | ||
|  | { | ||
|  |     [super touchesBegan:touches withEvent:event]; | ||
|  | 
 | ||
|  |     if (self.state == UIGestureRecognizerStatePossible && [self isValidTouch:touches event:event]) { | ||
|  |         self.state = UIGestureRecognizerStateRecognized; | ||
|  |     } else { | ||
|  |         self.state = UIGestureRecognizerStateFailed; | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
| 
											9 years ago
										 | - (UIView *)rootViewInViewHierarchy:(UIView *)view | ||
|  | { | ||
| 
											7 years ago
										 |     OWSAssertDebug(view); | ||
| 
											9 years ago
										 |     UIResponder *responder = view; | ||
|  |     UIView *lastView = nil; | ||
|  |     while (responder) { | ||
|  |         if ([responder isKindOfClass:[UIView class]]) { | ||
|  |             lastView = (UIView *)responder; | ||
|  |         } | ||
|  |         responder = [responder nextResponder]; | ||
|  |     } | ||
|  |     return lastView; | ||
|  | } | ||
|  | 
 | ||
| 
											9 years ago
										 | - (BOOL)isValidTouch:(NSSet<UITouch *> *)touches event:(UIEvent *)event | ||
|  | { | ||
|  |     if (event.allTouches.count > 1) { | ||
|  |         return NO; | ||
|  |     } | ||
|  |     if (touches.count != 1) { | ||
|  |         return NO; | ||
|  |     } | ||
|  | 
 | ||
|  |     UITouch *touch = touches.anyObject; | ||
|  |     CGPoint location = [touch locationInView:self.view]; | ||
|  |     if (!CGRectContainsPoint(self.view.bounds, location)) { | ||
|  |         return NO; | ||
|  |     } | ||
|  | 
 | ||
|  |     if ([self subviewControlOfView:self.view containsTouch:touch]) { | ||
|  |         return NO; | ||
|  |     } | ||
|  | 
 | ||
| 
											9 years ago
										 |     // Ignore touches that start near the top or bottom edge of the screen; | ||
|  |     // they may be a system edge swipe gesture. | ||
| 
											9 years ago
										 |     UIView *rootView = [self rootViewInViewHierarchy:self.view]; | ||
|  |     CGPoint rootLocation = [touch locationInView:rootView]; | ||
|  |     CGFloat distanceToTopEdge = MAX(0, rootLocation.y); | ||
|  |     CGFloat distanceToBottomEdge = MAX(0, rootView.bounds.size.height - rootLocation.y); | ||
| 
											9 years ago
										 |     CGFloat distanceToNearestEdge = MIN(distanceToTopEdge, distanceToBottomEdge); | ||
|  |     CGFloat kSystemEdgeSwipeTolerance = 50.f; | ||
|  |     if (distanceToNearestEdge < kSystemEdgeSwipeTolerance) { | ||
|  |         return NO; | ||
|  |     } | ||
|  | 
 | ||
| 
											9 years ago
										 |     return YES; | ||
|  | } | ||
|  | 
 | ||
|  | - (BOOL)subviewControlOfView:(UIView *)superview containsTouch:(UITouch *)touch | ||
|  | { | ||
|  |     for (UIView *subview in superview.subviews) { | ||
|  |         if (subview.hidden || !subview.userInteractionEnabled) { | ||
|  |             continue; | ||
|  |         } | ||
|  |         CGPoint location = [touch locationInView:subview]; | ||
|  |         if (!CGRectContainsPoint(subview.bounds, location)) { | ||
|  |             continue; | ||
|  |         } | ||
|  |         if ([subview isKindOfClass:[UIControl class]]) { | ||
|  |             return YES; | ||
|  |         } | ||
|  |         if ([self subviewControlOfView:subview containsTouch:touch]) { | ||
|  |             return YES; | ||
|  |         } | ||
|  |     } | ||
|  | 
 | ||
|  |     return NO; | ||
|  | } | ||
|  | 
 | ||
|  | @end | ||
| 
											7 years ago
										 | 
 | ||
|  | NS_ASSUME_NONNULL_END |