gpt4 book ai didi

swift - 如何切掉 UIButton 的角,而不是圆角?

转载 作者:行者123 更新时间:2023-11-30 10:44:02 27 4
gpt4 key购买 nike

我想快速切掉 UIButton 的边角。

我知道如何设置圆角半径,但我想剪掉圆角。

// what I DON'T want, rounded corners
myButton.layer.cornerRadius = myButton.frame.height / 2

切角适合我:

enter image description here

最佳答案

使用所需的路径创建一个 CAShapeLayer,如下所示

enter image description here

let button = UIButton()
button.backgroundColor = .red
button.frame = CGRect(x: 100, y: 100, width: 200, height: 40)
view.addSubview(button)

let layer = CAShapeLayer()
layer.fillColor = UIColor.yellow.cgColor

let cutSize:CGFloat = 20
let path = UIBezierPath()
path.move(to: CGPoint(x: 0, y: button.bounds.midY))
path.addLine(to: CGPoint(x: cutSize, y: 0))
path.addLine(to: CGPoint(x: button.bounds.maxX-cutSize, y: 0))
path.addLine(to: CGPoint(x: button.bounds.maxX, y: button.bounds.midY))
path.addLine(to: CGPoint(x: button.bounds.maxX-cutSize, y: button.bounds.maxY))
path.addLine(to: CGPoint(x: cutSize, y: button.bounds.maxY))
path.addLine(to: CGPoint(x: 0, y: button.bounds.midY))

layer.path = path.cgPath
button.layer.insertSublayer(layer, at: 0)

关于swift - 如何切掉 UIButton 的角,而不是圆角?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56184610/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com