Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect touch on subview

I have a Viewcontroller with two UIViews, one named textureView and one objectView.

When I only have one view I detect touch using this:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSArray *allTouches = [touches allObjects];

    for (UITouch *touch in allTouches)
    {
        gestureStartPoint = [touch locationInView:self.view];
            if([touch view] == controlUIImageview){
                   NSLog(@"TOUCH DETECT");
                }
        }
}

How do I write this part if I have some things I want to interact with in the textureView and some in the objectView?

Do I need to set the textureView andcontrolView to setUserInteractionEnabled:TRUE ?

Thanks!

like image 594
molle Avatar asked Jun 27 '11 08:06

molle


4 Answers

Other way to do this:

        gestureStartPoint = [touch locationInView:self.view];
        if(CGRectContainsPoint(textureView.frame, gestureStartPoint)){
            ...
        }
        if(CGRectContainsPoint(objectView.frame, gestureStartPoint)){
            ...
        }
like image 59
aknew Avatar answered Oct 22 '22 08:10

aknew


Try to add a tag for each subviews you want to add....like:

textureView.tag=0;
objectView.tag=1;

then:

for (UITouch *touch in allTouches)
    {
     if(touch.view.tag==0){
        //...do your stuff with textureView
     }else{
        //...do your stuff with objectView
     }
}
like image 20
Mat Avatar answered Oct 22 '22 09:10

Mat


Yes you need to do that, if you want to detect touch on any view you need to setUserInteractionEnabled:TRUE, or make enable in IB(if you are adding view's from IB).

like image 35
rptwsthi Avatar answered Oct 22 '22 08:10

rptwsthi


You can try the Singleton Approach

For example you have UIImageView as subview in TextureView

Make a class e.g object.h and Declare the subview you want to interact with E.g

UIImageView *DisplayPicture
UIView *View;

Once thats done. You can set Tags for each of the Two Views

TextureView.tag=0;
Objectview.tag=1;

Than use this method to get the Touch on the View and than Subviews

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];


if (touch.view.tag > 0) {


    NSLog(@"%d",[touch.view.subviews count]);

    //Get Reference for Texture view

    object.View=Touch.view;

    //the index will the position at which u added the Subview(UIImageView) e.g If its the first time you addsubview and index of the subview will be O.

    object.DipslayPicture=[touch.view.subviews objectAtIndex:0];

}else{
  //Similar for ObjectView

}
NSLog(@"tag=%@", [NSString stringWithFormat:@"%i", touch.view.tag]);
i=touch.view.tag;
}

Than you can use the object to do as you like e.g SetImage

[object.DisplayPicture setImage:@"happiness.png"];

It worked for me :) Hope it helps someone :)

like image 1
Araib karim Avatar answered Oct 22 '22 07:10

Araib karim