簡體   English   中英

在UIScrollView中為標准Pan Gesture Recognizer添加功能

[英]Add functionality to standard Pan Gesture Recognizer in a UIScrollView

我試圖跟蹤手指在UIScrollView 我已經將UIScrollView子類化(見下文),但遺憾的是我添加的手勢識別器正在覆蓋標准的手勢識別器。

結果我得到NSLog(@"Pan")工作但不幸的是視圖不再滾動。

如何讓兩個手勢識別器同時工作?

謝謝。

- (void)viewDidLoad:(BOOL)animated
{
    [super viewDidLoad:animated];

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [scrollView addGestureRecognizer:panRecognizer];
}


- (void)pan:(id)sender {
    NSLog(@"Pan");
}

如果你不想覆蓋標准的那個,你只需要同時識別它們。

- (void)viewDidLoad:(BOOL)animated
{
    [super viewDidLoad:animated];

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    panRecognizer.delegate = self;
    [scrollView addGestureRecognizer:panRecognizer];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
     return TRUE;
}


- (void)pan:(id)sender {
    NSLog(@"Pan");
}

編輯 :這種方法有效! 您只需要盡快設置canCancelContentTouches (我在viewDidLoad )。

原始答案 :我嘗試了一種新的方法,但不幸的是它沒有完全發揮作用。

我沒有添加手勢識別器,而是touchesBegan UIScrollView並編寫自己的touchesBegantouchesMoved等方法。

這樣我知道用戶在哪里觸摸但不幸的是,每當我canCancelContentTouches設置為NO后,每次開始滾動時touchesCancelled都會觸發touchesCancelled

有人知道為什么嗎? 我也發現了這個

暫無
暫無

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

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