所以这就是我到目前为止:
let indentTopToLeft: CGFloat = 50
let indentTopToBottom: CGFloat = 100
let path: CGMutablePathRef = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, 3, 0)
CGPathAddLineToPoint(path, nil, 200-indentTopToLeft, 0)
CGPathAddLineToPoint(path, nil, 200, indentTopToBottom)
CGPathAddLineToPoint(path, nil, 200 , 150)//4
CGPathAddLineToPoint(path, nil, 0, 150)
CGPathAddLineToPoint(path, nil, 0, 3)
//The rounded goes here. But I don't know how.
CGPathAddLineToPoint(path, nil, 3, 0)
CGPathCloseSubpath(path)
let maskLayer = CAShapeLayer()
maskLayer.path = path
self.mapView.layer.mask = maskLayer
现在我需要在这两点之间有一个圆角,但我不知道如何,请帮助。
CGPathAddLineToPoint(path, nil, 0, 3)
//HERE
CGPathAddLineToPoint(path, nil, 3, 0)
答案 0 :(得分:1)
有很多方法可以实现这一目标,但最直接的方法是使用CGPathAddArc
功能。请注意,这样做会使您离开0,3点,因此不需要为该点添加一行的代码。我希望修改后的绘图代码如下所示:
let indentTopToLeft: CGFloat = 50
let indentTopToBottom: CGFloat = 100
let topLeftCornerRadius: CGFloat = 3
let path: CGMutablePathRef = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, topLeftCornerRadius, 0)
CGPathAddLineToPoint(path, nil, 200-indentTopToLeft, 0)
CGPathAddLineToPoint(path, nil, 200, indentTopToBottom)
CGPathAddLineToPoint(path, nil, 200 , 150)//4
CGPathAddLineToPoint(path, nil, 0, 150)
CGPathAddLineToPoint(path, nil, 0, topLeftCornerRadius)
CGPathAddArc(path, nil, topLeftCornerRadius, topLeftCornerRadius, topLeftCornerRadius, CGFloat(M_PI + M_PI), CGFloat(M_PI + M_PI_2), false)
CGPathCloseSubpath(path)
CGPathAddArc
的参数已有详细记录;在零之后,我们得到弧的中心点的x和y位置,圆弧半径,起始角度和结束角度(以弧度表示),然后是一个值,表示是应该顺时针还是逆时针绘制。值得注意的是,我总是发现顺时针值和开始和结束角度不符合我的预期 - 这可能是因为CoreGraphics
的原点(左下角)不同于UIKit
(左上角) ,但我无法解释它。