gpt4 book ai didi

ios - 用 2 个弧线修剪 UIView

转载 作者:行者123 更新时间:2023-12-01 16:06:43 24 4
gpt4 key购买 nike

我有一个 UIView我想用两个圆圈修剪它,就像我有 drawn (对不起质量)。

我的代码:

final class TrimmedView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)

let size = CGSize(width: 70, height: 70)
let innerRadius: CGFloat = 366.53658283002471
let innerBottomRadius: CGFloat = 297.88543112651564

let path = UIBezierPath()
path.move(to: CGPoint(x: -innerRadius + (size.width / 2), y: innerRadius))
path.addArc(withCenter: CGPoint(x: size.width / 2, y: innerRadius), radius: innerRadius, startAngle: CGFloat.pi, endAngle: 0, clockwise: true)
path.move(to: CGPoint(x: -innerBottomRadius + (size.width / 2), y: innerBottomRadius))
path.addArc(withCenter: CGPoint(x: size.width / 2, y: innerBottomRadius), radius: innerBottomRadius, startAngle: 0, endAngle: CGFloat.pi, clockwise: true)

path.close()
let shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
shapeLayer.shadowPath = path.cgPath
layer.mask = shapeLayer
}

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

View Controller :
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

let view = UIView(frame: CGRect(origin: CGPoint(x: (self.view.bounds.width - 70) / 2, y: (self.view.bounds.height - 70) / 2), size: CGSize(width: 70, height: 70)))
view.backgroundColor = .red
self.view.addSubview(view)
let view1 = TrimmedView(frame: view.frame)
view1.backgroundColor = .yellow
self.view.addSubview(view1)
}

我得到了这个 result .对我来说,顶部修剪似乎有效,但底部没有,我不知道为什么。任何帮助,将不胜感激。谢谢。

最佳答案

这是一个自定义 View ,应该可以为您提供所需的内容。
UIBezierPath对顶部“凸”弧和底部“凹”弧使用 QuadCurves。

它被标记为 @IBDesignable所以你可以在设计时在 IB/Storyboard 中看到它。弧的“高度”和填充颜色分别设置为 @IBInspectable因此您也可以在设计时调整这些值。

要在 Storyboard 中使用它:

  • 添加普通UIView
  • 将类更改为 BohdanShapeView
  • 在 Attributes Inspector Pane 中,设置 Arc Offset 和 Fill Color
  • 像普通 View 一样设置背景颜色(您可能会使用 clear)

  • 结果:

    enter image description here

    enter image description here

    通过代码使用它:
    let view1 = BohdanShapeView(frame: view.frame)
    view1.fillColor = .systemTeal
    view1.arcOffset = 10
    self.view.addSubview(view1)

    这是类(class):
    @IBDesignable
    class BohdanShapeView: UIView {

    @IBInspectable var arcOffset: CGFloat = 0.0
    @IBInspectable var fillColor: UIColor = UIColor.white

    let shapeLayer = CAShapeLayer()

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

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

    func commonInit() -> Void {
    // add the shape layer
    layer.addSublayer(shapeLayer)
    }

    override func layoutSubviews() {
    super.layoutSubviews()

    // fill color for the shape
    shapeLayer.fillColor = self.fillColor.cgColor

    let width = bounds.size.width
    let height = bounds.size.height

    let bezierPath = UIBezierPath()

    // start at arcOffset below top-left
    bezierPath.move(to: CGPoint(x: 0.0, y: 0.0 + arcOffset))

    // add curve to arcOffset below top-right
    bezierPath.addQuadCurve(to: CGPoint(x: width, y: 0.0 + arcOffset), controlPoint: CGPoint(x: width * 0.5, y: 0.0 - arcOffset))

    // add line to bottom-right
    bezierPath.addLine(to: CGPoint(x: width, y: height))

    // add curve to bottom-left
    bezierPath.addQuadCurve(to: CGPoint(x: 0.0, y: height), controlPoint: CGPoint(x: width * 0.5, y: height - arcOffset * 2.0))

    // close the path
    bezierPath.close()

    shapeLayer.path = bezierPath.cgPath

    }

    }

    关于ios - 用 2 个弧线修剪 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58520058/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com