Obviously I missunderstood UIImageView vs UIImage, in order to register gesture(such as tap/click) on image, i need to have image in uiimageview and register gesture on uiimageview not on 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 the centered image, but useless because I want it to be clickable so I though just do it with UIImageview instead 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 kind of action when clicked, how hard can this be?
EDIT :
I'm actually implementing :
https://github.com/andreyvit/SoloComponents-iOS/tree/master/ATPagingView
Which offers neat page scroll. But with clickable images in the middle of screen not page 1, 2 etc ..
EDIT 2 :
Any perspective on this today?
It's not possible to insert a hyperlink within a PNG or JPG image. Instead, you would hyperlink the entire image which would be done outside of Snappa. If you create a Facebook Ad for example, the entire image will automatically be hyperlinked.
You don't need custom drawing (drawRect:
) for this. Just add the UIImageView
as a subview when you load the view (in Interface Builder or in loadView
depending on how you're initializing your view). You can then add UITapGestureRecognizer
to that view as you're doing.
As @jackslash notes, you also need to turn on user interaction for the image view once you've done that.
I believe you're making this much more complex than it is. There is no need for custom drawing with drawRect:
. There is no need for subclassing UIImageView
(the link @madmik3 references is not relevant to iOS4+). There probably is no need for an actual UIView
subclass (you can do all of this inside the view controller), but here's how you would do it in a view subclass:
- (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;
}
By default UIImageView
has its property userInteractionEnabled
set to NO
.
do:
[imageView setUserInteractionEnabled:YES];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With