使用Image在iPhone应用程序中执行操作而不是按钮

时间:2011-08-24 16:45:06

标签: iphone ios4

如何使用图像执行操作而不是使用按钮或向Button添加图像,只需单击按钮并执行特定的指令集。 (先谢谢)

2 个答案:

答案 0 :(得分:3)

使用UIGestureRecogniser

// IF your image view is called myImage
UIImageView *myImage = ...;

// Add a tap gesture recogniser
UITapGestureRecogniser *g = [[UITapGestureRecogniser alloc] initWithTarget:self action:@selector(imagePressed:)];
[myImage addGestureRecogniser:g];
[g release];

点按图片时,会调用此方法

- (void)imagePressed:(UIGestureRecogniser *)recogniser {
    NSLog(@"%@", recogniser);
}

为什么不想使用UIButton - 它继承自UIControl并且有很多你甚至不知道存在的代码?它只能包含一个图像,所以它看起来完全一样吗?

答案 1 :(得分:1)

好吧,关于使用UIButton的专家是它正确地构建了所有触摸事件。你可以使用UIImageView,但是你需要将它们子类化,而在大多数情况下,使用背景图像的UIButton恰好适合。