1
我有以下类名为PushButtonView。它的子类UIButton
Subclassed 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,那么我最终会得到一个圆形按钮。
我在哪里出错代码来编程创建按钮?
你刚刚从某处复制和粘贴代码?看起来你正在使用它,但你不明白它。 – matt
我认为这是(至少部分)从这里复制:http://www.raywenderlich.com/90690/modern-core-graphics-with-swift-part-1 –
我得到了在线代码,并添加了一些修改它。边框和边框颜色。根据下面的答案,我只是搞砸了一行代码。感谢答案,Aaron Brager。 –