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.
		
		
		
		
		
			
		
			
				
	
	
		
			69 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Swift
		
	
			
		
		
	
	
			69 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Swift
		
	
//  Copyright (c) 2019 Open Whisper Systems. All rights reserved.
 | 
						|
 | 
						|
import UIKit
 | 
						|
import SessionUtilitiesKit
 | 
						|
 | 
						|
// 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?
 | 
						|
 | 
						|
    // Capture the location history of this gesture.
 | 
						|
    public var locationHistory = [CGPoint]()
 | 
						|
 | 
						|
    public var locationFirst: CGPoint? {
 | 
						|
        return locationHistory.first
 | 
						|
    }
 | 
						|
 | 
						|
    // MARK: - Touch Handling
 | 
						|
 | 
						|
    @objc
 | 
						|
    public override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
 | 
						|
        updateLocationHistory(event: event)
 | 
						|
 | 
						|
        super.touchesBegan(touches, with: event)
 | 
						|
    }
 | 
						|
 | 
						|
    @objc
 | 
						|
    public override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
 | 
						|
        updateLocationHistory(event: event)
 | 
						|
 | 
						|
        super.touchesMoved(touches, with: event)
 | 
						|
    }
 | 
						|
 | 
						|
    @objc
 | 
						|
    public override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
 | 
						|
        updateLocationHistory(event: event)
 | 
						|
 | 
						|
        super.touchesEnded(touches, with: event)
 | 
						|
    }
 | 
						|
 | 
						|
    private func updateLocationHistory(event: UIEvent) {
 | 
						|
        guard let touches = event.allTouches, touches.count > 0 else {
 | 
						|
            Log.error("[ImageEditorPanGestureRecognizer] No touches.")
 | 
						|
            return
 | 
						|
        }
 | 
						|
        guard let referenceView = referenceView else {
 | 
						|
            Log.error("[ImageEditorPanGestureRecognizer] Missing view.")
 | 
						|
            return
 | 
						|
        }
 | 
						|
        // Find the centroid.
 | 
						|
        var location = CGPoint.zero
 | 
						|
        for touch in touches {
 | 
						|
            location = location.adding(touch.location(in: referenceView))
 | 
						|
        }
 | 
						|
        location = location.multiplying(by: CGFloat(1) / CGFloat(touches.count))
 | 
						|
        locationHistory.append(location)
 | 
						|
    }
 | 
						|
 | 
						|
    public override func reset() {
 | 
						|
        super.reset()
 | 
						|
 | 
						|
        locationHistory.removeAll()
 | 
						|
    }
 | 
						|
}
 |