我正在开发一个应用程序,我想要的是当用户单击图像视图中的图像时
应该触发一个动作(例如单击按钮时)。
我不想要按钮图像。我只想要一个图像视图和操作。
请帮忙。
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected)];
singleTap.numberOfTapsRequired = 1;
imageView.userInteractionEnabled = YES;
[imageView addGestureRecognizer:singleTap];
[singleTap release];
-(void)tapDetected{
NSLog(@"single Tap on imageview");
}
根据您的评论,如果您想了解图像的索引,那么您可以通过tag
图像视图的属性来做到这一点。根据需要分配图像视图的tag
属性并获取tag
on Touch 事件。
例如 :
添加UITapGestureRecognizer
ITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(tap:)];
tap.numberOfTapsRequired = 1;
[self addGestureRecognizer:tap];
[tap release];
使用它的委托方法:- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
// called when touch is began or when user touches
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *touch = [touches anyObject];
int indexOfImageView = touch.view.tag;
}
希望对你有帮助...