Stu*_*art 65
从iOS 3.2开始,您可以使用UIBezierPaths 的功能创建一个开箱即用的圆角矩形(其中只有您指定的角是圆角的).然后,您可以将其用作a的路径CAShapeLayer,并将其用作视图图层的蒙版:
// Create the path (with only the top-left corner rounded)
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds
byRoundingCorners:UIRectCornerTopLeft
cornerRadii:CGSizeMake(10.0, 10.0)];
// Create the shape layer and set its path
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = imageView.bounds;
maskLayer.path = maskPath.CGPath;
// Set the newly created shape layer as the mask for the image view's layer
imageView.layer.mask = maskLayer;
Run Code Online (Sandbox Code Playgroud)
就是这样 - 在Core Graphics中手动定义形状并没有搞乱,也没有在Photoshop中创建掩蔽图像.该层甚至不需要无效.应用圆角或更改为新角就像定义新角UIBezierPath并将其CGPath用作蒙版图层的路径一样简单.该方法的corners参数bezierPathWithRoundedRect:byRoundingCorners:cornerRadii:是位掩码,因此可以通过将它们"或"在一起来舍入多个角.
注 - 与CALayer renderInContext方法结合使用时,不会渲染图层蒙版.如果您需要使用此功能,请尝试使用以下内容进行圆角处理:只需要两个圆角?.
| 归档时间: |
|
| 查看次数: |
9062 次 |
| 最近记录: |