【问题标题】:Trim UIView with 2 arcs用 2 个弧线修剪 UIView
【发布时间】:2020-02-19 12:51:44
【问题描述】:

我有一个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)
    }
}

视图控制器:

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。对我来说,顶部修剪似乎有效,但底部没有,我不知道为什么。任何帮助,将不胜感激。谢谢。

【问题讨论】:

  • 在stackoverflow而不是第三方网站中添加图片

标签: ios swift calayer uibezierpath


【解决方案1】:

这是一个自定义视图,应该可以满足您的需求。

UIBezierPath 将 QuadCurves 用于顶部“凸”弧和底部“凹”弧。

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

在情节提要中使用它:

  • 添加一个普通的UIView
  • 将类更改为BohdanShapeView
  • 在“属性检查器”窗格中,设置弧偏移和填充颜色
  • 将背景颜色设置为普通视图(您可能会使用 clear)

结果:

通过代码使用:

let view1 = BohdanShapeView(frame: view.frame)
view1.fillColor = .systemTeal
view1.arcOffset = 10
self.view.addSubview(view1)

这是课程:

@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

    }

}

【讨论】:

  • 非常感谢,@DonMag。我认为 BohdanShapeView 是完美的名字:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-03
  • 1970-01-01
相关资源
最近更新 更多