3

我正在尝试减少 UIButton 的触摸区域。这甚至可能吗?当用户触摸按钮并将其触摸拖到按钮外时,触摸事件应在按钮图形结束时立即停止。不幸的是,该区域比实际图形大得多。我发现了很多关于如何增加面积而不是如何缩小面积的事情。

谢谢你的帮助。

4

1 回答 1

1

我想出了一个解决方案。您可以子类化UIButton和覆盖touchesMoved:,以便它识别触摸在按钮之外时要结束。这是我的片段。

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch * touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self];

    if(!CGRectContainsPoint(self.bounds, touchPoint))
    {
        [super touchesEnded:touches withEvent:event];
    }
    else
    {
        [super touchesMoved:touches withEvent:event];
    }
}

这样做的缺点是,如果您离开按钮并再次返回,按钮将不会变为活动状态。但除此之外,我认为它应该可以正常工作。

于 2012-10-12T19:08:37.247 回答