如何制作UIImageView.clipsToBounds = NO允许用户交互超出边界



我目前有我的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的父视图类上实现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");
    }
}

最新更新