【发布时间】:2019-01-04 11:03:13
【问题描述】:
我想在裁剪矩形的周围绘制蒙版,它在角落有夹点,我试图用组合的 UIBezierPath 填充矩形内部,例如:4 个圆形和 1 个矩形,而不是将此路径分配给 CAShapeLayer 以将其用作在我看来子层。到目前为止,它填充所需区域只是重叠部分(当我们在矩形上附加圆圈时)未填充(所有角落的四分之一三角形),所以我尝试使用 combinedPath.usesEvenOddFillRule = false - combinedPath.fill() - combinedPath.close() 但它们都没有使用我!所以我需要填写这些部分也有人可以帮助我吗?
你可以从这个link查看图片
我的代码:
// draw mask
let path = UIBezierPath(rect: viewBackground.bounds)
let maskRect = CGRect(x: imgView.frame.origin.x + roiRect.minX * rate, y: imgView.frame.origin.y + roiRect.minY * rate, width: roiRect.width * rate, height: roiRect.height * rate)
// First apth is for rectangle
let rectPath = UIBezierPath(rect: maskRect)
path.append(rectPath)
// and these paths for corners
let cornerMaskCircleSize: CGFloat = circleSize/2
let maskTopLeftCornerPath = UIBezierPath(ovalIn: CGRect(x: maskRect.minX - cornerMaskCircleSize/2, y: maskRect.minY - cornerMaskCircleSize/2, width: cornerMaskCircleSize, height: cornerMaskCircleSize))
let maskTopRightCornerPath = UIBezierPath(ovalIn: CGRect(x: maskRect.maxX - cornerMaskCircleSize/2, y: maskRect.minY - cornerMaskCircleSize/2, width: cornerMaskCircleSize, height: cornerMaskCircleSize))
let maskBottomRightCornerPath = UIBezierPath(ovalIn: CGRect(x: maskRect.maxX - cornerMaskCircleSize/2, y: maskRect.maxY - cornerMaskCircleSize/2, width: cornerMaskCircleSize, height: cornerMaskCircleSize))
let maskBottomLeftCornerPath = UIBezierPath(ovalIn: CGRect(x: maskRect.minX - cornerMaskCircleSize/2, y: maskRect.maxY - cornerMaskCircleSize/2, width: cornerMaskCircleSize, height: cornerMaskCircleSize))
// Combining all of them in one path
path.append(maskTopLeftCornerPath)
path.append(maskTopRightCornerPath)
path.append(maskBottomRightCornerPath)
path.append(maskBottomLeftCornerPath)
path.usesEvenOddFillRule = true
path.fill()
path.close()
cropMask.removeFromSuperlayer()
cropMask.bounds = viewBackground.frame
cropMask.position = viewBackground.center
cropMask.path = path.cgPath
cropMask.fillRule = .evenOdd
cropMask.fillColor = UIColor.init(hexString: "#212f41").cgColor
cropMask.opacity = 0.6
viewBackground.layer.addSublayer(cropMask)
【问题讨论】:
标签: swift uikit drawing uibezierpath