【问题标题】:Pass outer recognizers if inner didn't receive the touch如果内部没有收到触摸,则通过外部识别器
【发布时间】:2013-03-04 14:36:43
【问题描述】:

我有一个视图,上面有一个以编程方式绘制的圆圈。我还有一个手势识别器,它代表检查它是否应该接收触摸(如果在这个圆圈上进行了点击,它应该接收)。

- (void)awakeFromNib
{
    [super awakeFromNib];

    circleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toggleHighlighted:)];
    [self addGestureRecognizer:circleTapRecognizer];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    CGPoint hit = [touch locationInView:self];

    if (gestureRecognizer == circleTapRecognizer) {
        BOOL hitsCircle = hit.x >= CGRectGetMidX(self.bounds) - self.circleRadius &&
                          hit.x <= CGRectGetMidX(self.bounds) + self.circleRadius &&
                          hit.y >= CGRectGetMidY(self.bounds) - self.circleRadius &&
                          hit.y <= CGRectGetMidY(self.bounds) + self.circleRadius;

        return hitsCircle;
    }

    return YES;
}

但是,如果在此圆圈之外的空间上进行了点击,我希望超级视图能够接收到触摸。我怎样才能做到这一点?当然,我可以调用一个名为 tappedNotOnCircle 的委托方法调用 superview 的逻辑,但我想知道是否有更简单的方法。

【问题讨论】:

  • 很高兴看到您选择了没有 GestureRecognizer 的版本。保持简单。 :) 你能回答为什么你改变主意了吗? (只是为了我的兴趣)
  • @MarkKryzhanouski 因为手势识别器不起作用。有一次,当主题中的方法返回 NO 时,底层视图的识别器没有调用,什么也没发生。

标签: ios uikit uigesturerecognizer


【解决方案1】:

有没有 GestureRecognizer 的解决方案。只需覆盖 -(BOOL)[UIView pointInside:(CGPoint)point withEvent:(UIEvent *)event]

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    BOOL hitsCircle = hit.x >= CGRectGetMidX(self.bounds) - self.circleRadius &&
                      hit.x <= CGRectGetMidX(self.bounds) + self.circleRadius &&
                      hit.y >= CGRectGetMidY(self.bounds) - self.circleRadius &&
                      hit.y <= CGRectGetMidY(self.bounds) + self.circleRadius;

    return hitsCircle;
}

为了更好地理解,请参阅 Apple 文档Defining a Custom View

【讨论】:

    猜你喜欢
    • 2020-04-07
    • 2018-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多