【问题标题】:how to detect I touch on specific area?如何检测我触摸特定区域?
【发布时间】:2014-05-06 11:59:21
【问题描述】:

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

我在谷歌和这个网站上搜索并找到了很多答案,但我希望当触摸框视图(仅框视图)时在控制台NSLog("1") 中显示我,并且当触摸我的视图时 except 框地方(在其他到开箱即用的地方查看我的控制台给我看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];

}

【问题讨论】:

    标签: objective-c uiview touch uitouch


    【解决方案1】:

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

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
        UITouch *touch=[[event allTouches] anyObject];
        if([touch view]== Box)
        {
            NSLog(@"2");
        }
        else
            NSLog(@"1");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-12
      • 1970-01-01
      • 2012-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多