Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIBezierPath转换给出了错误的答案_Ios_Swift_Uibezierpath_Cgpath - Fatal编程技术网

Ios UIBezierPath转换给出了错误的答案

Ios UIBezierPath转换给出了错误的答案,ios,swift,uibezierpath,cgpath,Ios,Swift,Uibezierpath,Cgpath,当我尝试转换路径,将其移动到原点{0,0}时,生成的路径边界出错。(或者,我的假设是错误的) e、 g.路径提供以下边界信息: let bezier = UIBezierPath(cgPath: svgPath) print(bezier.bounds) // (0.0085, 0.7200, 68.5542, 41.1379) print(bezier.cgPath.boundingBoxOfPath) // (0.0085, 0.7200, 68.5542, 41.1379) pri

当我尝试转换路径,将其移动到原点{0,0}时,生成的路径边界出错。(或者,我的假设是错误的)

e、 g.路径提供以下边界信息:

let bezier = UIBezierPath(cgPath: svgPath)
print(bezier.bounds)

// (0.0085, 0.7200, 68.5542, 41.1379)

print(bezier.cgPath.boundingBoxOfPath)

// (0.0085, 0.7200, 68.5542, 41.1379)

print(bezier.cgPath.boundingBox)

// (-1.25, -0.1070, 70.0360, 41.9650)
I(尝试)将路径移动到原点:

let origin = bezier.bounds.origin
bezier.apply(CGAffineTransform(translationX: -origin.x, y: -origin.y))

print(bezier.bounds)

// (0.0, -2.7755, 68.5542, 41.1379)
如您所见,
x原点
组件在0处是正确的。但是,
y组件
(-2.7755)已经完全消失了。它应该是0,不是吗

当我对
cgPath
属性执行转换时,也会发生同样的情况

有人知道什么样的情况会导致
UIBezierPath/CGPath
在翻译时出现这种行为吗?在阅读苹果文档后,似乎
UIBezierPath/CGPath
没有保存转换状态;调用变换时,将立即变换点

谢谢你的帮助

背景:

路径数据来自
Font-awome
SVG,通过
PocketSVG
。所有的文件都会被解析,大多数都画得很好。但有一小部分表现出上述翻译问题。我想知道我是否做了一些根本错误的或愚蠢的事情,然后再通过SVG解析、路径构建代码查找缺陷

顺便说一句,我不是在这个阶段绘画或处理上下文;在绘制之前,我正在创建路径

[编辑]

为了检查PocketSVG是否为我提供了格式正确的数据,我将相同的SVG传递给SwiftSVG,并获得了与PocketSVG相同的路径数据,结果相同:

let svgURL = Bundle.main.url(forResource: "fa-mars-stroke-h", withExtension: "svg")!
var bezier = UIBezierPath.pathWithSVGURL(svgURL)!
print(bezier.bounds)

// (0.0085, 0.7200, 68.5542, 41.1379)

let origin = bezier.bounds.origin
let translation = CGAffineTransform(translationX: -origin.x, y: -origin.y)
bezier.apply(translation)
print(bezier.bounds)

// (0.0, -2.7755, 68.5542, 41.1379)
同样,y分量应该是0,但不是。很奇怪

一时兴起,我想我应该尝试再次应用一个转换。而且,它成功了

let translation2 = CGAffineTransform(translationX: -bezier.bounds.origin.x, y: -bezier.bounds.origin.y)
bezier.apply(translation2)
print(bezier.bounds)

// (0.0, 0.0, 68.5542491336633, 41.1379438254997)

莫名其妙!我在这里忽略了一些真正基本的东西吗?

我也尝试过与您相同的方法,现在为我在Xcode 8.3.2/iOS 10中工作


我也遇到了同样的问题,我通过使用下面的代码片段(Swift 5)成功地解决了这个问题。我在有机贝塞尔形状上进行了测试,它的工作原理与预期相符:

extension CGRect {
    var center: CGPoint { return CGPoint(x: midX, y: midY) }
}

extension UIBezierPath {
    func center(inRect rect:CGRect) {
        let rectCenter = rect.center
        let bezierCenter = self.bounds.center
        
        let translation = CGAffineTransform(translationX: rectCenter.x - bezierCenter.x, y: rectCenter.y - bezierCenter.y)
        self.apply(translation)
    }
}
用法示例:

override func viewDidLoad() {
    super.viewDidLoad()

    let bezier = UIBezierPath() // replace this with your bezier object

    let shape = CAShapeLayer()
    shape.strokeColor = UIColor.black.cgColor
    shape.fillColor = UIColor.clear.cgColor
    shape.bounds = self.view.bounds
    shape.position = self.view.bounds.center

    bezier.center(inRect: shape.bounds)
    shape.path = bezier.cgPath
    self.view.layer.addSublayer(shape)
}

它将在屏幕中央显示形状。

感谢您的反馈。如果我用rect创建一个路径,它也对我有效。因此,路径数据本身似乎在某种程度上被屏蔽,并导致UIBezierPath/CGPath产生奇怪的结果。如果是这样的话,那么被屏蔽的数据会以这种方式破坏边界,这是很奇怪的。干杯