Touch Up Inside for a UIImageView - iphone

I'm trying to implement touch event similar to a Touch Up Inside a UIButton. I've seen some codes using touchesBegan:withEvent but it looks I need to slide a bit my finger over the screen. I actually just want to touch in the image
Code:
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
NSLog(#"Image touched!");
}
Regards!

You could use the gesture to get the touch on UIImageView.
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(ClickEventOnImage:)];
[tapRecognizer setNumberOfTouchesRequired:2];
[tapRecognizer setDelegate:self];
//Don't forget to set the userInteractionEnabled to YES, by default It's NO.
myImageView.userInteractionEnabled = YES;
[myImageView addGestureRecognizer:tapRecognizer];
Implement the ClickEventOnImage function.
-(void) ClickEventOnImage:(id) sender
{
}

UIImageView is notorious regarding this as its userInteractionEnabled is set to NO by default. You will have to enable it by doing
imageView.userInteractionEnabled = YES;
And then you can use gesture recognizers or the normal touches* methods to handle touches.

Related

I can not touch the image when using UIScrollView

I use a UIScrollView to show a serial of UIImageView(the UIImageView is added as subview of scrollview), the problem is that I have rewrote the method in UIImageView:
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
But when I click the image , the method is not called, it seems that the image did not receive the click event. What is the exact reason? How can I fix this?
(I have enable the exclusiveTouch of the UIImageView and the User Interavtion of all the view)
Set the UIImageView's exclusiveTouch and userInteractionEnabled properties to YES and try again. I find that subviews of scrollview's receive only some or no events, and setting exclusiveTouch allows the view to claim them immediately.
set
imageView.userInteractionEnabled = YES;
and if you just want a tap event for the imageview
just use the UITapGestureRecognizer
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tap:)];
[imageView addGestureRecognizer:tap];
then you must implement the method
- (void) tap:(UITapGestureRecognizer*)gesture
{
UIImageView *v = gesture.view;// that your ImageView tapped
}
imageView.userInteractionEnabled = NO;

UITapGestureRecognizer with couple of views

I am using :
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleViewClicked:)];
[tmp addGestureRecognizer:gestureRecognizer];
[gestureRecognizer release];
to get notification when a view(bigview for our example) i clicked(I have a lot of view), and in the front there is a UIView (a blank one) the he is in the front of the view (there is a reason why this view is in the front before al the views).
there is now a problem to get notification when tmp is tapped because the bigview is in the front.
there is any solution for something like this?
EDIT
In the bigview i have UISwipeGestureRecognizer:
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeRight:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[itemsView addGestureRecognizer:recognizer];
[recognizer release];
and if i make userInteractionEnabled in the bigview to NO he don't get notification on swipe
Two options:
A. Set userInteractionEnabled to NO on that big/blank view.
B. Implement pointInside:withEvent: on that big/blank view
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
// UIView will be "transparent" for touch events if we return NO
return NO;
}
(as taken from allowing-interaction-with-a-uiview-under-another-uiview)

Action on UILabel in iphone

I have created some set of labels programmatically to display on the screen and i want after clicking on label some action should perform.
Please don't suggest me about UIButton. I want to do it for UILabel. After clicking on the label another detail view should appear.
Please help me to solve this problem without using Inteface Builder.
Finally i came up with the solution and i got the result
[titleLbl setUserInteractionEnabled:YES];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(labelButton:)];
[tapGestureRecognizer setNumberOfTapsRequired:1];
[titleLbl addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];
make IBoutlet of your label,
implement touchesBegin in your controller, pull out the CGPoint - touchCoordinate, check
CGRectContainsPoint(label.frame,touchCoordinate)
{
//you got the touch action on your label
}
In
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
.......
.......
CGPoint touch;//Touch Location
if(CGRectContainsPoint([objectOfLable frame], [touch locationInView:self.view ]) )
{
Do What you Want
}
}
Try This

scrollview's subview does not get touch events when scroll view scrolls

I have a scrollView onto which some imageViews are added.
I want that when scrollView scrolls then also the imageViews should get touch events.
But it is not happening in my case.
Only one of the two things happen at a time.
Either the scrollView scrolls but the imageViews do not get touch events
or the imageViews get touch events and the scrollView does not get scrolled.
Can't both of the things happen simultaneously.
Please suggest.
You can use "Tap Gesture Recognizer" to get the events on your images while it's being added to the scroll view.
EDIT
You can refer to this code:
-(void)yourGestureRecognizer
{
UITapGestureRecognizer *yourTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
[yourImageView addGestureRecognizer:yourGestureRecognizer];
[yourGestureRecognizer release];
}
You can handle tap here:
-(void)handleTap:(UIGestureRecognizer *)sender{
UIImageView *imageView = (UIImageView *)sender.view;
switch (imageView.tag)
{
case 1:
m_pYourInstance=[[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
[self.navigationController pushViewController:XXXXX animated:YES];
break;
}
Hope this helps !!
I know this was posted a while ago, but...
By default, UIImageView objects have their property userInteractionEnabled set to NO. This could be why touches aren't registering. This has tripped me up on more than one occasion and it's one of those little things that only experience will help.
Set it to YES and see if that fixes it.
Use UIGestureRecognizer. Add the recognizer to your image views.
And if you are targeting some old iOS version, have a look at this link.
yes you can do both. you need to customize your uiimageview by sub classing it and have to add Tap Gesture Recognizer like :
in .h file
#interface CustomView : UIImageView
{
// your variables
}
in .m file
//when you create an instants of your image view UITapGestureRecognizer will added in all your instants.
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame]))
{
// add Gesture for tapping
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(tap:)];
tap.numberOfTapsRequired = 1;
[self addGestureRecognizer:tap];
}
return self;
}
then add delegate methods in .m file too
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *touch = [touches anyObject];
}
now in your custom cell, add your custom image view with Tap Gesture Recognizer
hope it will help you...
Use UIButtons with background images set on them. Here's the loop you use to layout UIScrollView subviews:
for (int i = 0; i < [images count]; i++) {
CGRect aFrame = ...;
UIButton *button = [[[UIButton alloc] initWithFrame:aFrame] autorelease];
[button setImage:[images objectAtIndex:i] forState:UIControlStateNormal];
button.tag = i;
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:button];
}
And here you process the touch event:
- (void)buttonClicked:(id)sender {
UIButton *button = (UIButton *)sender;
[self performActionWithImageAtIndex:[images objectAtIndex:button.tag]];
}
That will omit the need to create additional gesture recognizers and will lower the amount of boilerplate work.
-- edit
The other reason your UIImageViews are not detecting the touches is because you create them in code and forget to set userInteractionEnabled to YES.

touch events on UIimageView which is on UIwindow

I m using UIimageview on top of UIWindow. I want to handle touch event for that Imageview, but m not able to do it.
When i use UIimageView on UIview, we can use touchBegan, touchEnded but how to handle it when it is on UIWindow instead of UIView.
Thanks..
If you just want to handle the tap, you have to set,
imageView.userInteractionEnabled = YES;
By default UIImageView has the userInteractionEnabled property set to NO. And you can add a tap gesture recognizer to it,
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(anyMethod)];
[imageView addGestureRecognizer:tgr];
[tgr release];
If you want to move the image, you can use UIPanGestureRecognizer.
- (void)sendEvent:(UIEvent *)event
try this function