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