IOS/objective-C/自动布局 : Swipe gesture on view only working once and then view moves with swipe

标签 ios objective-c uiscrollview uigesturerecognizer

我正在尝试调试一个奇怪的问题。我有创建 leftswiperecognizer 的代码,将其分配给 View ,并有一个处理程序可以在滑动时更改某些元素。

此代码的多个版本运行良好。然而,在一个 VC 上,在第一次滑动后也工作正常,在第二次滑动时,整个 View 不会被识别,而是移动——就像你正在拖动它或试图水平滚动它一样。有一个滚动识别器,但通常它只响应垂直拖动。此屏幕上还有一个点击识别器。他们都工作正常。同一个应用程序有其他屏幕,左滑、滚动和点击识别器都运行良好。

简而言之,第一次滑动时效果很好。然后没有识别出滑动。以前有没有人遇到过这个问题?提前感谢您的任何建议。

//create recognizer (called from viewdidload)
-(void) addLeftSwipeRecognizer {
    UISwipeGestureRecognizer *recognizer;
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleLeftSwipe:)];
    [recognizer setDirection:UISwipeGestureRecognizerDirectionLeft];
    [[self view] addGestureRecognizer:recognizer];
}

- (void) handleLeftSwipe:(UISwipeGestureRecognizer *) sender
{
    self.title = @"New title";
//some other changes to labels.
}    

//other recognizers
called from viewdidload    
-(void) addListTapRecognizer {
    UITapGestureRecognizer *tapList = [[UITapGestureRecognizer alloc]initWithTarget:self     action:@selector(handleTap:)];
      tapList.cancelsTouchesInView = NO;
    self.listSub.userInteractionEnabled=YES;
    self.listSub.editable=NO;
    [self.listSub addGestureRecognizer:tapList];
}
- (void) handleTap:(UITapGestureRecognizer *)recognizer{

    UITextView *textView =  (UITextView *)recognizer.view;
    CGPoint location = [recognizer locationInView:textView];

    CGPoint position = CGPointMake(location.x, location.y);
     UITextPosition *tapPosition = [textView closestPositionToPoint:position];
//do some stuff based on position of tap
}
//called from viewdidload
-(void) addScrollGestureRecognizer {
    UITapGestureRecognizer *tapScroll = [[UITapGestureRecognizer alloc]initWithTarget:self     action:@selector(tapped)];
    tapScroll.cancelsTouchesInView = NO;
    self.scrollView.userInteractionEnabled =YES;
    [self.scrollView addGestureRecognizer:tapScroll];
}
//dismiss keyboard when you tap scrollview
- (void) tapped
{
    [self.view endEditing:YES];
}

最佳答案

on the second swipe, instead of the gesture getting recognized, the entire view moves

为了便于讨论,我们假设您所做的一切都是正确的,并且没有将您的滑动手势识别器添加到错误的 View 中。那么,这意味着“整个 View ”有一个手势识别器,可能是滑动或平移手势识别器,它首先识别并导致你的手势识别器失败。

您可以通过请求该 View 的 gestureRecognizers 轻松找到它。这是一个实用程序方法,它允许您向 View 询问其所有手势识别器及其父 View 的所有手势识别器,因为它们会影响该 View :

extension UIView {
    func reportAllGestureRecognizers() {
        if let grs = self.gestureRecognizers {
            print(grs)
        }
        if let sup = self.superview {
            sup.reportAllGestureRecognizers()
        }
    }
}

在您的 View 进入界面后的某个时刻(即在 viewDidAppear 之后),将该消息发送到某个 View ,例如self.view.reportAllGestureRecognizers。您可能会对所学内容感到惊讶。

如果情况确实如此,则可以使用委托(delegate)方法和其他设备在您的手势识别器和另一个手势识别器之间进行调解,以便您的手势识别器首先识别。

关于IOS/objective-C/自动布局 : Swipe gesture on view only working once and then view moves with swipe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49456153/

相关文章:

objective-c - 调整成为FirstResponder 和 resignFirstResponder 的 View 大小 - Cocoa (OS X)

swift - 在 ScrollView 中缩放图像

ios - 在ios中添加一个控件到新的联系人屏幕

iOS Swift3 使用键盘移动 Textfield 和 ContainerView

iOS全文编辑器实现示例

iphone - 为什么 stringByReplacingPercentEscapesUsingEncoding 不 : convert L%C3%A9on back to Léon?

ios - NSCondition 或 @synchronized

ios - 如何以编程方式将 UICollectionView 滚动到底部?

iphone - 滚动时 ScrollView 中的振动

ios - 标签栏图标显示不正确