Question

I have add one UIView ("Box") in my view and I want detect touch in it.

I searching in google and this site and find many answer but I want that when touch on Box View (only box View) show me in console NSLog("1") and when touch on my view except Box place (in other to out of Box View my console Show me NSLog("2"))

I'm so confused please help me. this is my code:

@interface RootViewController : UIViewController

@property (nonatomic,strong) UIView *Box;
@end

@implementation RootViewController
@synthesize window,Box;
- (void)viewDidLoad
{
        [super viewDidLoad];

    Box = [[UIView alloc] initWithFrame:CGRectMake(53.0, 100.0, 214, 124)];
    [Box setBackgroundColor:[UIColor greenColor]];
    [self.view addSubview:Box];

}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint locationPoint = [[touches anyObject] locationInView:self.view];

}
Was it helpful?

Solution

my friend you can use this code for detect view that you touched this :

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

    UITouch *touch=[[event allTouches] anyObject];
    if([touch view]== Box)
    {
        NSLog(@"2");
    }
    else
        NSLog(@"1");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top