我有UIImageView的UIScrollview。在UIScrollView的內容上檢測觸摸事件
如果我在將UIImageView添加到scrollview之前添加手勢,則不會觸發touch事件。
我想得到一個水龍頭髮生在UIImageView。
我已經看到了一些關於從UIScrollView獲取觸摸點然後根據位置計算UIImageView的答案 - 但這看起來真的很混亂和矯枉過正。
是否有一種簡單的方法從UIScrollView中的對象獲取觸摸事件?
我有UIImageView的UIScrollview。在UIScrollView的內容上檢測觸摸事件
如果我在將UIImageView添加到scrollview之前添加手勢,則不會觸發touch事件。
我想得到一個水龍頭髮生在UIImageView。
我已經看到了一些關於從UIScrollView獲取觸摸點然後根據位置計算UIImageView的答案 - 但這看起來真的很混亂和矯枉過正。
是否有一種簡單的方法從UIScrollView中的對象獲取觸摸事件?
您的UIImageView
s可能禁用了用戶交互(這是他們默認使用的)。調用每個圖像視圖以下行,和你的水龍頭應該承認:
yourImageView.userInteractionEnabled = YES;
確保做到這一點before adding the gesture recognizer。
幾周前我有這個確切的問題,這是什麼修復了我。
爲了完整起見。
UIScrollView內部的UIImageView沒有被編程爲原生接收觸摸事件和手勢。
所以,你可能想要做的是:
>
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)];
singleTap.cancelsTouchesInView = NO;
[myScrollView addGestureRecognizer:singleTap];
>
CustomImageView *imageView = [[CustomImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[imageView setUserInteractionEnabled:YES];
[scrollView addSubview:imageView];
>
@界面CustomImageView:UIImageView {}
@implementation C ustomImageView
- (id)initWithFrame:(CGRect)frame{}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {}
@end
謝謝!我認爲「singleTap.cancelsTouchesInView = NO;」是關鍵。 – green0range 2017-12-14 22:49:05
BINGO!這完全是我需要的工作。點上感謝你! – Aggressor 2014-09-28 02:33:53
樂意幫忙! :) – rebello95 2014-09-28 02:36:56