快速光滑的圆角

Wal*_*ter 9 rounded-corners ios swift

如何创建一个光滑圆角的UIButton?标准的苹果圆角比标准的圆角更好.如果我在草图中设计一个按钮,我可以切换"平滑的角落",从Apple圆角(平滑)到标准圆角切换形状的角.

这是一个比较:

在此输入图像描述

我怎样才能在swift中切换它?

showContentButton.layer.cornerRadius = 20然而,我不知道这个圆角是"光滑的角落"还是"标准的圆角".我不知道如何切换这个.有任何想法吗?

Vin*_*ich 25

iOS 13.0开始,除了设置 cornerRadius 之外,您还可以使用此属性:

sampleButton.layer.cornerCurve = .continuous
Run Code Online (Sandbox Code Playgroud)

这也很容易设置动画,这在之前会导致使用 UIBezierPath 出现问题。

https://developer.apple.com/documentation/quartzcore/calayer/3152596-cornercurve查看更多


ino*_*key 17

为了达到这个效果,你可以使用 UIBezierPath

光滑的角落

编辑: UIView中的用法

class MyView: UIView {

    let myButton UIButton = UIButton()

    override func layoutSubviews() {
        super.layoutSubviews()

        // your roundPath code here
    }

}
Run Code Online (Sandbox Code Playgroud)

编辑2:

使用此方法可以舍入特定角点:

let roundPath = UIBezierPath(
    roundedRect: bounds,
    byRoundingCorners: [.topLeft, .topRight],
    cornerRadii: CGSize(width: 10, height: 10)
)
Run Code Online (Sandbox Code Playgroud)

资源

  • @WalterBeiter 谢谢!我添加了备注,表明我的解决方案适用于 iOS 13 之前的版本。 (3认同)