2014-05-06 15 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