1

我尝试将点击添加到 UIImageView,如下所示,但它不起作用!_img_compass_text_backgroundUIImageView

我发现另一个视图覆盖了我的视图,但大部分顶视图都是完全透明的,我怎样才能强制水龙头通过它?

换句话说,我怎样才能禁止视图使用水龙头?

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(locationNameTapped)];
tapGestureRecognizer.numberOfTapsRequired = 1;
_img_compass_text_background.userInteractionEnabled=YES;
[_img_compass_text_background addGestureRecognizer:tapGestureRecognizer];
4

5 回答 5

1

你说你imageView有另一个view在上面?

在这种情况下,您可以做很多事情:

  • 添加tapGesture重叠view,并在目标方法中,检查点击点是否包含在rect框架imageView中。使用locationInViewCGRectContainsPoint

  • 使用touchesBegan/Moved/Ended并检查imageView框架是否包含接触点containsPoint

编辑:

将点击手势添加到重叠视图,目标方法如下:

- (void) targetMethod:(UITapGestureRecogniser*)gesture{

    CGPoint tapPoint = [gesture locationInView:(the view that contains both the views)];

    if (CGRectContainsPoint(imageView.frame, tapPoint)){

        //your code

    }

}
于 2013-09-27T13:28:55.953 回答
0

您的代码是正确的,但是将 UIImageView 添加到其父视图或另一个视图覆盖它一定存在一些问题,如果另一个视图在它上面,请尝试此代码

[parentView bringSubviewToFront:childView];

或检查这是否正确添加到其父视图中

于 2013-09-27T18:18:35.867 回答
0

如果您有太多图像为它们设置标签并添加

- (void)viewDidLoad
    {
        [super viewDidLoad];


        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(locationNameTapped:)];
        tapGestureRecognizer.numberOfTapsRequired = 1;
        _background.userInteractionEnabled=YES;
         _background.tag = 0;
        [_background addGestureRecognizer:tapGestureRecognizer];
    }

    - (void)locationNameTapped:(UITapGestureRecognizer*)gesture
    {
          if(gesture.view.tag == 0)
        {
                      NSLog(@"image tapped");

        }
    }
于 2013-09-27T12:57:00.430 回答
0

使用 anIBOutlet链接您的图像:

@property (nonatomic, weak) IBOutlet UIImageView *background;

并将您的属性命名为不带下划线 _ 并使用 CamelCase 作为您的属性。

这段代码对我有用:

- (void)viewDidLoad
{
    [super viewDidLoad];


    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(locationNameTapped:)];
    tapGestureRecognizer.numberOfTapsRequired = 1;
    _background.userInteractionEnabled=YES;
    [_background addGestureRecognizer:tapGestureRecognizer];
}

- (void)locationNameTapped:(UITapGestureRecognizer *)gesture
{
    NSLog(@"image tapped");
}
于 2013-09-27T12:50:53.660 回答
0

可能是其他视图在您的上方UIImageView..在这种情况下,只需禁用上视图的用户交互,以便下方视图将获得触摸事件

于 2013-09-27T13:46:33.830 回答