Удалить лишнее пространство на UIBezierPath - PullRequest
0 голосов
/ 25 апреля 2018

Я добавил UIBezerPath к UIView с моим кодом ниже.

extension CGFloat {
    func toRadians() -> CGFloat {//0 360
        return self * CGFloat(Double.pi) / 180.0
    }
}

override func viewDidAppear(_ animated: Bool) {
    let trackLayer = CAShapeLayer()
    let circularPath = UIBezierPath(arcCenter: CGPoint(x: arcView.frame.size.width/2,
                                               y: arcView.frame.size.height),
                            radius: arcView.frame.size.height/2,
                            startAngle: CGFloat(180.0).toRadians(),
                            endAngle: CGFloat(0.0).toRadians(),
                            clockwise: true)

    trackLayer.path = circularPath.cgPath
    trackLayer.strokeColor = UIColor.red.cgColor
    trackLayer.lineWidth = 3
    trackLayer.fillColor = UIColor.clear.cgColor
    trackLayer.lineCap = kCALineCapButt

    arcView.layer.sublayers?.removeAll()
    arcView.layer.addSublayer(trackLayer)

}

Но проблема в том, что он дает дополнительное ненужное пространство сверху, которое мне не нужно.я попытался установить различное значение на circularPath, но это не так, как нужно.

enter image description here

Ответы [ 2 ]

0 голосов
/ 11 мая 2018

Я сделал следующее изменение по радиусу и центру, и результат кажется таким же близким к тому, что вы ищете (конечно, в зависимости от размера рамки, у вас есть немного места ниже); он также учитывает lineWidth, чтобы избежать рисования за рамкой.

    override func viewDidAppear(_ animated: Bool) {
        let trackLayer = CAShapeLayer()
        let lineWidth = CGFloat(3)
        let maxRadius = min(arcView.frame.size.height, arcView.frame.size.width/2) - lineWidth
        let circularPath = UIBezierPath(
            arcCenter: CGPoint(x: arcView.frame.size.width/2, y: maxRadius), 
            radius: maxRadius, 
            startAngle: CGFloat(180.0).toRadians(), 
            endAngle: CGFloat(0.0).toRadians(), 
            clockwise: true)

        trackLayer.path = circularPath.cgPath
        trackLayer.strokeColor = UIColor.red.cgColor
        trackLayer.lineWidth = lineWidth
        trackLayer.fillColor = UIColor.clear.cgColor
        trackLayer.lineCap = kCALineCapButt

        arcView.layer.sublayers?.removeAll()
        arcView.layer.addSublayer(trackLayer)

    }

enter image description here

0 голосов
/ 25 апреля 2018

Ваш код: -

let arcCenter = CGPoint(x: myview.frame.size.width / 2, y: myview.frame.size.height / 2)
let circleRadius = myview.frame.size.width / 2
let circlePath = UIBezierPath(arcCenter: arcCenter, radius: circleRadius, startAngle: CGFloat.pi, endAngle: CGFloat.pi * 2, clockwise: true)
let shapeLayer   = CAShapeLayer()
shapeLayer.path = circlePath.cgPath
shapeLayer.strokeColor = UIColor.red.cgColor
shapeLayer.fillColor = UIColor.blue.cgColor
shapeLayer.lineWidth = 5
myview.layer.addSublayer(shapeLayer)


// Make the view color transparent
   myview.backgroundColor = UIColor.clear

Вывод: -

enter image description here

Надеюсь, это поможет. !!

...