2015-06-25 82 views
1

所以我想在一個自定義的UIView創建一個簡單的座標軸上。下面的代碼:UI貝塞爾路徑改變線寬。

@IBDesignable 
class GraphingView: UIView { 


@IBInspectable 
var axesColor : UIColor = UIColor.redColor() { didSet{ setNeedsDisplay() } } 

@IBInspectable 
var lineWidth : CGFloat = 1.0 { didSet{ setNeedsDisplay() } } 

var origin : CGPoint{ 
    return convertPoint(center, fromView: superview) 
} 


override func drawRect(rect: CGRect) { 

    let axesPath = UIBezierPath() 
    axesPath.moveToPoint(CGPoint(x: 0, y: origin.y)) 
    axesPath.addLineToPoint(CGPoint(x: bounds.width, y: origin.y)) 
    axesPath.moveToPoint(CGPoint(x: origin.x, y: 0)) 
    axesPath.addLineToPoint(CGPoint(x: origin.x, y: bounds.height)) 
    axesPath.lineWidth = lineWidth 
    axesColor.set() 
    axesPath.stroke() 

} 


} 

在肖像,這原來罰款:

Portrait

但在景觀,垂直線是一點點厚:

Landscape

任何人都可以幫我解釋爲什麼會發生這種情況,以及如何解決它?謝謝!

回答

1

我最終想出了它。現在,自定義視圖的模式被設置爲「Scale to Fill」。我只是不得不改變它「重畫」,所以它會再次運行drawRect。

+0

我沒有想到爲此找到一個解決方案,但你的問題和答案救了我一把ha。謝謝 – Eyzuky