Fix swipe gestures

This commit is contained in:
Ali 2023-10-26 17:40:54 +04:00
parent 6a52632335
commit 3b49480ef3
3 changed files with 24 additions and 61 deletions

View File

@ -61,21 +61,16 @@ public enum InteractiveTransitionGestureRecognizerEdgeWidth {
}
public class InteractiveTransitionGestureRecognizer: UIPanGestureRecognizer {
private let staticEdgeWidth: InteractiveTransitionGestureRecognizerEdgeWidth
private let edgeWidth: InteractiveTransitionGestureRecognizerEdgeWidth
private let allowedDirections: (CGPoint) -> InteractiveTransitionGestureRecognizerDirections
public var dynamicEdgeWidth: ((CGPoint) -> InteractiveTransitionGestureRecognizerEdgeWidth)?
private var currentEdgeWidth: InteractiveTransitionGestureRecognizerEdgeWidth
private var ignoreOffset: CGPoint = CGPoint()
private var validatedGesture = false
private var firstLocation: CGPoint = CGPoint()
private var currentAllowedDirections: InteractiveTransitionGestureRecognizerDirections = []
public init(target: Any?, action: Selector?, allowedDirections: @escaping (CGPoint) -> InteractiveTransitionGestureRecognizerDirections, edgeWidth: InteractiveTransitionGestureRecognizerEdgeWidth = .constant(16.0)) {
self.allowedDirections = allowedDirections
self.staticEdgeWidth = edgeWidth
self.currentEdgeWidth = edgeWidth
self.edgeWidth = edgeWidth
super.init(target: target, action: action)
@ -86,7 +81,6 @@ public class InteractiveTransitionGestureRecognizer: UIPanGestureRecognizer {
override public func reset() {
super.reset()
self.ignoreOffset = CGPoint()
self.validatedGesture = false
self.currentAllowedDirections = []
}
@ -105,10 +99,6 @@ public class InteractiveTransitionGestureRecognizer: UIPanGestureRecognizer {
return
}
if let dynamicEdgeWidth = self.dynamicEdgeWidth {
self.currentEdgeWidth = dynamicEdgeWidth(point)
}
super.touchesBegan(touches, with: event)
self.firstLocation = point
@ -138,11 +128,6 @@ public class InteractiveTransitionGestureRecognizer: UIPanGestureRecognizer {
}
}
override public func translation(in view: UIView?) -> CGPoint {
let result = super.translation(in: view)
return result//.offsetBy(dx: self.ignoreOffset.x, dy: self.ignoreOffset.y)
}
override public func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
let location = touches.first!.location(in: self.view)
let translation = CGPoint(x: location.x - self.firstLocation.x, y: location.y - self.firstLocation.y)
@ -161,51 +146,31 @@ public class InteractiveTransitionGestureRecognizer: UIPanGestureRecognizer {
if absTranslationX > 2.0 && absTranslationX > absTranslationY * 2.0 {
self.state = .failed
} else if absTranslationY > 2.0 && absTranslationX * 2.0 < absTranslationY {
self.ignoreOffset = CGPoint(x: -translation.x, y: -translation.y)
self.validatedGesture = true
}
}
} else {
let defaultEdgeWidth: CGFloat
switch self.staticEdgeWidth {
let edgeWidth: CGFloat
switch self.edgeWidth {
case let .constant(value):
defaultEdgeWidth = value
edgeWidth = value
case let .widthMultiplier(factor, minValue, maxValue):
defaultEdgeWidth = max(minValue, min(size.width * factor, maxValue))
}
let extendedEdgeWidth: CGFloat
switch self.currentEdgeWidth {
case let .constant(value):
extendedEdgeWidth = value
case let .widthMultiplier(factor, minValue, maxValue):
extendedEdgeWidth = max(minValue, min(size.width * factor, maxValue))
edgeWidth = max(minValue, min(size.width * factor, maxValue))
}
if !self.validatedGesture {
if self.firstLocation.x < extendedEdgeWidth && !self.currentAllowedDirections.contains(.rightEdge) {
if self.firstLocation.x < edgeWidth && !self.currentAllowedDirections.contains(.rightEdge) {
self.state = .failed
return
}
if self.firstLocation.x > size.width - extendedEdgeWidth && !self.currentAllowedDirections.contains(.leftEdge) {
if self.firstLocation.x > size.width - edgeWidth && !self.currentAllowedDirections.contains(.leftEdge) {
self.state = .failed
return
}
if self.currentAllowedDirections.contains(.rightEdge) && self.firstLocation.x < extendedEdgeWidth {
if absTranslationY > 2.0 && absTranslationY > absTranslationX * 2.0 {
self.state = .failed
} else if absTranslationX > 2.0 && absTranslationY * 2.0 < absTranslationX {
self.ignoreOffset = CGPoint(x: -translation.x, y: -translation.y)
if self.currentAllowedDirections.contains(.rightEdge) && self.firstLocation.x < edgeWidth {
self.validatedGesture = true
fireBegan = true
}
} else {
if self.currentAllowedDirections.contains(.rightEdge) && self.firstLocation.x < defaultEdgeWidth {
self.ignoreOffset = CGPoint(x: -translation.x, y: -translation.y)
self.validatedGesture = true
} else if self.currentAllowedDirections.contains(.leftEdge) && self.firstLocation.x > size.width - defaultEdgeWidth {
self.ignoreOffset = CGPoint(x: -translation.x, y: -translation.y)
} else if self.currentAllowedDirections.contains(.leftEdge) && self.firstLocation.x > size.width - edgeWidth {
self.validatedGesture = true
} else if !self.currentAllowedDirections.contains(.leftCenter) && translation.x < 0.0 {
self.state = .failed
@ -214,13 +179,11 @@ public class InteractiveTransitionGestureRecognizer: UIPanGestureRecognizer {
} else if absTranslationY > 2.0 && absTranslationY > absTranslationX * 2.0 {
self.state = .failed
} else if absTranslationX > 2.0 && absTranslationY * 2.0 < absTranslationX {
self.ignoreOffset = CGPoint(x: -translation.x, y: -translation.y)
self.validatedGesture = true
fireBegan = true
}
}
}
}
if self.validatedGesture {
super.touchesMoved(touches, with: event)

View File

@ -142,12 +142,12 @@ public final class NavigationContainer: ASDisplayNode, UIGestureRecognizerDelega
}
return .right
})
panRecognizer.dynamicEdgeWidth = { [weak self] _ in
/*panRecognizer.dynamicEdgeWidth = { [weak self] _ in
guard let self, let controller = self.controllers.last, let value = controller.interactiveNavivationGestureEdgeWidth else {
return .constant(16.0)
}
return value
}
}*/
if #available(iOS 13.4, *) {
panRecognizer.allowedScrollTypesMask = .continuous
}

View File

@ -305,7 +305,7 @@ public class ChatMessageAnimatedStickerItemNode: ChatMessageItemView {
if let item = self.item {
let _ = item
replyRecognizer.allowBothDirections = false//!item.context.sharedContext.immediateExperimentalUISettings.unidirectionalSwipeToReply
self.view.disablesInteractiveTransitionGestureRecognizer = true
self.view.disablesInteractiveTransitionGestureRecognizer = false
}
replyRecognizer.shouldBegin = { [weak self] in
if let strongSelf = self, let item = strongSelf.item {