【问题标题】:Remove extra space on UIBezierPath删除 UIBezierPath 上的额外空间
【发布时间】:2018-04-25 09:29:11
【问题描述】:

我已使用以下代码将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,但它没有按需要设置。

【问题讨论】:

  • 你最后想要什么?不清楚。
  • arcView的大小有关系吗?
  • @Larme 我只想绘制没有多余空间的弧线。在UIView 上,我添加了UIBizerPath,但它考虑了UIView 的一半。
  • 简单地减少你的 arcView 框架 size.height 必须工作
  • 你是说CGPoint(x: arcView.frame.size.width/2, y: arcView.frame.size.height/2)?我还不清楚你想要什么。如果你的框架更大并且你画了半个圆圈,它会在某个时候有额外的空间。

标签: ios swift uiview uiviewanimation cashapelayer


【解决方案1】:

这是您的代码:-

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

输出:-

希望对你有帮助。!!

【讨论】:

  • 这会在下面创建空间:(
  • 你能告诉我你想要哪种类型的输出吗?
  • 查看问题cmets你会得到想法。
  • cmets 没有帮助。你能用手画出你的意思并张贴成图片吗?
  • @user1118321,让我试试。如果我得到结果,我将编辑我的答案。
【解决方案2】:

我对半径和中心进行了以下更改,结果似乎与您要寻找的一样接近(当然,根据框架的大小,您在下方有一些空间);它还考虑了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)

    }

【讨论】:

    猜你喜欢
    • 2013-01-09
    • 2020-10-16
    • 2013-08-21
    • 2016-11-23
    • 1970-01-01
    • 2013-02-15
    • 2018-02-18
    • 2021-12-08
    • 2014-08-25
    相关资源
    最近更新 更多