缩放CGPath& Class.Type没有名为nameHere的成员

时间:2015-04-21 09:53:49

标签: ios iphone xcode swift

我正在创建UIButton(从CGPath中提取),其初始化为CGRect帧,但不会缩放到其视图的大小。

相反,我正在尝试扩展路径,但Xcode似乎对我正在做的事情不太满意。

class MenuButton : UIButton {
    let shortStrokeOrig: CGPath = {
        let path = CGPathCreateMutable()
        CGPathMoveToPoint(path, nil, 2, 2)
        CGPathAddLineToPoint(path, nil, 28, 2)

        return path
    }()

    let outlineOrig: CGPath = {
        let path = CGPathCreateMutable()
        CGPathMoveToPoint(path, nil, 10, 27)
        // Blah blah more curves....
        CGPathAddCurveToPoint(path, nil, 13.16,  2.00,  2.00, 13.16,  2, 27)

        return path
    }()

    let shortStroke: CGPath = CGPathCreateCopyByTransformingPath(shortStrokeOrig, CGAffineTransformMakeScale(0.5, 0.5))
    let outline: CGPath = CGPathCreateCopyByTransformingPath(outlineOrig, CGAffineTransformMakeScale(0.5, 0.5))
}

这会导致错误MenuButton.Type does not have a member named shortStrokeOrigoutlineOrig相同。

不确定为什么抱怨。或者,如果有人对如何扩展我的路径有任何更好的建议..

1 个答案:

答案 0 :(得分:1)

问题出在shortStroke常量初始化 - CGPathCreateCopyByTransformingPath(shortStrokeOrig, CGAffineTransformMakeScale(0.5, 0.5))

问题

  • shortStrokeOrig - 被声明为实例变量,但您将其用作类变量。
  • CGPathCreateCopyByTransformingPath函数的第二个参数类型为UnsafePointer<CGAffineTransform>而不是CGAffineTransform

解决方案

  • shortStrokeOrigoutlineOrig设为类变量
  • 传递CGAffineTransform作为参考(创建变量并在将其传递给函数时使用&amp;)

代码:

class MenuButton : UIButton {
    static let shortStrokeOrig: CGPath = {
        let path = CGPathCreateMutable()
        CGPathMoveToPoint(path, nil, 2, 2)
        CGPathAddLineToPoint(path, nil, 28, 2)

        return path
        }()

    static let outlineOrig: CGPath = {
        let path = CGPathCreateMutable()
        CGPathMoveToPoint(path, nil, 10, 27)
        // Blah blah more curves....
        CGPathAddCurveToPoint(path, nil, 13.16,  2.00,  2.00, 13.16,  2, 27)

        return path
        }()

    static var transform = CGAffineTransformMakeScale(0.5, 0.5)
    let shortStroke: CGPath! =  CGPathCreateCopyByTransformingPath(MenuButton.shortStrokeOrig, &transform)
    let outline: CGPath = CGPathCreateCopyByTransformingPath(outlineOrig, &transform)
}