Created
May 12, 2023 00:24
-
-
Save christianselig/785372aef9403aa6fa59911437fe62c6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
var startTime: CFAbsoluteTime! | |
class ViewController: UIViewController { | |
let movingView = UIView() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
movingView.frame = CGRect(x: 200.0, y: 100.0, width: 200.0, 200.0) | |
movingView.backgroundColor = .systemBlue | |
view.addSubview(movingView) | |
let panGestureRecognizer = InstantPanGestureRecognizer( | |
target: self, | |
action: #selector(panned(panGestureRecognizer:)) | |
) | |
movingView.addGestureRecognizer(panGestureRecognizer) | |
} | |
@objc private func panned(panGestureRecognizer: UIPanGestureRecognizer) { | |
if panGestureRecognizer.state == .began { | |
// Takes 0.01 seconds on iPhone 🐇 | |
// Takes 0.75 seconds on iPads 🐢 | |
print("Time elapsed: \(CFAbsoluteTimeGetCurrent() - startTime) s.") | |
} | |
} | |
} | |
class InstantPanGestureRecognizer: UIPanGestureRecognizer { | |
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) { | |
super.touchesBegan(touches, with: event) | |
state = .began | |
startTime = CFAbsoluteTimeGetCurrent() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment