【问题标题】:How to create a multiple path from several BezierPath如何从多个 BezierPath 创建多个路径
【发布时间】:2017-02-07 13:52:37
【问题描述】:

我正在使用 swift 2.2,我正在处理这样的问题: 我有一组 UIBezier 对象,我需要为它们创建一个笔触作为一条路径。

我曾经有一个特殊的功能,但这种方法的优点是它创建了几个层。 这符合我的要求,因为我需要一层

 func createStroke(line: UIBezierPath) {

        self.currentPath = CAShapeLayer()
        currentPath.path = line.CGPath
        currentPath.strokeColor = UIColor.blackColor().CGColor
        currentPath.fillColor = UIColor.clearColor().CGColor
        currentPath.lineWidth = 1
        self.view.layer.addSublayer(currentPath)

    }

从我的贝塞尔线数组创建多路径的最佳方法是什么?第一个想法是创建一个for-loop循环,但我认为这不是一种干净的方式。

【问题讨论】:

标签: arrays swift uibezierpath cashapelayer bezier


【解决方案1】:

你可以这样做(这是操场代码):

let myView = UIView(frame:CGRect(x: 100, y: 100, width: 250, height: 250))
myView.backgroundColor = UIColor.white
let myLayer = CAShapeLayer()

func createPath(i: Int) -> UIBezierPath {
    let path = UIBezierPath()
    path.move(to: CGPoint(x: 2*i, y: 9*i - 4))
    path.addLine(to: CGPoint(x: 10 + 5*i, y: 20 + 4*i))

    return path
}

func createStroke(line: UIBezierPath) {
    myLayer.path = line.cgPath
    myLayer.strokeColor = UIColor.black.cgColor
    myLayer.fillColor = UIColor.black.cgColor
    myLayer.lineWidth = 1
}

let paths = [createPath(i: 0), createPath(i: 15), createPath(i: 8), createPath(i: 10)]
let myPath = UIBezierPath()
for path in paths {
    myPath.append(path) // THIS IS THE IMPORTANT PART
}
createStroke(line: myPath)
myView.layer.addSublayer(myLayer)

myView

这是在操场上的样子:

这样你只会有一层

【讨论】:

  • 感谢您提供适用于游乐场的代码!
  • @KirillKudaev 不客气 :)
猜你喜欢
  • 2015-07-24
  • 2011-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多