find which view was clicked - iphone

I have a view controller where there are several uiview object. I need to know on which uiview user have tapped. how is this possible? any guidance will help a lot....
Thanks
Pankaj

Here is what you can do to get what you wanted ..... In this example i have created 7 views
UITapGestureRecognizer* gestureRecognizer;
UIView* myView;
for (int i = 0; i < 8; i++)
{
gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doSomthing:)];
gestureRecognizer.numberOfTapsRequired = 1;//or what ever you want
myView = [[UIView alloc] initWithFrame:CGRectMake(10, i*30, 30, 28)];
myView.backgroundColor = [UIColor redColor];
myView.tag = 100+i;
[self.view addSubview:myView];
[myView addGestureRecognizer:gestureRecognizer];
[myView release];
[gestureRecognizer release];
}
Now you need to implement the method like this
-(void)doSomthing:(id)sender
{
UIView* temp = [(UITapGestureRecognizer*)sender view];
// here you get the view you wanted
NSLog(#"view number :%d",temp.tag);
}
I think this should help you

Set a tag for every view to keep track of them.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// We only support single touches, so anyObject retrieves just that touch from touches
UITouch *touch = [touches anyObject];
NSLog(#"view %i", [touch view].tag);
}

you can add gestures to the uiview objects to find which object has been touched. see the documentation.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIGestureRecognizer_Class/Reference/Reference.html
for specific code just comment.

U can probably add a custom button with a tag on top of each view. Then u can know which view is tapped based on the button tag.
pls take a look at this. It may help.
http://www.iphonedevsdk.com/forum/iphone-sdk-development/13041-touch-event-subview.html

Related

Detect touch of UIImage added as subview of UIScrollView

I have UIScrollView with many UIImageViews.I need to drag and drop image from UIScrollView to another view. Outside the scrollView touch is worked. But inside scroll view touch is not worked. I used touchesBegan,touchesMoved etc method. Please help me.
-(IBAction)selectBut:(id)sender
{
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(x,y,w,h)];
scrollView.userInteractionEnabled = YES;
int y = 0;
for (int i = 0; i < [myArray count]; i++) {
UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, y, 75, 30)];
image.userInteractionEnabled = YES;
y=y+35;
[scrollView addSubview:image];
}
[self.view addSubview:scrollView];
[scrollView setContentSize:CGSizeMake(150, 300)]
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 1) {
NSLog(#"One touch !!");
}
}
you need to customize the UIImageView with your own view inherited from UIImageView. Provide touch methods in that customized subclass and add it on your UIScrollView..
Subviews of UIScrollview will never call touchesBegan method directly. You need to customized with subview to get touchesBegan properties of that added subview/customized view.
I mean to say take subclass of ImageView like
CustomImageView *imageView = [[CustomImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
[imageView setUserInteractionEnabled:YES];
[scrollView addSubview:imageView];
[imageView release];
CustomImageView class is should be inherited from UIImageView like
#interface CustomImageView : UIImageView
{
}
in # .m File
#import "CustomImageView.h"
#implementation CustomImageView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"%s", __FUNCTION__);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2) {
drawImageView.image = nil;
return;
}
}
Add a Tap gesture recognizer to the scroll view for enabling touch inside scroll view:
UITapGestureRecognizer *singlTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(methodName:)];
singlTap.cancelsTouchesInView = NO; //Default value for cancelsTouchesInView is YES, which will prevent buttons to be clicked
[scrollViewName addGestureRecognizer:singlTap];
then write your code in specified method.
-(void)methodName:(UITapGestureRecognizer *)gesture
{
//your code here
}
I don't know if this help you. But try to set the exclusiveTouch property of the UIImageView to YES.
Did you add your scrollview as an IBOutlet? If its from xib and as you say touches are available outside the scroll and not within, I guess you might have accidentally unchecked userInteractionEnabled for the scrollview which inhibits touch events. And you have added UIImageViews as subviews to UIScrollView. For UIImageView you have to first set userInteractionEnabled to YES and then add any gestures if necessary to get events or use the touchesBegan, touchesMoved methods. Unless you set interaction enabled you wont receive touch events on UIImageView. If the parent view I mean the UIScrollView's interaction is disabled you wont get touches on UIImageView too. Hope this helps :)

Selecting images and then moving them

I am trying to figure out how to select an object first then I am able to move it. For example I want to select cow first by touching it then I am able to move it. The reason being is when I am touching the screen it is moving both cow and cow1. When I only want to move one cow at a time. Any help will be appreciated.
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];
cow.center = CGPointMake(touchLocation.x, touchLocation.y);
cow1.center = CGPointMake(touchLocation.x, touchLocation.y);
}
Try using UIPanGestureRecognizer, like this:
#synthesize myImage; //UIImageView
- (void)viewDidLoad
{
[super viewDidLoad];
[self startMoveImage];
}
-(void) startMoveImage{
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(pan:)];
[self.view addGestureRecognizer:panGestureRecognizer];
}
- (void)pan:(UIPanGestureRecognizer *)gesture
{
if ((gesture.state == UIGestureRecognizerStateChanged) ||
(gesture.state == UIGestureRecognizerStateEnded)) {
CGPoint position = [gesture locationInView:[myImage superview]];
[myImage setCenter:position];
}
}
There are many ways to implement this sort of thing. It depends on how your graphics are implemented, amongst other things, check out this Apple sample code:
http://developer.apple.com/library/ios/#samplecode/Touches/Introduction/Intro.html
The standard solution would be to add separate gesture recognizers for your two cow objects, e.g., in viewDidLoad:
UIPanGestureRecognizer *panCow1 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(moveCow:)];
[cow1 addGestureRecognizer:panCow1];
// if non ARC, make sure to add a line that says
// [panCow1 release];
UIPanGestureRecognizer *panCow2 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(moveCow:)];
[cow2 addGestureRecognizer:panCow2];
// if non ARC, make sure to add a line that says
// [panCow2 release];
And then you have your moveCow method look something like:
- (void)moveCow:(UIPanGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateChanged)
{
CGPoint translate = [sender translationInView:self.view];
sender.view.center = CGPointMake(sender.view.center.x + translate.x, sender.view.center.y + translate.y);
}
}
I haven't tested this code, but you get the idea. This is how I generally move separate subviews around...

Touch event on uimageview added as subview to parent view

I have added the view programmatically in the main view. And in the parent view i am adding imageview as a subview programmatically. Now I have set the touch event for imageview on click but its not working. It is not detecting the touch event on click. I am using the following code of touch event of imageview.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == originalphoto)
{
[fullview setHidden:NO];
[self.view addSubview:fullview];
[setimage setImage:image];
}
}
Do anyone has idea that how to set touch event of imageview which has been added programmatically?
Thanks alot.
Why dont you use a gesture? it is lot easier. You could use the UITapGestureRecognizer and let it work for you: http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITapGestureRecognizer_Class/Reference/Reference.html
//Add gesture to your view
UITapGestureRecognizer *Tap= [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(ResignResponder:)];
[tempV addGestureRecognizer:Tap];
[Tap release];
//method will be called on tapping the view
-(void)ResignResponder:(UIGestureRecognizer*)recognizer {
[LocSearchField resignFirstResponder];
[self.view sendSubviewToBack:tempV];
}
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:UIIMAGEVIEW];// uiimageview object
touchx = location.x;
touchy = location.y;
NSLog(#" x= %i ", touchx);
NSLog(#"y = %i",touchy);
Maybe you need to set property imageView.userInteractionEnabled = YES?

How can i associate an IBAction event with an image?

I have a application which contains a moving image and i want to animate the image using a my custom animation.
How can i associate an IBAction event with the image?
How can i detect the coordinates on screen where the user touched the image?
Please Give Your Suggestions.
Thanks in advance and Your Suggestions are Most Welcome.
You could use custom UIButtons with your image. All the touch handling code is already builtin.
Edit: UIGestureRecognizer also work on UIImageViews:
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];
You can also do this way:
first define the rect where you image will be:
touchRect=CGRectMake(screen.width/2-screen.width/8, 0, screen.width/4, screen.height/5);
then with touchesEnded method you can define what you want to do when you touch over that zone:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint targetPoint = [[touches anyObject] locationInView:self];
if (CGRectContainsPoint(touchRect, targetPoint)){
//YOUR CODE HERE
}
}

touchesBegan doesnt get detected

I have a viewcontroller like the following. But the touchsBegan doestnt get detected.
Can anyone plz tell me what is wrong.
- (id)init
{
if (self = [super init])
self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
return self;
}
-(void) viewWillAppear:(BOOL)animated
{
overlay = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"overlay.png"]] autorelease];
[self.view addSubview:overlay];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Detect touch anywhere
UITouch *touch = [touches anyObject];
// Where is the point touched
CGPoint point = [touch locationInView:self.view];
NSLog(#"pointx: %f pointy:%f", point.x, point.y);
// Was a tab touched, if so, which one...
if (CGRectContainsPoint(CGRectMake(1, 440, 106, 40), point))
NSLog(#"tab 1 touched");
else if (CGRectContainsPoint(CGRectMake(107, 440, 106, 40), point))
NSLog(#"tab 2 touched");
else if (CGRectContainsPoint(CGRectMake(214, 440, 106, 40), point))
NSLog(#"tab 3 touched");
}
Touches are only called on UIViews. This means you have to subclass UIView and put your touchesBegan:withEvent: code in this subclass. You're using the code inside an UIViewController and this doesn't get any touches itself because it's not an object on screen. A controller is just for the application logic behind a view.
As chriB said touches are detected only on views and as u access it in a controller its not detected.
also FYI, the touches wont be detected on any views you add over the base view.
ie: if u have a view and u add a scroll view or a table view over it, then the touches wont be detected over this added regions. same goes for buttons, textfields etc. Touch is detected only wen its done exactly on the base view.