How to make my image clickable

Obviously, I skipped UIImageView versus UIImage to register a gesture (e.g. click / click) on the image, I need to have the image in uiimageview and the registration gesture on uiimageview, not uiimage.

So this piece of code works for me:

- (void)drawRect:(CGRect)rect {     Obj *obj = ... obj has imageHref which is NSString   UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:obj.imageHref]];   [image drawInRect:CGRectMake((self.frame.size.width/2) - (image.size.with/2), (self.frame.size.height / 2) - (image.size.height / 2), image.size.width, image.size.height)]; } 

This gives me a centered image, but it's useless because I want it to be clickable, so although I just do it with UIImageview, and not like this:

  - (void)drawRect:(CGRect)rect {    Obj *obj = ... obj has imageHref which is NSString  UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:obj.imageHref]]; CGRect rect1=CGRectMake((self.frame.size.width/2) - (image.size.width/2), (self.frame.size.height / 2) - (image.size.height / 2), image.size.width, image.size.height); UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:radioStationImage]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)]; [backgroundImageView addGestureRecognizer:tap];  [backgroundImageView drawRect:rect1]; } 

I just want to make this image clickable to do some action when clicked, how difficult is it?

EDIT:

I really implement:

https://github.com/andreyvit/SoloComponents-iOS/tree/master/ATPagingView

Which offers a neat scroll page. But with clickable images in the middle of the screen is not page 1, 2, etc.

EDIT 2:

Any perspective today?

+6
source share
2 answers

For this you do not need a special drawing ( drawRect: . Just add a UIImageView as a subquery when loading the view (in Interface Builder or in loadView depending on how you initialize your view). You can then add the UITapGestureRecognizer to this view as you do it.

As @jackslash notes, you also need to enable user interaction to view the image as soon as you do it.


I believe that you are doing this a lot harder than it is. No custom drawing needed with drawRect: There is no need to subclass UIImageView (@ madmik3 links do not apply to iOS4 +). There is probably no need for a valid UIView subclass (you can do all this inside the view controller), but here is how you do it in a subclass of the form:

 - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { Obj *obj = ... obj has imageHref which is NSString UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:obj.imageHref]]; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((self.frame.size.width/2) - (image.size.with/2), (self.frame.size.height / 2) - (image.size.height / 2), image.size.width, image.size.height)]; imageView.userInteractionEnabled = YES; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)]; [imageView addGestureRecognizer:tap]; [self addSubview:imageView]; } return self; } 
+7
source

By default, UIImageView has the UIImageView property set to NO .

do:

 [imageView setUserInteractionEnabled:YES]; 
+2
source

Source: https://habr.com/ru/post/909586/


All Articles