how to make my image clickable

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?

--------------Solutions-------------

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];

have a Look at UIGuestureRecognizer

Category:objective c Time:2012-02-28 Views:1

Related post

  • Making a Table Row clickable 2008-10-13

    I wonder what the best way to make an entire tr clickable would be? The most common (and only?) solution seems to be using JavaScript, by using onclick="javascript:document.location.href('bla.htm');" (not to forget: Setting a proper cursor with onmou

  • What is the best way to include the icon in the target region of clickable text? 2008-11-04

    I am currently using the following javascript to create an expanding menu: http://demo.raibledesigns.com/struts-menu/scripts/menuExpandable.js.src Currently, only the hyperlink text is clickable. What is the best way to have the icon next to the text

  • Rewrite this code: clickable divs 2008-11-10

    I'd like to change this: <a href='foo'> <div> Moo </div> </a> to be standards compliant (you're not supposed to have block elements in inline elements). Wiring javascript to the divs just for navigation seems like a hack and d

  • Converting Plain Text to Clickable link or Link to PlainText in asp.net 2008-11-12

    I need your advice with converting plain text to an URL. The scenario will be this: The user will select some entry and then click a "convert to link" button. The entry text the user selected will convert to (link: selected_text). I do it with JavaSc

  • What's the standard way to give a set of different clickable options all submitting the same form? 2008-11-19

    I have a PHP-generated page which is displaying some data about a set of films. The page is updated using POST. The form only shows films starting with a particular letter. I want to present a set of clickable options at the top of the screen, each o

  • How to use javascript onclick on a DIV tag to toggle visibility of a section that contains clickable links? 2009-01-04

    Hi I've got a DIV section that has only its title visible initially. What I would like to achieve is that when the visitor clicks anywhere on the area of toggle_section the toggle_stuff div toggles between visible/hidden. <div id="toggle_section"

  • How to make clickable color areas with rounded corners in WPF, Button or Border/Textarea? 2009-01-26

    I want to have four colored areas which are clickable. I can user TextBlock in a Border to get the color areas, but then neither of them have a Click event. So I can make the whole thing a button, but then it doesn't have rounded corners and I can't

  • Making an entire tab clickable 2009-02-12

    I'm making a template that is relying on a lot on a tabbed interface and in order to make it a bit more intuitive, I want to make sure that a user can click anywhere in the tab in order to activate it, instead of having to click the text inside the t

  • How can I make a ElementHost transparent so that the form behind it is visible and clickable? 2009-02-19

    I tried to derive a class form ElementHost and overiding the CreateParams method: protected override CreateParams CreateParams { get { const int WS_EX_TRANSPARENT = 0x20; CreateParams cp = base.CreateParams; cp.ExStyle = cp.ExStyle | WS_EX_TRANSPAREN

  • .net calendar - making the whole cell perform postback (clickable) 2009-03-06

    I've got a .net calendar up and running and bringing information from a database. By default the day number has a post back action applied to it. What I'm trying to do is have that action apply to the whole cell so the user doesn't need to click on j

  • Implementing clickable page numbers in search result 2009-04-06

    Classic scenario: Take user input, get a search-result and display it in pages to the user. I then need to display buttons for First, Next, Previous etc, and I maintain the users current page in viewstate. All is good, works fine. Then I need to impl

  • Clickable url link in NSTextFieldCell inside NSTableView? 2009-04-19

    I have a NSAttributedString that I'm using in a NSTextFieldCell. It makes several clickable url links and puts a big NSAttributedString inside the NSTextFieldCell. Whenever I am viewing the NSTextFieldCell normally and it's highlighted, I cannot clic

  • How can I make a background clickable 2009-05-07

    I've been asked to make a sponsored background (site takeover) for one of our sites and the question arose of whether I can make the logos in the tiled background clickable. My initial thought was 'no', but the more I think about it, the more I think

  • Central Clickable MSDN like Linux System/C/C++ Standard Library Documentation 2009-05-15

    If you are windows programmer and you want to program something new where you are going to use some new API with which you are not that familiar then you can type MSDN on your web browser and you get immediately what you need. Nicely grouped API func

  • how do I increase the clickable area of an icon in google maps 2009-05-20

    We are trying to increase the clickable area of a marker on a google map. The reason we want to do that clicking on the icon sometimes misses the anchor point. We rather not reduce the size of the icon size We are assuming that one of the properties

  • Why are Page Titles on some websites (including Stack Overflow) Clickable URLs? 2009-05-24

    Why on sites like Stack Overflow, Techcrunch, Smashingmagazine, etc. are the page titles (i.e. the text at the top of the page) clickable URLs that redirect to the same page that the user is on? Some examples: I believe that this does not effect SEO

  • Make clickable: Setting a element inside the on 100% width & height 2009-05-26

    I have a css problem that I'm stuck on.. I can't get my clickable header links to get a 100% height; without using javascript. I've tried many things I've found while googling but none of them fixed my problem. I get this result: This is my html:

  • Maximum clickable options #IE8 2009-05-26

    There seem to be a limitation in the maximum number of clickable options in a select element. If there is a select element with 3200+ options. Items from 3125 to N doesn't fire change event when they are clicked. However, items can be selected with t

  • Clickable slide show with images stored on a database 2009-05-27

    i have a table called images which consists of alot of images from property images to estate logos...i've written a ruby code to get all property images which works fine...the only problem i have is i want the images to display as a clickable slidesh

  • Clickable images (IMG's) but need the mouse icon to change over it 2009-06-28

    I have click-able images in my html page that call a javascript function... however nobody is clicking on them as they do not look click-able... how can I make them click-able without using an <a href=""></a> tag around it? Here is an exa

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.777 (s). 13 q(s)