ios - 修改 endcap .round 样式,使其看起来更圆,与此处显示的示例图像不同

标签 ios uikit cashapelayer swift5

我刚刚了解到标准 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 就太神奇了

enter image description here

我们如何解决这个问题?

enter image description here

enter image description here

enter image description here

你可以在这里看到它有多远:

enter image description here

更多信息:

在以下情况下会出现非圆形视觉外观:

  • 细线
  • 可能当宽度为非整数时
  • 在手头的情况下,我的渐变和渐变无处不在,这可能无济于事

最佳答案

没有办法“子类化”帽形。如果您想要不同的帽子形状,则必须手动构建形状的轮廓。

圆帽已经和用 CGPath(ellipseIn:) 创建的圆完全一样了。

这是在半径稍大的圆顶部绘制的圆顶线,放大 8 倍:

zoomed image

线帽的曲率正好从圆的水平中心开始。

代码如下:

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/

相关文章:

ios - 在不关闭 UIDocument 的情况下重命名文档

swift - 如何在 60 秒内制作适合图形形状的动画?

ios - 如何屏蔽和添加阴影到 UIView

ios - 为什么我们在 ios 中使用枚举

ios - 将 AirPlay 配置为在应用程序进入后台时在苹果电视上为 AVPlayer 播放视频

swift - 在 swift playground 中使用 NSTimer

ios - UIStackView 调整 subview 的高度

ios - Swift 3,用特殊的lineCap画圆

ios - 发送到 Apple 设备的电子邮件有时链接已损坏

ios - 单击更改节点图像-SWIFT