Adding Tap Gesture on UIImage

2019-04-19 18:17发布

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

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...

am i right?

i keep receiving this error message

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

is there any other way?

4条回答
孤傲高冷的网名
2楼-- · 2019-04-19 18:51

You have to add TapGesture in UIImageView not 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...
 }
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-04-19 19:04

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.

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
}
查看更多
ら.Afraid
4楼-- · 2019-04-19 19:08

Try with UIButton instead of UIIMage and make the UIButton type custom. And on clicking the same you can show the animation.

查看更多
淡お忘
5楼-- · 2019-04-19 19:09

You have to add the gesture to UIImageView, not UIImage

查看更多
登录 后发表回答