我有一个视图,并且以编程方式绘制了一个圆圈。此外,我还有一个手势识别器,它会委托检查是否应该接触(如果在这个圆圈上进行了点击则应该这样做。)
- (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的逻辑,但我想知道是否有更简单的方法。
答案 0 :(得分:1)
没有GestureRecognizer的解决方案。只需覆盖 - (BOOL)[UIView pointInside:(CGPoint)指向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