如何使 UIImageView.clipsToBounds = NO 允许 userInteraction 越界?

How to make UIImageView.clipsToBounds = NO allow userInteraction outside of bounds?

我目前有我的 UIImageView.clipsToBounds = NO;,它工作得很好,除非我需要启用 userInteraction,这样如果触摸子视图我可以响应触摸。任何人都知道如何保持 UIImageView.clipsToBounds = NO; 但仍然允许用户在边界之外进行交互?

你必须实现 UIGestureRecognizerDelegate 的方法。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if (touch.view != YourImageView) { // accept only touchs on superview, not accept touchs on subviews
        return NO;
    }
    return YES;
}

启用 UIImageView 用户交互默认值 UIImageView userInteration 应被禁用。

yourImageView.userInteractionEnabled = YES;

处理点击 UIImageView 裁剪边界的动作。在 UIImageView 的超级视图 class 上实现 tochesBegan:withEvent:。检查触摸位置是否在 imageView 边界的范围内,然后您已经点击了该位置。为此,请执行以下操作:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint point = [[touches anyObject] locationInView:self.imageView];
    if(CGRectContainsPoint(self.imageView.bounds, point)) {
        // Your code on tap of UIImgeView.
        NSLog(@"image view is tapped");
    }
}

这可能对您有所帮助。