2015-07-13 60 views
1

我有以下類名爲PushButtonView。它的子類UIButtonSubclassed 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

你剛剛從某處複製和粘貼代碼?看起來你正在使用它,但你不明白它。 – matt

+0

我認爲這是(至少部分)從這裏複製:http://www.raywenderlich.com/90690/modern-core-graphics-with-swift-part-1 –

+0

我得到了在線代碼,並添加了一些修改它。邊框和邊框顏色。根據下面的答案,我只是搞砸了一行代碼。感謝答案,Aaron Brager。 –

回答

2

我認爲你必須設置useBorder

button1.useBorder = true 

否則層的cornerRadius不被設置。

如果這是正確的,將來,通過在drawRect中設置斷點並逐步執行實現,可能更容易進行調試。