Context menus sometimes present during CVC scroll

This commit is contained in:
Eugene Bistolas 2021-08-04 10:55:47 -10:00
parent 305953b91a
commit 5faee7bfc9
7 changed files with 36 additions and 3 deletions

View File

@ -48,7 +48,7 @@ public class ContextMenuInteraction: NSObject, UIInteraction {
private var longPressGestureRecognizer: UIGestureRecognizer = {
let recognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressRecognized(sender:)))
recognizer.minimumPressDuration = 0.1
recognizer.minimumPressDuration = 0.2
return recognizer
}()
@ -278,6 +278,10 @@ public class ChatHistoryContextMenuInteraction: ContextMenuInteraction {
}
public func initiatingGestureRecognizerDidEnd() {
cancelPresentationGesture()
}
public func cancelPresentationGesture() {
gestureEligibleForMenuPresentation = false
if contextMenuController == nil, let configuarion = self.configuration {

View File

@ -9,6 +9,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)collectionViewWillChangeSizeFrom:(CGSize)oldSize to:(CGSize)newSize;
- (void)collectionViewDidChangeSizeFrom:(CGSize)oldSize to:(CGSize)newSize;
- (void)collectionViewWillAnimate;
- (BOOL)collectionViewShouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
@end

View File

@ -112,6 +112,13 @@ NS_ASSUME_NONNULL_BEGIN
[super scrollRectToVisible:rect animated:animated];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return
[self.layoutDelegate collectionViewShouldRecognizeSimultaneouslyWithGestureRecognizer:otherGestureRecognizer];
}
@end
NS_ASSUME_NONNULL_END

View File

@ -444,6 +444,10 @@ extension ConversationViewController: ConversationCollectionViewDelegate {
scrollingAnimationDidStart()
}
public func collectionViewShouldRecognizeSimultaneously(with otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return otherGestureRecognizer == collectionViewContextMenuGestureRecognizer
}
public func scrollingAnimationDidStart() {
AssertIsOnMainThread()

View File

@ -16,7 +16,7 @@ extension ConversationViewController: UIGestureRecognizerDelegate {
if FeatureFlags.contextMenus {
collectionViewContextMenuGestureRecognizer.addTarget(self, action: #selector(handleLongPressGesture))
collectionViewContextMenuGestureRecognizer.minimumPressDuration = 0.1
collectionViewContextMenuGestureRecognizer.minimumPressDuration = 0.2
collectionViewContextMenuGestureRecognizer.delegate = self
collectionView.addGestureRecognizer(collectionViewContextMenuGestureRecognizer)
}
@ -71,7 +71,13 @@ extension ConversationViewController: UIGestureRecognizerDelegate {
}
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
// Support standard long press recognizing for body text cases, and context menu long press recognizing for everything else
// Allow the context menu gesture recognizer to recognize simultaneously with scroll
if gestureRecognizer == collectionViewContextMenuGestureRecognizer && otherGestureRecognizer == collectionView.panGestureRecognizer {
return true
}
// Support standard long press recognizing for body text cases, and context menu long press recognizing for everything else
let currentIsLongPressOrTap = (gestureRecognizer == collectionViewLongPressGestureRecognizer || gestureRecognizer == collectionViewContextMenuGestureRecognizer || gestureRecognizer == collectionViewTapGestureRecognizer)
let otherIsLongPressOrTap = (otherGestureRecognizer == collectionViewLongPressGestureRecognizer || otherGestureRecognizer == collectionViewContextMenuGestureRecognizer || otherGestureRecognizer == collectionViewTapGestureRecognizer)
return currentIsLongPressOrTap && otherIsLongPressOrTap

View File

@ -286,6 +286,9 @@ extension ConversationViewController: ContextMenuInteractionDelegate {
}
public func contextMenuInteraction(_ interaction: ContextMenuInteraction, willDisplayMenuForConfiguration: ContextMenuConfiguration) {
// Reset scroll view pan gesture recognizer, so CV does not scroll behind context menu post presentation on user swipe
collectionView.panGestureRecognizer.isEnabled = false
collectionView.panGestureRecognizer.isEnabled = true
dismissKeyBoard()
}

View File

@ -150,6 +150,8 @@ extension ConversationViewController: UIScrollViewDelegate {
scheduleScrollUpdateTimer()
updateScrollingContent()
updateContextMenuInteractionIfNeeded()
}
private func scheduleScrollUpdateTimer() {
@ -173,6 +175,12 @@ extension ConversationViewController: UIScrollViewDelegate {
RunLoop.main.add(scrollUpdateTimer, forMode: .common)
}
private func updateContextMenuInteractionIfNeeded() {
if let contextMenuInteraction = collectionViewActiveContextMenuInteraction {
contextMenuInteraction.cancelPresentationGesture()
}
}
@objc
private func scrollUpdateTimerDidFire() {
AssertIsOnMainThread()