如何在子视图的中心绘制

时间:2017-07-28 19:02:06

标签: ios swift3 bounds

我想在由

创建的SubView的中心绘制一个箭头
    {let arrowView = ArrowView()
    arrowView.frame = CGRect(x: selectButton.frame.minX, y: selectButton.frame.maxY, width: selectButton.frame.width, height: processButton.frame.minY - selectButton.frame.maxY)
    arrowView.backgroundColor = UIColor.white
    arrowView.viewWithTag(100)
    view.addSubview(arrowView)
    } //these codes are in the view controller 

要在子视图中绘制箭头,我使用

    let arrowPath = UIBezierPath.bezierPathWithArrowFromPoint(startPoint: startPoint, endPoint: endPoint, tailWidth: 4, headWidth: 8, headLength: 6)
// the error occurs here

override func draw(_ rect: CGRect) {
    let fillColor = UIColor.white
    fillColor.setFill()

    arrowPath.lineWidth = 1.0
    let strokeColor = UIColor.blue
    strokeColor.setStroke()

    arrowPath.stroke()
    arrowPath.fill()
 //these codes are in the subclass of UIView

因为我想在子视图的中心绘制箭头, 我将startPoint和endPoint定义为

    private var startPoint: CGPoint {
    return CGPoint(x: bounds.midX, y: bounds.minY)
}
private var endPoint: CGPoint {
    return CGPoint(x: bounds.midX, y: bounds.maxY)
} 
//try to find the point at the centre of Subview

但是,此代码无法编译,错误显示:

无法使用实例成员' startPoint'在属性初始化器内;属性初始化程序在“自我”之前运行可用

编辑: 我想知道如何获取代码创建的子视图的边界。 我尝试了#34;界限"变量但得到上述错误。

1 个答案:

答案 0 :(得分:1)

arrowPath.center = CGPoint(x:arrowView.frame.size.width/2, y: arrowView.frame.size.height/2)

arrowView.addSubview(arrowPath)