問題描述
我的 UIScrollView 的內容視圖中的 UITapGestureRecognizer 上的 UITapGestureRecognizer 沒有調用它的方法.
I have a problem where my UITapGestureRecognizer on my UILabels in a content view in my UIScrollView is not calling it's methods.
視圖層次結構如下:
- 滾動視圖(UIScrollView)
- 內容視圖(UIView)
- testLabel (UILabel) - 這里是 UITapGestureRecognizer 的附加位置
我已將代碼提煉成一個示例來突出問題
I have distilled the code down to an example to highlight the problem
// Set scrollview size - Added in Storyboad [scrollView setContentSize:CGSizeMake([arrayOfVerbs count]*self.view.frame.size.width, scrollView.contentSize.height)]; [scrollView setCanCancelContentTouches:YES]; // Tried both yes and no [scrollView setPagingEnabled:YES]; // Add content view UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height)]; [scrollView addSubview:contentView]; // Add test UILabel UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 100)]; [testLabel setBackgroundColor:[UIColor redColor]]; [testLabel setText:@"Test touch"]; [testLabel setUserInteractionEnabled:YES]; [contentView addSubview:testLabel]; // Add gesture recogniser UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(playSound:)]; singleTap.numberOfTapsRequired = 1; [testLabel addGestureRecognizer:singleTap];
這是點擊手勢識別器應該調用的方法
And this is the method that the tap gesture recogniser should call
- (void)playSound:(UITapGestureRecognizer *)sender { NSLog(@"play sound"); if(sender.state == UIGestureRecognizerStateEnded) { int pronounNumber = [sender.view tag]; int exampleNumber = (int)sender.view.frame.origin.x%(int)self.view.frame.size.width; NSLog(@"Pronoun is %i and example is %i", pronounNumber, exampleNumber); } }
當我嘗試觸摸 UILabel 時,從未調用此方法.
This method is never called when I tried to touch on the UILabel.
我已嘗試按照此 線程,但它仍然無法正常工作.
I have tried setting the property canCancelContentTouches to both YES and NO on the scroll view as suggested by this thread, but it's still not working.
奇怪的是,如果我在 scrollView 之外添加一個 UILabel,那么手勢識別器就會起作用!所以問題只出現在我的 contentView 中,它是我的 scrollView 的子視圖.
The strange thing is, if I add a UILabel outside of the scrollView, then the gesture recogniser works! So the problem only occurs in my contentView which is a subview of my scrollView.
我正在使用自動布局,如果這有什么不同嗎?
I am using auto-layout, if that might be any difference?
謝謝!
推薦答案
滾動視圖也有手勢識別器.默認情況下,任何時候只有 1 個手勢識別器可以處理觸摸.您需要讓自己成為手勢的代表,然后實現
gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
以返回YES
.這將允許它與滾動視圖同時工作.The scroll view also has a gesture recogniser. By default, only 1 gesture recognizer can be handling touches at any one time. You need to make yourself the delegate of your gesture and then implement
gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
to returnYES
. This will allow it to work at the same time as the scroll view.這篇關于UIScrollView子視圖中UILabels上的UITapGestureRecognizer不起作用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!
- 內容視圖(UIView)