Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用宽度和高度过大的自定义类创建圆形指示器_Ios_Swift - Fatal编程技术网

Ios 使用宽度和高度过大的自定义类创建圆形指示器

Ios 使用宽度和高度过大的自定义类创建圆形指示器,ios,swift,Ios,Swift,我想使用CAShapeLayer制作一个圆形指示器。我在一个自定义类中执行此操作,以便可以在每个UIView中重用它 这就是我现在所拥有的: class CircularIndicator: UIView { var circularIndicator = CAShapeLayer() override init(frame: CGRect) { super.init(frame: frame) configure() }

我想使用
CAShapeLayer
制作一个圆形指示器。我在一个自定义类中执行此操作,以便可以在每个
UIView
中重用它

这就是我现在所拥有的:

class CircularIndicator: UIView {

    var circularIndicator = CAShapeLayer()

    override init(frame: CGRect) {
        super.init(frame: frame)

        configure()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        configure()
    }

    func configure() {
        let centerPoint = CGPoint(x: self.bounds.width / 2, y: self.bounds.width / 2)
        let circleRadius = self.bounds.size.width / 2
        let circlePath = UIBezierPath(arcCenter: centerPoint, radius: circleRadius, startAngle: CGFloat(-0.5 * M_PI), endAngle: CGFloat(1.5 * M_PI), clockwise: true)

        circularIndicator.path = circlePath.cgPath
        circularIndicator.strokeColor = UIColor.blue.cgColor
        circularIndicator.fillColor = UIColor.clear.cgColor
        circularIndicator.lineWidth = 6
        circularIndicator.strokeStart = 0
        circularIndicator.strokeEnd = 1

        self.layer.addSublayer(circularIndicator)
    }
}
我在主视图控制器中添加了一个高度和宽度为150的
UIView
。我的问题是,当我运行这个应用程序时,
circleRadius
的值最终是500,这使得圆圈变得更大

我的理解是
self.bounds.witdh
self.bounds.height
应该等于包含此类的
UIView
的值。
我错了吗?为什么我的宽度和高度的值会这么大?

问题是
帧可能会改变。因此,您应该在
布局子视图中设置
路径

class CircularIndicator: UIView {

    var circularIndicator = CAShapeLayer()
    var lineWidth: CGFloat = 6

    override init(frame: CGRect) {
        super.init(frame: frame)

        configure()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        configure()
    }

    private func configure() {
        circularIndicator.strokeColor = UIColor.blue.cgColor
        circularIndicator.fillColor = UIColor.clear.cgColor
        circularIndicator.lineWidth = lineWidth
        circularIndicator.strokeStart = 0
        circularIndicator.strokeEnd = 1

        layer.addSublayer(circularIndicator)
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        let centerPoint = CGPoint(x: bounds.width / 2, y: bounds.width / 2)
        let circleRadius = (bounds.size.width - lineWidth) / 2

        circularIndicator.path = UIBezierPath(arcCenter: centerPoint, radius: circleRadius, startAngle: CGFloat(-0.5 * M_PI), endAngle: CGFloat(1.5 * M_PI), clockwise: true).cgPath
    }
}

顺便说一句,您可能需要将圆插入
线宽的一半,如上图所示,我会相应地调整
圆环
。否则,圆的边将超出视图的
框架。

好的,显然我必须在布局子视图中进行半径计算。然后我会得到正确的值。是的。你要么接受Rob的答案,要么发布你自己的答案并接受,因此问题被标记为已回答。