XCode 5 - 使图像像按钮一样可以点亮

时间:2014-10-08 18:23:00

标签: ios xcode image tap

在XCode 5中,我怎么能拥有它,以便当我点击UIImage时会发生一些事情。像按钮,但我不想使用按钮。我需要它成为一个形象。我正在制作一个游戏,我无法在其他地方找到如何做到这一点,所以请帮忙。

2 个答案:

答案 0 :(得分:2)

没有松散的图像,因此图像必须位于显示它的 内。确保某些内容(可能是图像视图)启用了用户交互。然后将UITapGestureRecognizer附加到它。

答案 1 :(得分:2)

为了显示您的UIImage,需要将其附加到UIViewCALayer。你可以这样做:

UIImageView *notAButton = [[UIImageView alloc] initWithImage:myImage];
[notAButton setUserInteractionEnabled:YES];
[notAButton addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedButton:)]];
[self.view addSubview notAButton];

顺便说一句,你对UIButton有什么看法?它是迄今为止最容易和最广泛接受的方式。

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setImage:myImage forState:UIControlStateNormal];
[aButton addTarget:self action:@selector(tappedButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:aButton];