Ios 在你的视野中挖一个洞

Ios 在你的视野中挖一个洞,ios,xcode,Ios,Xcode,我正在尝试使用UIView作为遮罩添加到另一个UIView前面。我需要这个来创建我的应用程序的教程。代码如下: UIView *baseView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; //self.view = baseView; [self.view addSubview:baseView]; [baseView setBackgroundColor:[UIColor blackCo

我正在尝试使用UIView作为遮罩添加到另一个UIView前面。我需要这个来创建我的应用程序的教程。代码如下:

UIView *baseView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
//self.view = baseView;
[self.view addSubview:baseView];
[baseView setBackgroundColor:[UIColor blackColor]];
baseView.userInteractionEnabled = YES;
baseView.alpha = 0.5;

CAShapeLayer *mask = [[CAShapeLayer alloc] init];
mask.frame = baseView.layer.bounds;
CGRect biggerRect = CGRectMake(mask.frame.origin.x, mask.frame.origin.y, mask.frame.size.width, mask.frame.size.height);
CGRect smallerRect = CGRectMake(200.0f, 500.0f, 300.0f, 200.0f);

UIBezierPath *maskPath = [UIBezierPath bezierPath];
[maskPath moveToPoint:CGPointMake(CGRectGetMinX(biggerRect), CGRectGetMinY(biggerRect))];
[maskPath addLineToPoint:CGPointMake(CGRectGetMinX(biggerRect), CGRectGetMaxY(biggerRect))];
[maskPath addLineToPoint:CGPointMake(CGRectGetMaxX(biggerRect), CGRectGetMaxY(biggerRect))];
[maskPath addLineToPoint:CGPointMake(CGRectGetMaxX(biggerRect), CGRectGetMinY(biggerRect))];
[maskPath addLineToPoint:CGPointMake(CGRectGetMinX(biggerRect), CGRectGetMinY(biggerRect))];

[maskPath moveToPoint:CGPointMake(CGRectGetMinX(smallerRect), CGRectGetMinY(smallerRect))];
[maskPath addLineToPoint:CGPointMake(CGRectGetMinX(smallerRect), CGRectGetMaxY(smallerRect))];
[maskPath addLineToPoint:CGPointMake(CGRectGetMaxX(smallerRect), CGRectGetMaxY(smallerRect))];
[maskPath addLineToPoint:CGPointMake(CGRectGetMaxX(smallerRect), CGRectGetMinY(smallerRect))];
[maskPath addLineToPoint:CGPointMake(CGRectGetMinX(smallerRect), CGRectGetMinY(smallerRect))];

mask.path = maskPath.CGPath;
[mask setFillRule:kCAFillRuleEvenOdd];
mask.fillColor = [[UIColor blackColor] CGColor];
baseView.layer.mask = mask;

这样我就有了一个带矩形孔的黑色透明UIView。我想这样做,如果用户触摸UIView中除切割部分以外的任何其他地方,什么都不会发生。如果用户触摸切割零件中的任何位置,应如同用户触摸其下方的UIView一样。这是可能的吗?

您可能希望用event:(UIEvent*)事件覆盖
-(BOOL)pointInside:(CGPoint)point用作覆盖的视图的方法

从这里您可以看到该点是否在切割区域内,是否为返回编号

你可以看到这里有更多的信息


合理解释pointInside:withEvent和hitTest:withEvent之间的关系

你能详细解释一下吗?我从来没有遇到过这种方法。我用更多的金块更新了答案,这应该有助于解释这种方法是如何涉及到确定哪个视图应该响应触摸的。谢谢,是的,这种方法正是我想要的!查看此类似问答帮助:[在UIView中剪切透明孔][1][1]: