iPhone:如何找出触摸的对象

时间:2012-03-07 12:37:44

标签: iphone

在我的UIViewController上,我有不同的UIViews,其中一些是我的自定义UIViews。如何知道哪个UIView被触及,我的习惯是不是?

2 个答案:

答案 0 :(得分:2)

您可以为每个视图设置标记。

view1.tag = y;
UITapGestureRecognizer *tapGesture = 
    [[UITapGestureRecognizer alloc] initWithTarget:self 
                                            action:@selector(singleTapGestureCaptured:)];
tapGesture.numberOfTapsRequired = 1;
[view1 addGestureRecognizer:tapGesture];

并在singleTapGestureCaptured方法中:

- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{ 
    [[gesture view] tag];
    NSLog(@"tap captured for view :%d", [[gesture view] tag]);
}

答案 1 :(得分:1)

您可以创建两个UIGestureRecognizers,然后您必须将手势识别器与您的观点相关联:

UITapGestureRecognizer * recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[view addGestureRecognizer:recognizer];

UITapGestureRecognizer * recognizerCustom = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapCustom:)];
[customView addGestureRecognizer:recognizer];

通过这种方式,您知道在调用handleTap:方法时,您的普通视图被触及,当您的handleTapCustom:被调用时,您的自定义视图被调用。