Skip to content

Instantly share code, notes, and snippets.

@christianselig
Created May 12, 2023 00:24
Show Gist options
  • Save christianselig/785372aef9403aa6fa59911437fe62c6 to your computer and use it in GitHub Desktop.
Save christianselig/785372aef9403aa6fa59911437fe62c6 to your computer and use it in GitHub Desktop.
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