문제

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];

}
도움이 되었습니까?

해결책

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");
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top