回答
添加UITapGestureRecognizer:
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(handleTap:)];
[imageView addGestureRecognizer:tapRecognizer];
[tapRecognizer release];
然後回調...
- (void)handleTap:(UITapGestureRecognizer *)tapGestureRecognizer
{
//show your alert...
}
使用觸摸事件的方法做烏爾任務
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location= [touch locationInView:self.view];
if(CGRectContainsPoint(urImageView.frame, location)) {
//AlertView COde
}
}
如何設置一個圖像視圖的觸摸事件? – Gabrielle
我給出了示例代碼 –
使用觸摸委託方法,找到你的圖像視圖那裏,
如果你不原意子類UIImageView
覆蓋觸摸事件方法 - 或者實現ViewController中的觸摸方法,並檢查觸摸幀位於圖像視圖幀 - 你可以(這可能更容易)添加一個UITapGestureRecognizer
到你的UIImageView
。
See here in the documentation更多細節
UITapGestureRecognizer* tapGR = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(tapOnImage:)];
[yourImageView addGestureRecognizer:tapGR];
[tagGR release];
然後實現-(void)tapOnImage:(UIGestureRecognizer*)tapGR
方法,如你所願
UITapGestureRecognizer *singleTapOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
singleTapOne.numberOfTouchesRequired = 1; singleTapOne.numberOfTapsRequired = 1; singleTapOne.delegate = self;
[self.view addGestureRecognizer:singleTapOne]; [singleTapOne release];
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
首先, 啓用財產
yourImgView.setUserInteractionEnabled = TRUE;
然後應用在viewDidLoad中下面的代碼;
UITapGestureRecognizer *tapOnImg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapOnImgView:)];
tapOnImg.numberOfTapsRequired = 1; tapOnImg.delegate = self;
[yourImgView addGestureRecognizer:tapOnImg];
而不是使用UITapGestureRecognizer
如果其中有您的ImageView覆蓋類UIResponder
你可以只覆蓋touchesBegan
:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == YOUR_IMAGE_VIEW) {
// User clicked on the image, display the dialog.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Image Clicked" message:@"You clicked an image." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
您必須確保userInteractionEnabled
是YES
您的圖像視圖。
- 1. UIImageView觸摸點
- 2. 顯示時觸摸事件
- 3. 顯示PickerView時觸摸UITextView
- 4. 顯示觸摸
- 5. 通過UIImageView觸摸?
- 6. UIImageView塊觸摸QLPreviewController
- 7. UIImageView觸摸處理
- 8. UIImageView觸摸事件
- 9. 多個uiimageview觸摸
- 10. 可可觸摸 - 在UIImageView中觸摸
- 11. UIAlert不顯示
- 12. Swift - 向UIImageView添加觸摸
- 13. UIImageView子類觸摸檢測
- 14. 刪除觸摸的UIImageView
- 15. 觸摸內部的UIImageView
- 16. 的UIImageView觸摸檢測
- 17. iOS的觸摸一個UIImageView
- 18. UIImageView拖動和觸摸
- 19. 猜測觸摸的UIImageView
- 20. 當圖像被觸摸時顯示UIButton?
- 21. UISearchBar - 觸摸時不顯示鍵盤
- 22. 點擊時顯示觸摸UIButton Objective-C
- 23. EditText觸摸時沒有突出顯示
- 24. 觸摸按鈕時顯示發光
- 25. sencha觸摸列表顯示
- 26. 顯示卡與sencha觸摸
- 27. 無法獲得觸摸事件委託方法觸摸uiimageview
- 28. 觸摸時更大觸摸
- 29. UIImageView顯示UIImageView圖像UIImageView
- 30. sencha觸摸圖工具提示顯示
使用UITapGestureRecognizer的 – Narayana
在.h UIGestureRecognizerDelegate – Narayana