Swift 如何在Xcode中创建这样的倾斜按钮?

Swift 如何在Xcode中创建这样的倾斜按钮?,swift,xcode,Swift,Xcode,我希望结果与给定的图像一样精确 如何创建下图所示的倾斜按钮 我不想使用图像作为背景。 我将把颜色留给你,但以下是我所做的: @IBOutlet var callButton: UIButton! @IBOutlet var bookButton: UIButton! func createSlantOnButton() { let buttonSlant = 10 as CGFloat //Change this to change the slant angle let buttonHe

我希望结果与给定的图像一样精确

如何创建下图所示的倾斜按钮

我不想使用图像作为背景。

我将把颜色留给你,但以下是我所做的:

@IBOutlet var callButton: UIButton!
@IBOutlet var bookButton: UIButton!

func createSlantOnButton() {

let buttonSlant = 10 as CGFloat //Change this to change the slant angle
let buttonHeight = bookButton.bounds.height
let buttonWidth = bookButton.bounds.width
let customButton = bookbutton

customButton!.backgroundColor = UIColor.green
callButton.backgroundColor = UIColor.green

let drawView = UIBezierPath()

drawView.move(to: CGPoint(x: 100.0, y: 0.0))

drawView.addLine(to: CGPoint(x: buttonWidth, y: 0.00))
drawView.addLine(to: CGPoint(x: buttonWidth, y: buttonHeight))
drawView.addLine(to: CGPoint(x: 00.0, y: buttonHeight))
drawView.addLine(to: CGPoint(x: buttonSlant, y: 00.0))
drawView.close()

let layer = CAShapeLayer()
layer.fillColor = UIColor.blue.cgColor
layer.strokeColor = UIColor.blue.cgColor
layer.path = drawView.cgPath

customButton!.layer.addSublayer(layer)

self.view.addSubview(customButton!)
}

override func viewDidLoad() {
    super.viewDidLoad()
    createSlantOnButton()

}

复制并粘贴,挂上你的按钮。让我知道你是怎么走的

看看这里还有一个Swift 3/4的答案它对我没有帮助..因为我只想让波顿的一侧倾斜并改变角度