2014-05-06 51 views
0

我在视图中添加了一个UIView(“盒子”),我想在它中检测触摸。如何检测我触摸特定区域?

我在谷歌搜索和这个网站,并找到很多答案,但我想,当上箱视图(仅中查看)触摸显示我在控制台NSLog("1"),当我的看法触摸除了箱的地方(在对方出的盒子查看我的控制台演示我NSLog("2")

我很困惑,请帮助我。这是我的代码:

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

} 

回答

1

我的朋友,你可以使用此代码检测认为你感动的:

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

    UITouch *touch=[[event allTouches] anyObject]; 
    if([touch view]== Box) 
    { 
     NSLog(@"2"); 
    } 
    else 
     NSLog(@"1"); 
} 
+0

感谢我的朋友:d – user3599133