ios - 如何在 subview 的中心绘制

标签 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 

要在 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/

相关文章:

ios - 出于安全原因,如何在iOS中从内存中删除字符串?甚至需要它还是iOS自己处理

ios - xCode 8 中裁剪的 iPad 模拟器

swift - 将 CGPathAddArcToPoint 转换为 Swift 3

工具栏中条形按钮项底部的 Swift 条

javascript - Node/Javascript 中的 RegEx - 如何获取模式匹配边界?

ios - 应用 CGAffineTransformRotate 后更新框架的原点

iOS 5 以编程方式读取文本并具有辅助功能

objective-c - 在 iOS 程序中比较 char 数组和字符串

ios - 将 nib 文件添加到 ScrollView 时如何删除底部空白

java - 如何使边界(碰撞矩形)小于 LibGDX 中的 Sprite