我有以下名为PushButtonView的类。它是UIButton
import UIKit
@IBDesignable
class PushButtonView: UIButton {
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
@IBInspectable var fillColor: UIColor = UIColor.greenColor()
@IBInspectable var isAddButton: Bool = true
@IBInspectable var borderColor: UIColor = UIColor.blackColor()
@IBInspectable var useBorder: Bool = false
@IBInspectable var borderWidth: CGFloat = 1.0
override func drawRect(rect: CGRect) {
var path = UIBezierPath(ovalInRect: rect)
fillColor.setFill()
//Path's don't draw anything. To draw the path, fill it.
path.fill()
//Set up width and height variables for the plus
let plusHeight: CGFloat = 3.0
let plusWidth: CGFloat = min(bounds.width, bounds.height) * 0.6
//Create the path
var plusPath = UIBezierPath()
//Set the path's line width to the height of the stroke
plusPath.lineWidth = plusHeight
//Move the initial point of the path to the start of the horizontal stroke.
//Point is the middle of the button - half of the width of the plus.
plusPath.moveToPoint(CGPoint(x: bounds.width/2 - plusWidth/2 + 0.5, y: bounds.height/2 + 0.5))
//Add a point to the path at the end of the stroke.
//Final point is the middle of the button + half of the width of the plus.
plusPath.addLineToPoint(CGPoint(x: bounds.width/2 + plusWidth/2 + 0.5, y: bounds.height/2 + 0.5))
if isAddButton {
//Move the initial point of the path to the start of the vertical stroke.
//Point is the middle of the button. Start point is half of the height - half of the width of the plus.
plusPath.moveToPoint(CGPoint(x: bounds.width/2 + 0.5, y: bounds.height/2 - plusWidth/2 + 0.5))
//Add a point to the path at the end of the stroke.
//Final point is the middle of the button. Start pointis half of the heigh + half of the width of the plus
plusPath.addLineToPoint(CGPoint(x: bounds.width/2 + 0.5, y: bounds.height/2 + plusWidth/2 + 0.5))
}
//Set the stroke color
UIColor.whiteColor().setStroke()
//Stroke the path
plusPath.stroke()
if useBorder {
//Extra code here. Paints a black border around the button. In order for it to be round, the
//button width and height must be the same. The cornerRadius must be half of the width (or height).
super.layer.borderColor = borderColor.CGColor
super.layer.borderWidth = borderWidth
super.layer.cornerRadius = super.frame.width / 2 + 0.5
}
}
}
最初,我使用它在视图上创建按钮。我会在视图上放一个UIBUtton
,然后将类设置为PushButtonView。只要按钮的宽度和高度相同,按钮就会呈圆形。
现在我试图通过在代码中创建按钮来使用此类。当我这样做时,我最终得到一个方形按钮,而不是一个圆形按钮。这是创建按钮的代码。
button1 = PushButtonView(frame: CGRectMake(0, 0, 50, 50))
button1.frame = CGRectMake(0, 0, 50, 50)
button1.center = CGPointMake(self.view.frame.width/2, label.center.y - (label.frame.height/2) - 50)
button1.fillColor = UIColor.greenColor()
button1.addTarget(self, action: "button1Clicked", forControlEvents: .TouchUpInside)
self.view.addSubview(button1)
当我运行它时,按钮不是圆圈;这是一个广场。
如果我直接在视图上放置UIButton并将类设置为PushButtonView,那么我最终会得到一个圆形按钮。
我在哪里编写代码以创建按钮?
答案 0 :(得分:2)
我认为你必须设置useBorder
:
button1.useBorder = true
否则图层的cornerRadius
未设置。
如果这是正确的,将来通过在drawRect
中设置断点并逐步完成实施,可能更容易调试。