如何快速裁剪圆的外部区域? [英] How to clip outer area of circle in swift?
问题描述
我已经使用 UIBezierPath
在 swift 中绘制了半圆,父对象是 UIImageView
现在我想剪辑圆的外部区域.我试过 clipsToBounds
但没有用.
I have drawn semi circle in swift using UIBezierPath
the parent is UIImageView
now i am looking to clip the outer area of circle. I have tried clipsToBounds
but it didn't works.
let path = UIBezierPath(arcCenter: CGPoint(x: -60, y: imgView.frame.size.height/2),
radius: imgView.frame.size.width*1.3,
startAngle: CGFloat(90).toRadians(),
endAngle: CGFloat(270).toRadians(),
clockwise: false)
let shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.strokeColor = UIColor.blue.cgColor
shapeLayer.lineWidth = 5
imgView.clipsToBounds = true
imgView.layer.addSublayer(shapeLayer)
extension CGFloat {
func toRadians() -> CGFloat {
return self * .pi / 180
}
}
1 我尝试过 imgView.layer.mask = shapeLayer
但它消除了 strokeColor
1 i have tried imgView.layer.mask = shapeLayer
but it wipes out strokeColor
推荐答案
形状图层要么绘制在另一个图层的顶部,要么在将其用作蒙版时剪裁另一个图层.它不能同时进行.
A shape layer either draws on top of another layer or, if you use it as a mask, clips another layer. It can't do both at the same time.
您需要两个形状层.保留您现在拥有的蓝色描边,作为图像视图层的子层.
You need two shape layers. Leave the one you have now, stroked in blue, as a sublayer of your image view layer.
创建第二个形状图层,在其中安装另一个圆形路径,将其填充颜色设置为任何不透明颜色,然后使用第二个形状图层作为图像视图上的蒙版.您可能需要向遮罩层添加描边颜色和边框宽度,使其足够大,不会遮住蓝色圆圈.(我忘记了描边是画在其形状边界的内侧、外侧还是居中.我认为它们是居中的,因此除非您还以相同的厚度对遮罩层进行描边,否则它会剪掉蓝色圆圈的外半部分形状层,但我必须尝试确定.)
Create a second shape layer, install another circle path into it, set it's fill color to any opaque color, and use that second shape layer as a mask on your image view. You might need to add a stroke color and borderWidth to your mask layer so it is big enough not to mask your blue circle. (I forget if strokes are drawn inside, outside, or centered on the edge of their shape boundaries. I think they are centered, so unless you also stroke your mask layer at the same thickness, it will clip the outer half of your blue circle shape layer, but I'd have to try it to be sure.)
这篇关于如何快速裁剪圆的外部区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!