ios - UIBezierPath 绘制矩形以及所需的圆

标签 ios swift uiview uibezierpath

我正在尝试使用 UIBezierPath(rect:) 构造函数绘制圆,但与圆一起 - 矩形形状也被绘制为框架并且可见。这是我的代码:

class ProgressView: UIView {
let progressLayer = CustomShapeLayer()//declared below this class
override init(frame: CGRect) {
    super.init(frame: frame)
    self.isOpaque = false
}

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

}
override func draw(_ rect: CGRect) {
    let bounds = self.layer.bounds
    let centerX = bounds.midX
    let centerY = bounds.midY

    let upperCenterPoint = CGPoint(x: centerX, y: (centerY))
    let arcPathStartAngle: CGFloat = 2 * .pi
    let arcPathEndAngle: CGFloat = 0.0
    let radius: CGFloat = bounds.size.width / 3

    print(centerX, centerY, radius, "dim")

    let strokeWidth: CGFloat = 1//to show rect being formed
    let arcPath = UIBezierPath(rect: bounds.insetBy(dx: -strokeWidth, dy: -strokeWidth))

    arcPath.move(to: CGPoint(x: centerX + radius, y: centerY))
    arcPath.addArc(withCenter: upperCenterPoint, radius: radius, startAngle: arcPathStartAngle, endAngle: arcPathEndAngle, clockwise: false)
    arcPath.close()

    progressLayer.strokeColor = UIColor.white.cgColor
    progressLayer.path = arcPath.cgPath
    self.layer.addSublayer(progressLayer)

    let animateStrokeEnd = CABasicAnimation(keyPath: "strokeEnd")
    animateStrokeEnd.duration = 2.0
    animateStrokeEnd.fromValue = 0.0
    animateStrokeEnd.toValue = 1.0

    progressLayer.add(animateStrokeEnd, forKey: "animate stroke end animation")
}
}

//subclassing
class CustomShapeLayer: CAShapeLayer {
override init() {
    super.init()

    self.fillColor = UIColor.clear.cgColor
    self.lineWidth = CGFloat(5*Double.pi)
    self.lineCap = kCALineCapRound
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
}

我不需要将矩形绘制为框架,我是否只需要使用 UIBezierPath 空构造函数(因为它有目的),并且不能使用 UIBezierPath(rect:) 构造函数吗?

最佳答案

为这个 let arcPath = UIBezierPath() 替换 bezier 路径初始化 你的问题是你正在使用 UIBezierPath(rect: 初始化,它会创建一个矩形路径可以在这张图片中看到

enter image description here

完整绘制方法代码

override func draw(_ rect: CGRect) {
    let bounds = self.layer.bounds
    let centerX = bounds.midX
    let centerY = bounds.midY

    let upperCenterPoint = CGPoint(x: centerX, y: (centerY))
    let arcPathStartAngle: CGFloat = 2 * .pi
    let arcPathEndAngle: CGFloat = 0.0
    let radius: CGFloat = bounds.size.width / 3

    print(centerX, centerY, radius, "dim")

    let strokeWidth: CGFloat = 1//to show rect being formed
    let arcPath = UIBezierPath()
    arcPath.move(to: CGPoint(x: centerX + radius, y: centerY))
    arcPath.addArc(withCenter: upperCenterPoint, radius: radius, startAngle: arcPathStartAngle, endAngle: arcPathEndAngle, clockwise: false)
    arcPath.close()

    progressLayer.strokeColor = UIColor.white.cgColor
    progressLayer.path = arcPath.cgPath
    self.layer.addSublayer(progressLayer)

    let animateStrokeEnd = CABasicAnimation(keyPath: "strokeEnd")
    animateStrokeEnd.duration = 2.0
    animateStrokeEnd.fromValue = 0.0
    animateStrokeEnd.toValue = 1.0

    progressLayer.add(animateStrokeEnd, forKey: "animate stroke end animation")
}

关于ios - UIBezierPath 绘制矩形以及所需的圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48884015/

相关文章:

ios - 从 Swift 函数中的异步调用返回数据

ios - 在 UIView 中显示 UILabel 的属性文本

ios - Simperium iOS CoreData 关系随机为空

objective-c - Box2D 多边形未正确碰撞

ios - 在 ios 中设置在 ipad 设备中设计良好的约束的好方法或好做法是什么

ios - 如何确定哪个 Core Data 可转换属性未使用安全编码

html - 背景 :cover not scaling on mobile browsers

ios - 弹回上一个 ViewController 无法在警报错误消息上工作

ios - 我们可以为一个 View Controller 使用两个不同的 Xib 吗?

ios - 更改 UIView 的背景颜色