views:

10158

answers:

6

Hi,

I have placed an image (UIImageView) on the navigation bar. Now I want to detect the touch event and want to handle the event. Could you let me know how can I do that?

Thanks.

+3  A: 

What you might want to do is override the touchesBegan:withEvent: method of the UIView (or subclass) that contains your UIImageView subview.

Within this method, test if any of the UITouch touches fall inside the bounds of the UIImageView instance (let's say it is called imageView).

That is, does the CGPoint element [touch locationInView] intersect with with the CGRect element [imageView bounds]? Look into the function CGRectContainsPoint to run this test.

Alex Reynolds
Thanks for your reply. I have added the view like this:[navBar addSubview:self.pImage]; // added as a subview of the navigation bar. I have overriden the function but the function is never called. I have set the property userInteractionEnabled of the UIImageView to YES though. Am I missing anything?
ebaccount
Check out the touchesBegan:withEvent: method. There are plenty of examples on search engines of how this works.
Alex Reynolds
+18  A: 

A UIImageView is derived from a UIView which is derived from UIResponder so it's ready to handle touch events. You'll want to provide the touchesBegan, touchesMoved, and touchesEnded methods and they'll get called if the user taps the image. If all you want is a tap event, it's easier to just use a custom button with the image set as the button image. But if you want finer-grain control over taps, moves, etc. this is the way to go.

You'll also want to look at a few more things:

  • Override canBecomeFirstResponder and return YES to indicate that the view can become the focus of touch events (the default is NO).

  • Set the userInteractionEnabled property to YES. The default for UIViews is YES, but for UIImageViews is NO so you have to explicitly turn it on.

  • If you want to respond to multi-touch events (i.e. pinch, zoom, etc) you'll want to set multipleTouchEnabled to YES.

Ramin
+13  A: 

In practical terms, don't do that.

Instead add a button with Custom style (no button graphics unless you specify images) over the UIImageView. Then attach whatever methods you want called to that.

You can use that technique for many cases where you really want some area of the screen to act as a button instead of messing with the Touch stuff.

Kendall Helmstetter Gelner
I have added a button at the navigation bar and I can also handle touch events. However, I want to add an round image on this button. Could you let me know how can I do that programmatically? Also, could you let me know how can I set the button property to "custom" programmatically?
ebaccount
Look at the docs for UIButton - you need to set background images for a control state, you'll want different images for Normal vs. Selected/Highlighted.The button style is what you set to UIButtonStyleCustom, I believe - again, look at the docs for the style property on UIButton.
Kendall Helmstetter Gelner
Isn't adding a UIButton on top of the UIImageView a little over the top? You're adding an extra object, whereas you could just implement the touches methods of the already existing UIImageView object. No?
Sam V
If you look at the size of a UIButton in memory, it's virtually nothing and you only have a few per screen.What you get for free is the whole target action wiring for different states (like touch up inside) and also nice behavior like not triggering when you press down but slide a finger to the side.Basically buttons are one of the most finely crafted objects in the whole system and to think you are going to do custom touch code that works better is madness. Use that which is cheap and works well, save the custom work for things that the frameworks don't already handle for you.
Kendall Helmstetter Gelner
A: 

Hi there, can you tell me how i would turn on UIImageView for touch events. I'm new to xcode and i'll need a really detailed instruction.

muhammed
A: 

Thanks in advance

muhammed
+2  A: 

Instead of making a touchable UIImageView then placing it on the navbar, you should just create a UIBarButtonItem, which you make out of a UIImageView.

First make the image view:

UIImageView *yourImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"nameOfYourImage.png"]];

Then make the barbutton item out of your image view:

UIBarButtonItem *yourBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:yourImageView];

Then add the bar button item to your navigation bar:

self.navigationItem.rightBarButtonItem = yourBarButtonItem;

Remember that this code goes into the view controller which is inside a navigation controller viewcontroller array. So basically, this "touchable image-looking bar button item" will only appear in the navigation bar when this view controller when it's being shown. When you push another view controller, this navigation bar button item will disappear~

yujean