問題描述
我有一個 UIScrollView
,我添加了一個點擊手勢識別器來顯示/隱藏一些 UI 覆蓋,使用:
I have a UIScrollView
to which I added a single tap gesture recognizer to show/hide some UI overlay using:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[scrollView addGestureRecognizer:singleTap];
和:
- (void)handleTap:(UITapGestureRecognizer *)sender {
// report click to UI changer
}
我在 UIScrollView
的底部添加了一個簡易表格視圖.一切正常(水平和垂直滾動),但問題是點擊只能被手勢識別器識別(上圖),而不是簡單的表格視圖.如果我刪除注冊手勢偵聽器的行,一切正常,表格視圖會通知自己點擊.
I added an easy table view to the bottom of the UIScrollView
. Everything works right (scrolling both horizontally and vertically) but the problem is that taps are recognized only by the gesture recognizer (above), but not by the easy table view.
If I remove The line that registers the gesture listener, everything works fine, the table view notices taps on itself.
就好像手勢識別器函數吃掉"了表格視圖上的點擊事件,而不是向下傳播它們.
It's as if the gesture recognizer function "eats" the tap events on the table view and doesn't propagate them downward.
感謝任何幫助
推薦答案
這應該可以解決您的問題.
檢測 UIScrollView 上的觸摸事件 AND在 UIView 的組件上[放置在 UIScrollView 內]
這個想法是告訴手勢識別器不要吞噬觸摸事件.為此,您需要將 singleTap 的 cancelsTouchesInView
屬性設置為 NO
,默認為 YES
.
This should solve your problem.
Detect touch event on UIScrollView AND on UIView's components [which is placed inside UIScrollView]
The idea is to tell the gesture recognizer to not swallow up the touch events. To do this you need to set singleTap's cancelsTouchesInView
property to NO
, which is YES
by default.
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
singleTap.cancelsTouchesInView = NO;
[scrollView addGestureRecognizer:singleTap];
這篇關于ScrollView 手勢識別器吃掉所有的觸摸事件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!