【问题标题】:UIView with UIBezierPath with reversing pathUIView 与 UIBezierPath 与反转路径
【发布时间】:2018-03-15 17:42:39
【问题描述】:

我正在尝试使用UIBezierPathaddCurve() 方法在底部视图中提供一些掩码。

预期结果

电流输出

我尝试过的是:

        viewHeader.backgroundColor = headerColor
        let path = UIBezierPath()
        let cpoint1 = CGPoint(x: 80, y: 160)
        let cpoint2 = CGPoint(x: 280, y: viewHeader.Height + 50)
        path.move(to:.init(x: 0, y: viewHeader.Height + 10))
        path.addCurve(to: .init(x: viewHeader.Width, y: 205), controlPoint1: cpoint1, controlPoint2: cpoint2)
        let shapeLayer = CAShapeLayer()
        shapeLayer.path = path.reversing().cgPath
        shapeLayer.fillColor = headerColor?.cgColor
        shapeLayer.strokeColor =  UIColor.black.cgColor
        shapeLayer.lineWidth = 1.0
        viewHeader.layer.addSublayer(shapeLayer)

【问题讨论】:

  • 您好,您需要再添加 3 条线来完成方框,1.从曲线末端向下的右直线,2.从右到左边缘的底线,3.从左下角回到曲线起点位置。
  • @Ammaiappan:感谢您的快速回复。由于我是图形新手,请您提供一些代码,参考这个。
  • @iSwifter 真的吗?你不能先自己试试?
  • @AshleyMills:我试过了。但没有成功。
  • @AshleyMills:是的,你是对的,我很沮丧。首先对粗鲁的语言感到抱歉,请原谅我。并感谢您的 cmets/想法。下次我会注意的。谢谢。

标签: ios swift3 uiview uibezierpath cashapelayer


【解决方案1】:

我希望它对你有用,享受吧。

func drawLine() {
    let headerColor = UIColor.red
    let path = UIBezierPath()
    let cpoint1 = CGPoint(x: viewHeader.frame.width/4, y: 160)
    let cpoint2 = CGPoint(x: viewHeader.frame.width*3/4, y: viewHeader.frame.height + 50)
    path.move(to:.init(x: 0, y: 0))
    path.addLine(to:.init(x: 0, y: viewHeader.frame.height - 50))
    path.addCurve(to: .init(x: viewHeader.frame.width, y: viewHeader.frame.height - 50), controlPoint1: cpoint1, controlPoint2: cpoint2)
    path.addLine(to: CGPoint(x: viewHeader.frame.width, y: 0))
    path.addLine(to: CGPoint(x: 0, y: 0))
    path.close()
    let shapeLayer = CAShapeLayer()
    shapeLayer.path = path.reversing().cgPath
    shapeLayer.fillColor = headerColor.cgColor
    shapeLayer.strokeColor =  headerColor.cgColor
    shapeLayer.lineWidth = 1.0
    viewHeader.layer.addSublayer(shapeLayer)
}

【讨论】:

  • 谢谢,它正在工作。
  • 嗨,@Ammaiappan 有可能进入 Objective-C 吗?
  • @MihirOza:自己做吧。 Swift 到 Obj-C 没什么大不了的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-24
  • 2020-12-09
相关资源
最近更新 更多