我刚刚了解到标准 Apple .round
endcap
let l = CAShapeLayer()
l.path = .. just a straight line, say 50 long
l.lineWidth = say "3.1" in the example shown here
l.lineCap = .round
在某些情况下不是很明显,请看图片。
如果我想要明显“精确的半圆”端盖,该怎么办?
我很感激我可以建立自己的线条并填充它。
但是有没有办法以某种方式将“endcap”子类化?
如果你能说 = .myHappyEndcapMode
就太神奇了
我们如何解决这个问题?
你可以在这里看到它有多远:
更多信息:
在以下情况下会出现非圆形视觉外观:
- 细线
- 可能当宽度为非整数时
- 在手头的情况下,我的渐变和渐变无处不在,这可能无济于事
最佳答案
没有办法“子类化”帽形。如果您想要不同的帽子形状,则必须手动构建形状的轮廓。
圆帽已经和用 CGPath(ellipseIn:)
创建的圆完全一样了。
这是在半径稍大的圆顶部绘制的圆顶线,放大 8 倍:
线帽的曲率正好从圆的水平中心开始。
代码如下:
import UIKit
import PlaygroundSupport
let lineWidth: CGFloat = 20
let circleRadius = lineWidth / 2 + 1
let lineLayer = CAShapeLayer()
lineLayer.position = .init(x: 30, y: 30)
let path = CGMutablePath()
path.move(to: .zero)
path.addLine(to: .init(x: 30, y: 0))
lineLayer.path = path
lineLayer.lineWidth = lineWidth
lineLayer.lineCap = .round
lineLayer.strokeColor = UIColor.gray.cgColor
lineLayer.fillColor = nil
let circleLayer = CAShapeLayer()
circleLayer.position = lineLayer.position
circleLayer.path = CGPath(ellipseIn: CGRect.zero.insetBy(dx: -circleRadius, dy: -circleRadius), transform: nil)
circleLayer.fillColor = UIColor.blue.cgColor
let view = UIView(frame: .init(x: 0, y: 0, width: 100, height: 80))
view.layer.backgroundColor = UIColor.white.cgColor
view.layer.addSublayer(circleLayer)
view.layer.addSublayer(lineLayer)
PlaygroundPage.current.liveView = view
关于ios - 修改 endcap .round 样式,使其看起来更圆,与此处显示的示例图像不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58208946/