簡體   English   中英

iOS / Objective-C / Autolayout:僅在視圖上滑動手勢一次即可,然后通過滑動視圖移動

[英]IOS/Objective-C/Autolayout: Swipe gesture on view only working once and then view moves with swipe

我正在嘗試調試一個奇怪的問題。 我有代碼創建一個leftswiperecognizer,將其分配給視圖,並具有可更改某些元素的處理程序。

我有此代碼的版本工作正常。 但是,在一個VC上,在第一次滑動也能正常工作之后,在第二次滑動之后,並不是識別出手勢,而是移動了整個視圖-就像您拖動它或嘗試使其水平滾動一樣。 存在滾動識別器,但通常只響應垂直拖動。 此屏幕上還有一個點擊識別器。 他們倆都很好。 相同的應用程序還有其他屏幕,它們向左滑動,滾動和點擊識別器都可以正常運行。

簡而言之,先滑動即可。 然后沒有識別出滑動。 以前有人遇到過這個問題嗎? 在此先感謝您的任何建議。

//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];
}

在第二次滑動時,整個視圖會移動,而不是識別手勢

為了便於討論,我們假設您正確地進行了所有操作,並且未將滑動手勢識別器添加到錯誤的視圖中。 好吧,這意味着“整個視圖”具有手勢識別器,可能是滑動或平移手勢識別器,並且它首先識別並導致手勢識別器失敗。

您可以通過請求該視圖的gestureRecognizers輕松地找到它。 這是一個實用程序方法,可讓您向視圖詢問其所有手勢識別器及其超級視圖的所有手勢識別器,因為它們會影響該視圖:

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

在視圖進入界面后的某個時刻(例如,在viewDidAppear之后),將該消息發送到某個視圖,例如self.view.reportAllGestureRecognizers 您可能會對所學的知識感到驚訝。

如果真是這樣,那么可以使用委托方法和其他設備來在手勢識別器和另一個手勢識別器之間進行調解,以便您的手勢識別器可以首先識別出它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM