ios - 用 2 条弧线修剪 UIView

标签 ios swift calayer uibezierpath

我有一个 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
  • 在“属性检查器” Pane 中,设置“圆弧偏移”和“填充颜色”
  • 将背景颜色设置为普通 View (您可能会使用透明)

结果:

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/

相关文章:

ios - 通过字符串设置 Objective-C 属性

swift - 在 for 循环完成快速处理 Firebase 快照之前,处理程序被提前调用,为什么?

swift - Startindex swift 3 问题

ios - 宽度的 CALayer 动画

objective-c - 在下一个运行循环 : What's Wrong With GCD? 上执行

ios - 无法使用 PFQuery Table ViewController (Swift 2) 显示自定义单元格

iphone - iOS:如何在登录后对用户进行身份验证(用于自动登录)?

ios - 从推送通知打开 View Controller 时如何管理 View 层次结构

ios - 从当前 CAAnimation 的状态开始新的 CAAnimation

iphone - 观察 CALayer 中的动画属性变化