ios UIBezier路径带圆角矩形的Bezier路径:cornerRadius值不一致

guykilcj  于 2022-12-05  发布在  iOS
关注(0)|答案(1)|浏览(135)

我想在CALayer中使用[UIBezierPath bezierPathWithRoundedRect:cornerRadius:],在设置矩形的cornerRadius时,我发现它从0到最大半径(大约是图层边界的一半)并不一致。该值在最大半径的1/3处跳变,这让人很困惑。经过一番研究,我发现可能是iOS7风格的UIBezierPath绘制圆形矩形的bug。PaintCode's research on that。所以我的问题是如何绘制一个旧风格的完美圆形矩形,并保持一致的cornerRadius值变化?

qacovj5a

qacovj5a1#

看了看你发布的PaintCode链接(虽然,在这里回答问题更容易,而不必去其他地方阅读文章)...
是的,当半径大于矩形尺寸的1/2的1/3时,UIBezierPath(roundedRect: ...)似乎仍然有问题。
要“手动”创建圆角矩形路径,我们可以这样做(使用该链接的矩形尺寸和圆角半径):

let r: CGRect = CGRect(x: 0, y: 0, width: 150.0, height: 153.0)
let cornerRad: CGFloat = 50.0

// center points for the corner arcs
let ptCTR: CGPoint = CGPoint(x: r.maxX - cornerRad, y: r.minY + cornerRad)
let ptCBR: CGPoint = CGPoint(x: r.maxX - cornerRad, y: r.maxY - cornerRad)
let ptCBL: CGPoint = CGPoint(x: r.minX + cornerRad, y: r.maxY - cornerRad)
let ptCTL: CGPoint = CGPoint(x: r.minX + cornerRad, y: r.minY + cornerRad)
    
let bez: UIBezierPath = UIBezierPath()
    
// Top-Right corner
bez.addArc(withCenter: ptCTR, radius: cornerRad, startAngle: .pi * 1.5, endAngle: .pi * 0.0, clockwise: true)
    
// Bottom-Right corner
bez.addArc(withCenter: ptCBR, radius: cornerRad, startAngle: .pi * 0.0, endAngle: .pi * 0.5, clockwise: true)
    
// Bottom-Left corner
bez.addArc(withCenter: ptCBL, radius: cornerRad, startAngle: .pi * 0.5, endAngle: .pi * 1.0, clockwise: true)
    
// Top-Left corner
bez.addArc(withCenter: ptCTL, radius: cornerRad, startAngle: .pi * 1.0, endAngle: .pi * 1.5, clockwise: true)
    
// close the path
bez.close()

相关问题