我遇到的问题是如何使图像视图的行为与按钮相同。
实施例。在我的.h文件
-(IBAction)ClickTheImage:(id)sender;
然后在.m文件中。
-(void)ClickTheImage:(id)sender
{
//something to do with image click.
}
但是当我尝试在我的xib文件上连接IBAction时,当我尝试连接到我的UIImageView时,就会发生这种情况。关于如何做到这一点的任何想法?。
答案 0 :(得分:2)
确保在图片视图中将userInteractionEnabled
设置为YES
。然后创建并添加手势识别器,如下所示:
UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ClickTheImage:)];
[self.imageView addGestureRecognizer:tapRecognizer];
self.tapRecognizer.delegate = self;
让您的课程符合UIGestureRecognizerDelegate
。
答案 1 :(得分:0)
你可以反过来创造它。创建 UIButton 并将ImageView定义为UIButton的背景图像。然后IBAction和UIButton之间的连接将起作用..
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
或者你为什么要反过来使用呢?
答案 2 :(得分:0)
只需使用UITapGestureRecognizer
即可在.h文件中添加GestureRecognizerDelegate。 假设您的ImageView是* img;
UITapGestureRecognizer *TapRecognizer=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ImageHasTouched:)];
[TapRecognizer setDelegate:self];
[img setUserInteractionEnable:YES];
[img addGestureRecognizer:TapRecognizer];
-(void)ImageHasTouched:(TapGestureRecognizer *)Recognizer
{
UIImageView *TouchedImage=(UIImageView *)[[Recognizer self] view];
//Now when ever your image will be touched this method will get fired.
// do what ever you want
}