Ios 如何创建圆形和三角形按钮
下面我附上了我目前用来在Swift 2,Xcode 7中创建蓝色按钮的代码 我想知道是否有办法将按钮创建为三角形和圆形Ios 如何创建圆形和三角形按钮,ios,swift,uibutton,swift2,xcode7,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) bt
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
}
}
为什么不创建三角形和圆形图像(使用图像文件或动态创建),然后相应地设置按钮图像:
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
}
}
要画一个三角形,下面是代码
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)
要画一个三角形,下面是代码
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)
对于圆形按钮,请在
viewDidLoad()
中:
对于三角形按钮,这可能会有所帮助:对于圆形按钮,请在您的
viewDidLoad()
中:
对于三角形按钮,这可能会有所帮助:我总是通过设置
btn.layer.cornerRadius=btn.frame.height/2来制作圆角按钮,这样无论按钮有多高,按钮末端都始终是圆角的。如果您想要一个圆,请将宽度和高度设置为相同的值。我总是通过设置btn.layer.cornerRadius=btn.frame.height/2来制作圆角按钮,这样无论按钮有多高,按钮末端都是圆角的。如果您想要一个圆,请将宽度和高度设置为相同的值。您知道bezierpath吗?对于圆,你们可以使用btn.layer.cornerRadius给出一些值(若你们的框架是正方形,那个么这个值是高度或宽度的一半),谢谢,我工作了。但是,在添加此项--“btn.layer.cornerRadius=50”并单击此项后,应用程序崩溃。你知道发生了什么事吗@Ramkumarchintalacan u将崩溃日志添加到此处我的崩溃日志一直被破坏,所以它只是说“未能连接”。。。你在代码中看到可能导致这种情况的东西了吗@拉姆库马金塔拉多你知道贝塞尔路吗?对于圆,你们可以使用btn.layer.cornerRadius给出一些值(若你们的框架是正方形,那个么这个值是高度或宽度的一半),谢谢,我工作了。但是,在添加此项--“btn.layer.cornerRadius=50”并单击此项后,应用程序崩溃。你知道发生了什么事吗@Ramkumarchintalacan u将崩溃日志添加到此处我的崩溃日志一直被破坏,所以它只是说“未能连接”。。。你在代码中看到可能导致这种情况的东西了吗@RamkumarchintalaTo添加到这个答案中,确保您的按钮约束设置为1:1,否则这可能不会给您一个圆圈。@Jerland2是的,对不起,我忘了提到这个!谢谢按钮约束是否自动设置为1:1?@Nosh由1:1约束我们的意思是按钮的高度和宽度几乎相等!我们不会说中文!要添加到这个答案中,请确保您的按钮约束设置为1:1,否则这可能不会给您一个圆圈。@Jerland2是的,对不起,我忘了提到这个!谢谢按钮约束是否自动设置为1:1?@Nosh由1:1约束我们的意思是按钮的高度和宽度几乎相等!我们不会说中文!谢谢,这将是最好的选择,但我需要能够识别和更改形状的颜色,并且在代码中字典中包含所有颜色会更容易。编辑我的答案-添加从UIButton继承的形状按钮,添加形状和颜色属性。更新了drawCustomImage以绘制三角形或圆形。谢谢,这将是最好的选择,但我需要能够识别和更改形状的颜色,并且在代码中字典中包含所有颜色会更容易。编辑了我的答案-添加了从UIButton继承的形状按钮,添加了形状和颜色属性。更新了drawCustomImage以绘制三角形或圆。此外,无论我如何努力,我似乎都无法使三角形成为等边和右侧向上。。。有什么帮助吗?:)@如果你的点之间的距离是x,你需要你的“中间线”的长度是√3/2*x这意味着你的点应该有这些坐标:(0,0)这里是坐标(0,0)(50,86.6)(100,0)和返回到(0,0)以使三角形点反过来:(0,86.6)(100,86.6)(50,86.6)和返回到(0,86.6)告诉我这些方向是否也适合你,不管我怎么努力,我似乎无法使三角形成为等边且右侧向上的三角形。。。有什么帮助吗?:)@如果你的点之间的距离是x,你需要你的“中间线”的长度是√3/2*x这意味着您的点应该有这些坐标:(0,0)这里是坐标(0,0)(50,86.6)(100,0)和返回到(0,0)以使三角形点反过来:(0,86.6)(100,86.6)(50,86.6)和返回到(0,86.6)告诉我这些方向是否适合您