我想在由
创建的 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
要在 subview 中绘制箭头,我使用
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
因为我想在 subview 的中心绘制箭头, 我将起点和终点定义为
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”可用之前运行
编辑: 我想知道如何获取由代码创建的 subview 的边界。 我尝试使用“bounds”变量,但出现上述错误。
最佳答案
arrowPath.center = CGPoint(x:arrowView.frame.size.width/2, y: arrowView.frame.size.height/2)
arrowView.addSubview(arrowPath)
关于ios - 如何在 subview 的中心绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45380645/