使用贝塞尔path作为剪贴蒙版

我想知道是否有可能将视图剪辑到贝塞尔path。 我的意思是,我只想看到封闭的贝塞尔path内的地区的景色。 其原因是我有一个不规则形状的轮廓,我想从上到下逐渐用一个纯色填充形状。 如果我可以使某个视图只在path中可见,那么我可以简单地创build一个我想要的颜色的UIView,然后根据需要更改其框架的y坐标,从而有效地填充形状。 如果任何人有什么更好的想法如何实现这将不胜感激。 为了logging形状的填充将匹配用户手指的y值,所以它不能是连续的animation。 谢谢。

更新(很长时间后):

我试过你的答案,罗布,除了一件事外,它的效果很好。 我的意图是将掩码移动到屏幕上的同一位置。 这样我就可以给人以视觉上“填满”面具的印象。 问题是,根据你的回答我写的代码,当我移动视图时,面具随之移动。 我知道这是可以预料的,因为我所做的只是把它作为视图的掩码,所以如果它绑定的东西移动,它就会移动。 我试图添加面具作为superview的子层,以便它保持放置,但是那有非常奇怪的结果。 这是我的代码:

self.test = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)]; self.test.backgroundColor = [UIColor greenColor]; [self.view addSubview:self.test]; UIBezierPath *myClippingPath = [UIBezierPath bezierPath]; [myClippingPath moveToPoint:CGPointMake(100, 100)]; [myClippingPath addCurveToPoint:CGPointMake(200, 200) controlPoint1:CGPointMake(self.screenWidth, 0) controlPoint2:CGPointMake(self.screenWidth, 50)]; [myClippingPath closePath]; CAShapeLayer *mask = [CAShapeLayer layer]; mask.path = myClippingPath.CGPath; self.test.layer.mask = mask; CGRect firstFrame = self.test.frame; firstFrame.origin.x += 100; [UIView animateWithDuration:3 animations:^{ self.test.frame = firstFrame; }]; 

感谢您的帮助。

您可以通过将视图的图层蒙版设置为CAShapeLayer来轻松完成此CAShapeLayer

 UIBezierPath *myClippingPath = ... CAShapeLayer *mask = [CAShapeLayer layer]; mask.path = myClippingPath.CGPath; myView.layer.mask = mask; 

如果你还没有QuartzCore框架,你需要添加QuartzCore框架。


在Swift中…

 let yourCarefullyDrawnPath = UIBezierPath( .. blah blah let maskForYourPath = CAShapeLayer() maskForYourPath.path = carefullyRoundedBox.CGPath layer.mask = maskForYourPath 

只是Rob解决scheme的一个例子,有一个UIWebView作为一个名为smoothView的UIView的子视图 。 smoothView使用bezierPathWithRoundedRect来制作一个四舍五入的灰色背景(注意在右边)。 工作正常。

但如果smoothView只有正常的剪辑子视图,你会得到这个..

在这里输入图像说明

如果你做什么罗布说,你可以在smoothView 和所有子视图中的圆angular…

在这里输入图像说明

好东西。

Interesting Posts