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.
		
		
		
		
		
			
		
			
	
	
		
			71 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Swift
		
	
		
		
			
		
	
	
			71 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Swift
		
	
| 
								 
											7 years ago
										 
									 | 
							
								//
							 | 
						||
| 
								 | 
							
								//  Copyright (c) 2019 Open Whisper Systems. All rights reserved.
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								import UIKit
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								// This GR:
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								// * Tries to fail quickly to avoid conflicts with other GRs, especially pans/swipes.
							 | 
						||
| 
								 | 
							
								// * Captures a bunch of useful "pan state" that makes using this GR much easier
							 | 
						||
| 
								 | 
							
								//   than UIPanGestureRecognizer.
							 | 
						||
| 
								 | 
							
								public class ImageEditorPanGestureRecognizer: UIPanGestureRecognizer {
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public weak var referenceView: UIView?
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								    // Capture the location history of this gesture.
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								    public var locationHistory = [CGPoint]()
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								    public var locationFirst: CGPoint? {
							 | 
						||
| 
								 | 
							
								        return locationHistory.first
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								    // MARK: - Touch Handling
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    @objc
							 | 
						||
| 
								 | 
							
								    public override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								        updateLocationHistory(event: event)
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								        super.touchesBegan(touches, with: event)
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    @objc
							 | 
						||
| 
								 | 
							
								    public override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								        updateLocationHistory(event: event)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								        super.touchesMoved(touches, with: event)
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    @objc
							 | 
						||
| 
								 | 
							
								    public override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
							 | 
						||
| 
								 | 
							
								        updateLocationHistory(event: event)
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								        super.touchesEnded(touches, with: event)
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    private func updateLocationHistory(event: UIEvent) {
							 | 
						||
| 
								 | 
							
								        guard let touches = event.allTouches,
							 | 
						||
| 
								 | 
							
								            touches.count > 0 else {
							 | 
						||
| 
								 | 
							
								                owsFailDebug("no touches.")
							 | 
						||
| 
								 | 
							
								                return
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								        guard let referenceView = referenceView else {
							 | 
						||
| 
								 | 
							
								            owsFailDebug("Missing view")
							 | 
						||
| 
								 | 
							
								            return
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								        // Find the centroid.
							 | 
						||
| 
								 | 
							
								        var location = CGPoint.zero
							 | 
						||
| 
								 | 
							
								        for touch in touches {
							 | 
						||
| 
								 | 
							
								            location = location.plus(touch.location(in: referenceView))
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        location = location.times(CGFloat(1) / CGFloat(touches.count))
							 | 
						||
| 
								 | 
							
								        locationHistory.append(location)
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public override func reset() {
							 | 
						||
| 
								 | 
							
								        super.reset()
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								        locationHistory.removeAll()
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 | 
							
								}
							 |