Skip to content

Instantly share code, notes, and snippets.

@alfian0
Created July 3, 2020 10:09
Show Gist options
  • Save alfian0/d92c3c41377116b2ff2475ac9c9090fd to your computer and use it in GitHub Desktop.
Save alfian0/d92c3c41377116b2ff2475ac9c9090fd to your computer and use it in GitHub Desktop.
@objc
private func panGesture(_ sender: UIPanGestureRecognizer) {
let percentThreshold: CGFloat = 0.3
let translation = sender.translation(in: sender.view)
let verticalMovement = translation.y / sender.view!.bounds.height
let downwardMovement = fmaxf(Float(verticalMovement), 0.0)
let downwardMovementPercent = fminf(downwardMovement, 1.0)
let progress = CGFloat(downwardMovementPercent)
guard let interactor = interactor else { return }
switch sender.state {
case .began:
interactor.hasStarted = true
case .changed:
let velocity = sender.velocity(in: sender.view?.superview)
let maxOffset = containerView!.bounds.height - presentedView!.frame.height
presentedView!.frame.origin.y = max(maxOffset, maxOffset + translation.y)
interactor.shouldFinish = progress > percentThreshold
interactor.update(progress)
direction = velocity.y
case .cancelled:
interactor.hasStarted = false
interactor.cancel()
case .ended:
interactor.hasStarted = false
if direction < 0 {
presentedView!.frame.origin.y = containerView!.bounds.height - self.presentedView!.frame.height
interactor.cancel()
} else {
presentedViewController.dismiss(animated: true, completion: nil)
interactor.shouldFinish ? interactor.finish() : interactor.cancel()
}
default:
break
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment