如何检测我触摸特定区域?

时间:2014-05-06 11:59:21

标签: objective-c uiview touch uitouch

我在视图中添加了一个UIView(“Box”),我希望在其中检测触摸。

我在google和这个网站上搜索并找到了很多答案,但我希望在触摸Box View(只有框查看)时显示我在控制台NSLog("1")中以及触摸我的视图除了盒子位置(在Box的其他位置查看我的控制台显示我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 个答案:

答案 0 :(得分:1)

我的朋友,您可以使用此代码检测您触摸过的视图:

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

    UITouch *touch=[[event allTouches] anyObject];
    if([touch view]== Box)
    {
        NSLog(@"2");
    }
    else
        NSLog(@"1");
}
相关问题