相关疑难解决方法(0)

在2017年使用IBDesignable绘制虚线(不是虚线!)线

用UIKit 绘制虚线很容易.所以:

CGFloat dashes[] = {4, 2};
[path setLineDash:dashes count:2 phase:0];
[path stroke];
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

有没有办法画出真正的虚线?

在此输入图像描述

有任何想法吗?


由于这个问题很老,没有人提出完整的@IBDesignable解决方案,这里是...

希望能节省一些打字的人.

@IBDesignable class DottedVertical: UIView {

    @IBInspectable var dotColor: UIColor = UIColor.etc
    @IBInspectable var lowerHalfOnly: Bool = false

    override func draw(_ rect: CGRect) {

        // say you want 8 dots, with perfect fenceposting:
        let totalCount = 8 + 8 - 1
        let fullHeight = bounds.size.height
        let width = bounds.size.width
        let itemLength = fullHeight / CGFloat(totalCount)

        let path = UIBezierPath()

        let …
Run Code Online (Sandbox Code Playgroud)

uikit uiview ios swift

84
推荐指数
4
解决办法
4万
查看次数

带虚线的圆圈 uiview

我正在尝试用虚线做一个圆圈。我能够在矩形中制作线条,但我不知道如何在圆形中制作这些线条。这是我得到的答案,但它在 Objective-C 中:UIView Draw Circle with Dotted Line Border

这是我的代码,它用虚线制作了一个矩形。

func addDashedBorder() {
    let color = UIColor.red.cgColor

    let shapeLayer:CAShapeLayer = CAShapeLayer()
    let frameSize = self.frame.size
    let shapeRect = CGRect(x: 0, y: 0, width: frameSize.width, height: frameSize.height)

    shapeLayer.bounds = shapeRect
    shapeLayer.position = CGPoint(x: frameSize.width/2, y: frameSize.height/2)
    shapeLayer.fillColor = UIColor.clear.cgColor
    shapeLayer.strokeColor = color
    shapeLayer.lineWidth = 2
    shapeLayer.lineJoin = CAShapeLayerLineJoin.round
    shapeLayer.lineDashPattern = [6,3]
    shapeLayer.path = UIBezierPath(roundedRect: shapeRect, cornerRadius: 5).cgPath

    self.layer.addSublayer(shapeLayer)
}
Run Code Online (Sandbox Code Playgroud)

uiview ios swift

5
推荐指数
1
解决办法
1940
查看次数

标签 统计

ios ×2

swift ×2

uiview ×2

uikit ×1