Last active
February 6, 2024 09:23
-
-
Save SergLam/a8a8a0ce54e913a7ceac7d852b7e216c to your computer and use it in GitHub Desktop.
Custom screen transition with pan gesture from the center of screen
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
import UIKit | |
enum HorizontalDirection { | |
case LeftToRight | |
case RightToLeft | |
} | |
enum VerticalDirection { | |
case TopToBottom | |
case BottomToTop | |
} | |
enum PanDirection { | |
case vertical(direction: VerticalDirection) | |
case horizontal(direction: HorizontalDirection) | |
} | |
class PanDirectionGestureRecognizer: UIPanGestureRecognizer { | |
let direction: PanDirection | |
init(direction: PanDirection, target: AnyObject, action: Selector) { | |
self.direction = direction | |
super.init(target: target, action: action) | |
} | |
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) { | |
super.touchesMoved(touches, with: event) | |
guard state == .began else { | |
return | |
} | |
let vel = velocity(in: view) | |
switch direction { | |
case .horizontal(let direction): | |
guard abs(vel.y) > abs(vel.x) else { | |
switch direction { | |
case .LeftToRight: | |
guard vel.x > 0 else { | |
state = .cancelled | |
return | |
} | |
case .RightToLeft: | |
guard vel.x < 0 else { | |
state = .cancelled | |
return | |
} | |
} | |
return | |
} | |
state = .cancelled | |
case .vertical(let direction): | |
guard abs(vel.x) > abs(vel.y) else { | |
switch direction { | |
case .TopToBottom: | |
guard vel.y < 0 else { | |
state = .cancelled | |
return | |
} | |
case .BottomToTop: | |
guard vel.y > 0 else { | |
state = .cancelled | |
return | |
} | |
} | |
return | |
} | |
state = .cancelled | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment