How to achieve touch even for UIImageView in appDelegate?
You can use:
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(anyMethod)];
[imageView addGestureRecognizer:tgr];
[tgr release];
Before doing this, enable user interaction of UIimageView.
This might be helpful for you:
http://mithin.in/2009/08/26/detecting-taps-and-events-on-uiwebview-the-right-way/
Related
how can you delete an image by using touches in the iphone sdk? Im making a game with images so i was wondering if there is a way were if you touch an image it gets deleted from the view.
Yes. You can add UITapGestureRecognizer to your image view, and in the recognizer action you can get the image view by using recognizer.view propery. And you can delete/remove it from its super view by using [image removeFromSuperview] method.
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(onImageTapped:)];
[imageView addGestureRecognizer:tgr];
[tgr release];
And the onImageTapped: method looks like this,
- (void)onImageTapped:(UITapGestureRecognizer*)recognizer {
UIImageView *imgView = (UIImageView*) recognizer.view;
[imgView removeFromSuperview];
}
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.
In the UIView, there is several sub views of UIImageView objects.
Since I need to detect the touch events from the UIImageView and from the remaining area of the UIView.
Can I implement touch events in UIView and UIImagView simultaneously ?
Thanks.
Based on your comment, I think you should consider gesture recognizers. Use the UITapGestureRecognizer to identify and respond to taps on a view.
UITapGestureRecognizer *tapGesture;
tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapOnView:)];
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;
[view addGesture:tapGesture];
[tapGesture release];
...
tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapOnImage:)];
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;
[imageView addGesture:tapGesture];
[tapGesture release];
...
define -(void)handleTapOnView:(UITapGestureRecognizer*)gesture and -(void)handleTapOnImage:(UITapGestureRecognizer*)gestureand handle the touches there.
There are other kinds of gestures too. You can read more about them here.
I use below code and work for 'self.view' but doesn't work for imageview and for direction just can add one direction how to use four direction(right,left,uo,down)?
UISwipeGestureRecognizer *swipe =[[UISwipeGestureRecognizer alloc]initWithTarget:selfaction:#selector(sr)];
swipe.direction =UISwipeGestureRecognizerDirectionRight;
[imageview addGestureRecognizer:swipe];
[swipe release];
Delegates
touchesBegan:
touchesEnded:
//--------add this code in the imageview allocation-------//
imageview.userInteractionEnabled = YES;
for four direction you have to use two UISwipeGestureRecognizer:-
UISwipeGestureRecognizer *swipeRightLeft =[[UISwipeGestureRecognizer alloc]initWithTarget:selfaction:#selector(sr)];
[swipeRightLeft setDirection:(UISwipeGestureRecognizerDirectionRight |UISwipeGestureRecognizerDirectionLeft )];
[imageview addGestureRecognizer:swipeRightLeft];
[swipeRightLeft release];
UISwipeGestureRecognizer *swipeUpDown =[[UISwipeGestureRecognizer alloc]initWithTarget:selfaction:#selector(sr)];
[swipeUpDown setDirection:(UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown )];
[imageview addGestureRecognizer:swipeUpDown];
[swipeUpDown release];
Try enabling interaction:
yourImageView.userInteractionEnabled = YES;
and add some directions, something like this:
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;
If you are OK with your app only running on iOS 3.2+, the best way to do this is to use the UIGestureRecognizer API class. In fact, there is a UISwipeGestureRecognizer subclass (docs are here).
To do what you want, do something like this:
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(doSomething:)];
swipe.direction = UISwipeGestureRecognizerDirectionRight;
[yourImageView addGestureRecognizer:swipe];
[swipe release];
To change to any other direction, look at the docs link above - there is a constant with all four directions.
I would like to be able to add a new UIImageView of a certain size which should have a few UIGestureRecognizers detecting touch on the UIImageView.
How does one subclass UIImageView or how would this be best achieved?
I want the user to be able to add UIImageViews as they want at runtime and then the gestureRecognizer will allow them to manipulate the views.
Thanks
I think you just forgot to enable userInteractions.
UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"foo.png"]] autorelease];
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapAction:)] autorelease];
[imageView addGestureRecognizer:tapGesture];
[self.view addSubview:imageView];