【问题标题】:How to give cornerRadius for UIBezierPath如何为 UIBezierPath 提供cornerRadius
【发布时间】:2016-01-28 05:54:54
【问题描述】:

我使用以下代码创建了一个矩形,现在我需要圆角这个矩形的角。但我找不到名为 layer.cornerRadius 的属性,谁能帮帮我?

class OvalLayer: CAShapeLayer {

    let animationDuration: CFTimeInterval = 0.3

    override init() {
        super.init()
        fillColor = Colors.green.CGColor
        path = ovalPathSmall.CGPath
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    var ovalPathStart: UIBezierPath {
        let path = UIBezierPath(ovalInRect: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 100.0))

        return path
    }
}

【问题讨论】:

  • 你可以在绘制时使用 UIBezierPath 本身来制作圆角半径
  • 由于您使用的是 UIBezierPath,因此您只需要使用 UIBezierPath 绘制圆角。签出this,它会为您提供所需的帮助。
  • @iphonic 你能把你建议的链接发给我吗?我将无法打开它。
  • @KhushbuDesai 好像搬家了,我找到了一个新链接here 它是...
  • @iphonic 你能帮我解决这个问题吗? stackoverflow.com/questions/50541219/…

标签: ios swift uibezierpath cashapelayer cornerradius


【解决方案1】:

您可以使用以下方法使所有角落都看到...

 UIBezierPath(roundedRect: anyView.bounds, cornerRadius: CGSize(width: 10.0, height: 10.0))

如果你想让特定的角落使用下面的方法。

 UIBezierPath(roundedRect: anyView.bounds,
        byRoundingCorners: .BottomLeft | .BottomRight,
        cornerRadius: CGSize(width: 10.0, height: 10.0))

【讨论】:

  • 能否为自定义形状(如星形)推荐圆角。
  • @LakshmiKeerthanaSiddu 你为明星做过圆角吗?我也想这样做。你能帮帮我吗!
  • 是的,我做到了,但没有使用 uibezierpath 。我得到了 CGPath,否则你可以使用绘画工具
【解决方案2】:

这样使用:

let pathWithRadius = UIBezierPath(roundedRect:yourView.bounds, byRoundingCorners:[.TopRight, .TopLeft], cornerRadii: CGSizeMake(5.0, 5.0))
let maskLayer = CAShapeLayer()
maskLayer.pathWithRadius = pathWithRadius.CGPath
yourView.layer.mask = maskLayer

对于所有角半径编辑此

[.TopRight,.TopLeft,.BottomRight, .BottomLeft]

【讨论】:

    【解决方案3】:

    使用这个初始化器

    let path1 = UIBezierPath(roundedRect: CGRect, cornerRadius: CGFloat)
    

    let path1 = UIBezierPath(roundedRect:  CGRect, byRoundingCorners: UIRectCorner, cornerRadii: CGSize))
    

    【讨论】:

      【解决方案4】:

      目标 c:

      UIBezierPath* path = [UIBezierPath
          bezierPathWithRoundedRect: CGRectMake(0, 0, 150, 153)
                       cornerRadius: 50];
      

      SWIFT:

      var path: UIBezierPath = UIBezierPath(roundedRect: CGRectMake(0, 0, 150, 153), cornerRadius: 50)
      

      希望这会有所帮助。

      【讨论】:

        【解决方案5】:

        就这么简单:

         UIBezierPath(roundedRect: anyView.bounds, cornerRadius: CGFloat(4.0))
        

        【讨论】:

          猜你喜欢
          • 2020-03-19
          • 1970-01-01
          • 1970-01-01
          • 2012-10-29
          • 1970-01-01
          • 2017-03-17
          • 2011-05-08
          • 2016-03-20
          • 2014-09-18
          相关资源
          最近更新 更多