Created
July 3, 2020 10:09
-
-
Save alfian0/d92c3c41377116b2ff2475ac9c9090fd to your computer and use it in GitHub Desktop.
This file contains 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
@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