Ios 如何在子视图的中心绘制
我想在由创建的子视图的中心绘制一个箭头Ios 如何在子视图的中心绘制,ios,swift3,bounds,Ios,Swift3,Bounds,我想在由创建的子视图的中心绘制一个箭头 {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.backg
{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
因为我想在子视图的中心绘制箭头,
我将起点和终点定义为
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”;属性初始值设定项在“self”可用之前运行
编辑:
我想知道如何获取由代码创建的子视图的边界。
我尝试了bounds变量,但得到了上面的错误。为什么要让我们猜测错误发生在哪里?您是否搜索了错误并找到了其他线索?谢谢您的帮助。然而,我仍然对你的答案感到困惑。我应该将代码添加到视图类还是视图控制器类?我使用贝塞尔路径函数在子视图上绘制。我想在子视图的中心绘制,但是我不能使用子视图的bounds属性,这会导致上面的错误。您好,您可以在视图控制器上使用它。请尝试一下,告诉我
arrowPath.center = CGPoint(x:arrowView.frame.size.width/2, y: arrowView.frame.size.height/2)
arrowView.addSubview(arrowPath)