下面是我附加的代码,我正在使用它在swift2xcode7中创建一个蓝色按钮
我想知道是否有办法将按钮创建为三角形和圆形。

    let btn = UIButton(type: UIButtonType.System) as UIButton
    btn.backgroundColor = UIColor.blueColor()
    btn.setTitle("CALL TPT AGENT", forState: UIControlState.Normal)
    btn.frame = CGRectMake(100, 100, 200, 100)
    btn.addTarget(self, action: "clickMe:", forControlEvents: UIControlEvents.TouchUpInside)
    self.view.addSubview(btn)

任何帮助都将不胜感激;)

最佳答案

画一个三角形,这里是代码

let trianglePath = UIBezierPath()
let triangleLayer = CAShapeLayer()
//change the CGPoint values to get the triangle of the shape you want
trianglePath.move(to: CGPoint(x: 0, y: 0))
trianglePath.addLine(to: CGPoint(x: 100, y: 50))
trianglePath.addLine(to: CGPoint(x: 0, y: 100))
trianglePath.addLine(to: CGPoint(x: 0, y: 0))

triangleLayer.path = trianglePath.cgPath
triangleLayer.fillColor = UIColor.black.cgColor

btn.layer.addSublayer(triangleLayer)

关于ios - 如何创建圆形和三角形按钮,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41546105/

10-16 01:30