fixed some bugs
This commit is contained in:
@@ -44,6 +44,13 @@ final class VideoSurfaceView: UIView {
|
||||
private var tapRecognizer: UITapGestureRecognizer!
|
||||
private var panRecognizer: UIPanGestureRecognizer!
|
||||
private var longPressRecognizer: UILongPressGestureRecognizer!
|
||||
private var didDispatchSwipe = false
|
||||
private var swipeAxis: SwipeAxis?
|
||||
|
||||
private enum SwipeAxis {
|
||||
case horizontal
|
||||
case vertical
|
||||
}
|
||||
|
||||
override init(frame: CGRect) {
|
||||
super.init(frame: frame)
|
||||
@@ -80,25 +87,65 @@ final class VideoSurfaceView: UIView {
|
||||
}
|
||||
|
||||
@objc private func handlePan(_ recognizer: UIPanGestureRecognizer) {
|
||||
guard recognizer.state == .ended || recognizer.state == .cancelled else {
|
||||
return
|
||||
}
|
||||
let translation = recognizer.translation(in: self)
|
||||
let velocity = recognizer.velocity(in: self)
|
||||
let dx = translation.x
|
||||
let dy = translation.y
|
||||
if abs(dy) > abs(dx),
|
||||
abs(dy) >= minSwipeDistance,
|
||||
abs(velocity.y) >= minSwipeVelocity {
|
||||
|
||||
switch recognizer.state {
|
||||
case .began:
|
||||
resetSwipeTracking()
|
||||
case .changed:
|
||||
// 在滑动达到阈值时立即派发,并且同一个手势只允许派发一次。
|
||||
evaluateSwipe(dx: dx, dy: dy, velocity: velocity)
|
||||
case .ended, .cancelled, .failed:
|
||||
if !didDispatchSwipe {
|
||||
evaluateSwipe(dx: dx, dy: dy, velocity: velocity)
|
||||
}
|
||||
resetSwipeTracking()
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
private func evaluateSwipe(dx: CGFloat, dy: CGFloat, velocity: CGPoint) {
|
||||
guard !didDispatchSwipe else { return }
|
||||
|
||||
if swipeAxis == nil {
|
||||
// 只有方向足够明确时锁定轴向,避免轻微斜滑被误判为上下滑。
|
||||
if abs(dx) > abs(dy) * 1.2 {
|
||||
swipeAxis = .horizontal
|
||||
} else if abs(dy) > abs(dx) * 1.2 {
|
||||
swipeAxis = .vertical
|
||||
} else {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
switch swipeAxis {
|
||||
case .vertical:
|
||||
guard abs(dy) >= minSwipeDistance,
|
||||
abs(velocity.y) >= minSwipeVelocity else {
|
||||
return
|
||||
}
|
||||
didDispatchSwipe = true
|
||||
onVerticalSwipe?(dy < 0)
|
||||
return
|
||||
case .horizontal:
|
||||
guard abs(dx) >= abs(dy),
|
||||
abs(dx) >= minSwipeDistance,
|
||||
abs(velocity.x) >= minSwipeVelocity else {
|
||||
return
|
||||
}
|
||||
didDispatchSwipe = true
|
||||
controller?.handleGesture(dx > 0 ? .swipeRight : .swipeLeft)
|
||||
default:
|
||||
break
|
||||
}
|
||||
guard abs(dx) >= abs(dy),
|
||||
abs(dx) >= minSwipeDistance,
|
||||
abs(velocity.x) >= minSwipeVelocity else {
|
||||
return
|
||||
}
|
||||
controller?.handleGesture(dx > 0 ? .swipeRight : .swipeLeft)
|
||||
}
|
||||
|
||||
private func resetSwipeTracking() {
|
||||
didDispatchSwipe = false
|
||||
swipeAxis = nil
|
||||
}
|
||||
|
||||
@objc private func handleLongPress(_ recognizer: UILongPressGestureRecognizer) {
|
||||
|
||||
Reference in New Issue
Block a user