ios 在 UIImage 上添加点击手势

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9206374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:43:18  来源:igfitidea点击:

Adding Tap Gesture on UIImage

objective-ciosuiimagecore-animation

提问by Crisn

I am trying to make clickable UIImage, where the user can click it then it'll animate...

我正在尝试制作可点击的 UIImage,用户可以在其中点击它然后它会动画......

i am working with the UIScrollVIew that's why i used the UITapGesture instead of touchesBegan, and it seems that UIGestureRecognizer is not compatible with UIImage...

我正在使用 UIScrollVIew 这就是为什么我使用 UITapGesture 而不是 touchesBegan 的原因,而且 UIGestureRecognizer 似乎与 UIImage 不兼容...

am i right?

我对吗?

i keep receiving this error message

我不断收到此错误消息

receiver type 'UIImage' for instance message does not declare a method with selector 'addGestureRecognizer'

例如消息的接收器类型“UIImage”未声明带有选择器“addGestureRecognizer”的方法

is there any other way?

还有其他方法吗?

回答by Pooja Jalan

You have to add TapGesture in UIImageView not UIImage

你必须在 UIImageView 中添加 TapGesture 而不是 UIImage

imgView.userInteractionEnabled = YES;

UITapGestureRecognizer *tapGesture1 = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(tapGesture:)];

tapGesture1.numberOfTapsRequired = 1;

[tapGesture1 setDelegate:self];

[imgView addGestureRecognizer:tapGesture1];

[tapGesture1 release];

You can response to the tap with the defined selector and do stuff there

您可以使用定义的选择器响应点击并在那里执行操作

- (void) tapGesture: (id)sender
{
    //handle Tap...
 }

回答by jasondinh

You have to add the gesture to UIImageView, not UIImage

您必须将手势添加到 UIImageView,而不是 UIImage

回答by mariusLAN

You can simply add a TapGestureRecognizer to a UIImageView. You have to use a UIImageView because gesture recognizer are only allowed to be added to views.

您可以简单地将 TapGestureRecognizer 添加到 UIImageView。你必须使用 UIImageView 因为手势识别器只允许添加到视图中。

UIView *someView = [[UIView alloc] initWithFrame:CGRectZero];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
tapRecognizer.numberOfTapsRequired = 1;
[someView addGestureRecognizer:tapRecognizer];

You can response to the tap with the defined selector and do stuff there

您可以使用定义的选择器响应点击并在那里执行操作

- (void)tapAction:(UITapGestureRecognizer *)tap
{
    // do stuff
}

回答by Sarah

Try with UIButtoninstead of UIIMageand make the UIButtontype custom. And on clicking the same you can show the animation.

尝试使用UIButton而不是UIIMage并使UIButton类型custom。单击相同的按钮,您可以显示动画。