如何切入UIButton的角落,而不是倒圆角?

时间:2019-05-17 10:32:32

标签: swift uibutton

我想迅速切掉UIButton的角落。

我知道如何设置拐角半径,但我想切角。

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

为我砍掉角落:

enter image description here

2 个答案:

答案 0 :(得分:0)

您可以通过以下方式实现:

sampleButton.layer.cornerRadius = 15
sampleButton.clipsToBounds = true

答案 1 :(得分:0)

使用所需路径创建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)