【问题标题】:Why the max value of cornerRadius is not half of the rectangle? Drawing roundedRect with UIBezierPath in swift为什么cornerRadius的最大值不是矩形的一半?用 UIBezierPath 快速绘制roundedRect
【发布时间】:2020-10-26 07:52:26
【问题描述】:

正如文件所说,cornerRadius 不能大于一半宽度,但在我的代码中,圆形矩形只有宽度的 1/3 变成了圆形,不知道为什么?需要帮助

文档:https://developer.apple.com/documentation/uikit/uibezierpath/1624356-init

角半径: 每个角椭圆的半径。值为 0 会生成一个没有圆角的矩形。大于矩形宽度或高度一半的值被适当地限制为宽度或高度的一半。

var path = UIBezierPath(roundedRect: CGRect(x: 50, y: 200, width: 94, height: 94), cornerRadius: 31)
path.stroke()

path = UIBezierPath(roundedRect: CGRect(x: 50, y: 300, width: 94, height: 94), cornerRadius: 30.5)
path.stroke()
                
path = UIBezierPath(roundedRect: CGRect(x: 50, y: 400, width: 94, height: 94), cornerRadius: 31.5)
path.stroke()

【问题讨论】:

    标签: swift core-graphics quartz-2d


    【解决方案1】:

    我认为这是因为类型转换错误。

    试试这个。

    let xCor: CGFloat = 50.0
    let width: CGFloat = 94.0
    let height: CGFloat = 94.0
    
    let cornerRadius: CGFloat = width / 3.0
    
    var path = UIBezierPath(roundedRect: CGRect(x: xCor, y: 200.0, width: width, height: height), cornerRadius: cornerRadius)
    path.stroke()
    
    path = UIBezierPath(roundedRect: CGRect(x: xCor, y: 300.0, width: width, height: height), cornerRadius: cornerRadius)
    path.stroke()
                    
    path = UIBezierPath(roundedRect: CGRect(x: xCor, y: 400.0, width: width, height: height), cornerRadius: cornerRadius)
    path.stroke()
    

    PS:没有通过运行此代码进行检查,如果这里有任何问题,请告诉我。

    【讨论】:

      猜你喜欢
      • 2016-08-18
      • 1970-01-01
      • 1970-01-01
      • 2015-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多