【问题标题】:Applying a transformation to UIBezierPath makes drawing disappear对 UIBezierPath 应用转换使绘图消失
【发布时间】:2018-06-21 05:49:09
【问题描述】:

也许这是一个愚蠢的问题,但我正在尝试将转换应用于如此简单的路径:

let path = UIBezierPath(rect: CGRect(x: 10, y: 10, width: 20, height: 20))
path.apply(CGAffineTransform(rotationAngle: 2.0))
"colorLiteral".setFill()
path.fill()

似乎添加“应用”线的事实完全消除了视图上的形状,而如果我不包括该线,则形状会正确显示在其位置。

我是 Swift 的新手,所以我想我错过了重要的一步? 谢谢大家!

【问题讨论】:

    标签: swift core-graphics uibezierpath cgaffinetransform


    【解决方案1】:

    试试这个并记住rotationAngle 需要弧度

    let bounds = CGRect(x: 10, y: 10, width: 20, height: 20)
    let path = UIBezierPath(rect: bounds)
    path.apply(CGAffineTransform(translationX: -bounds.midX, y: -bounds.midY))
    path.apply(CGAffineTransform(rotationAngle: CGFloat.pi / 4))
    path.apply(CGAffineTransform(translationX: bounds.midX, y: bounds.midY))
    

    【讨论】:

    • 好吧,我的错误是不理解转换总是从原点发生,而不是路径本身。谢谢!!
    【解决方案2】:

    使用 CATransform3D

    shapeLayer.transform = CATransform3DMakeScale(1, -1, 1)
    

    变换路径,

    let shapeBounds = shapeLayer.bounds
    let mirror = CGAffineTransform(scaleX: 1,
                                      y: -1)
    let translate = CGAffineTransform(translationX: 0,
                                      y: shapeBounds.size.height)
    let concatenated = mirror.concatenating(translate)
    
    bezierPath.apply(concatenated)
    
    shapeLayer.path = bezierPath.cgPath
    

    变换层,

    let shapeFrame = CGRect(x: -120, y: 120, width: 350, height: 350)
    let mirrorUpsideDown = CGAffineTransform(scaleX: 1,
                                                  y: -1)
    shapeLayer.setAffineTransform(mirrorUpsideDown)
    shapeLayer.frame = shapeFrame
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-16
      相关资源
      最近更新 更多