我有一个ImageView,当用户按下图像视图将消息显示为警报时,我想要它。我应该使用什么方法?你能举个例子吗?
提前致谢..
答案 0 :(得分:5)
添加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...
}
答案 1 :(得分:1)
使用触摸事件方法执行任务
-(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
}
}
答案 2 :(得分:1)
如果您不打算将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
方法
答案 3 :(得分:1)
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
答案 4 :(得分:1)
首先, 启用属性
yourImgView.setUserInteractionEnabled = TRUE;
然后在viewDidLoad上应用以下代码;
UITapGestureRecognizer *tapOnImg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapOnImgView:)];
tapOnImg.numberOfTapsRequired = 1; tapOnImg.delegate = self;
[yourImgView addGestureRecognizer:tapOnImg];
答案 5 :(得分:0)
使用触摸委托方法并在那里找到您的图片视图,
答案 6 :(得分:0)
如果您使用imageview的类覆盖UITapGestureRecognizer
,则可以覆盖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
。