【问题标题】:With strokeColor, a line is drawn from the center to the circumference使用strokeColor,从中心到圆周画一条线
【发布时间】:2018-05-06 07:20:37
【问题描述】:



我正在使用 swift 4。
我写了下面的代码,并尝试用笔画画一个圆圈。
但是,使用下面的代码,我无法画出我想要的圆圈。
问题是线是从圆心到外围绘制的
(严格来说,是朝向'startAngle'属性的坐标点)。

我想擦掉线,怎么办?

(我准备了一张图片。)

class Something{

    var line:[CAShapeLayer] = []
    var path:[UIBezierPath] = []

    func drawingNow(){
            let layer = CAShapeLayer()
            self.layer.addSublayer(layer)
            self.line.append(layer)

            let addPath: UIBezierPath = UIBezierPath()
            addPath.move(to: CGPoint(x: 100, y: 100))
            addPath.addArc(
                withCenter: CGPoint(x: 100, y: 100),
                radius: CGFloat(50),
                startAngle: CGFloat(//someangle),
                endAngle: CGFloat(//someangle),
                clockwise: true
            )

            self.path.append(addPath)

            //self.line.last!.strokeColor = etc... (If don't use "override func draw()")
            self.line.last!.fillColor = UIColor.clear.cgColor
            self.line.last!.path = addPath.cgPath

            self.setNeedsDisplay()
    }

    override func draw(_ rect: CGRect) {
        if self.path.count != 0{
            UIColor.orange.setStroke()
            self.path.last!.stroke()
        }
    }
}

Image

【问题讨论】:

  • 尝试删除addPath.move(to: CGPoint(x: 100, y: 100))
  • 感谢您的回答!!我用你的回答解决了这个问题XD谢谢谢谢谢谢
  • @Sweeper 如果可以,请写下答案。所以我想检查一下。

标签: ios swift xcode uibezierpath cashapelayer


【解决方案1】:

调用addPath.move(to: CGPoint(x: 100, y: 100))后,UIBezierPath 移动到了指定坐标。现在你告诉它从那里添加一个圆弧 ,中心为 (100, 100)。要画一个圆心(100, 100)的圆弧,首先需要移动到圆周上。但此时它已经开始绘制了!这就是addArc 的工作原理。 addLine 也是如此。看docs

此方法添加从当前点开始的指定弧。

所以弧总是从当前点开始,即 (100, 100)。

与其告诉它先移动到中心,不如告诉它通过移除 move(to:)` 线来绘制弧线。

【讨论】:

  • 这是最好的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-20
  • 1970-01-01
  • 1970-01-01
  • 2022-11-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多