ios - 如何创建圆形和三角形按钮

标签 ios swift uibutton swift2 xcode7

下面我附上了我目前用来在 Swift 2、Xcode 7 中创建蓝色按钮的代码

我想知道是否有办法将按钮创建为三角形和圆形。

    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)

任何帮助将不胜感激;)

最佳答案

为什么不创建三角形和圆形图像(使用图像文件或动态)然后相应地设置按钮图像:

enum Shape {
    case Triangle, Circle
}

class ShapeButton : UIButton
{
    var shapeColor:UIColor!
    var buttonShape:Shape!
}

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    let button1  = UIButton(type:  .custom)
    button1.frame = CGRect(x:100, y:50, width:50, height:50)
    if let image = UIImage(named:"circle.png") {
        button1.setImage(image, for: .normal)
    }
    self.view.addSubview(button1)

    let button2  = ShapeButton(type:  .custom)
    button2.shapeColor = UIColor.red
    button2.buttonShape = Shape.Triangle
    button2.frame = CGRect(x:50, y:50, width:50, height:50)
    if let image = drawCustomImage(size:button2.frame.size,imageShape: button2.buttonShape,color:button2.shapeColor) {
        button2.setImage(image, for: .normal)
    }
    self.view.addSubview(button2)

    let button3  = ShapeButton(type:  .custom)
    button3.buttonShape = Shape.Circle
    button3.shapeColor = UIColor.green
    button3.frame = CGRect(x:150, y:50, width:50, height:50)
    if let image = drawCustomImage(size:button3.frame.size,imageShape: button3.buttonShape,color:button3.shapeColor) {
        button3.setImage(image, for: .normal)
    }
    self.view.addSubview(button3)

}

func drawCustomImage(size: CGSize,imageShape:Shape,color:UIColor) -> UIImage? {
    // Setup our context
    let bounds = CGRect(origin: CGPoint.zero, size: size)
    let opaque = false
    let scale: CGFloat = 0
    UIGraphicsBeginImageContextWithOptions(size, opaque, scale)
    guard let context = UIGraphicsGetCurrentContext() else { return nil }

    // Setup complete, do drawing here
    context.setStrokeColor(color.cgColor) //UIColor.red.cgColor
    context.setLineWidth(1)

    // Would draw a border around the rectangle
    // context.stroke(bounds)

    context.beginPath()
    if imageShape == Shape.Triangle {
        context.move(to: CGPoint(x: bounds.minX, y: bounds.maxY))
        context.addLine(to: CGPoint(x: bounds.maxX, y: bounds.maxY))
        context.addLine(to: CGPoint(x: bounds.maxX/2, y: bounds.minY))
        context.addLine(to: CGPoint(x: bounds.minX, y: bounds.maxY))
    } else {
        context.addEllipse(in: bounds)
    }
    context.closePath()

    context.setFillColor(color.cgColor)
    context.fillPath()

    // Drawing complete, retrieve the finished image and cleanup
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image
}

}

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

相关文章:

ios - 显示可达性状态的警报

ios - 在流上为 UIButton 设置动画

swift - 使用 rx Swift 过滤条件

ios - Segue延迟UIButton高亮显示

ios - UIButton 没有设置居中

android - WebView 应用程序和拒绝

ios - UIWebView 检测 mp3 文件加载

ios - Swift - 核心数据和 NSCoding : Failed to call designated initializer

ios - 从 Today Extension 打开特定的 ViewController

objective-c - 带有 UITextAlignmentLeft 的 iOS UIButton 仍然居中文本?