2015-06-25 80 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