我正在创建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 shortStrokeOrig
和outlineOrig
相同。
不确定为什么抱怨。或者,如果有人对如何扩展我的路径有任何更好的建议..
答案 0 :(得分:1)
问题出在shortStroke
常量初始化 -
CGPathCreateCopyByTransformingPath(shortStrokeOrig, CGAffineTransformMakeScale(0.5, 0.5))
问题:
shortStrokeOrig
- 被声明为实例变量,但您将其用作类变量。CGPathCreateCopyByTransformingPath
函数的第二个参数类型为UnsafePointer<CGAffineTransform>
而不是CGAffineTransform
。解决方案
shortStrokeOrig
和outlineOrig
设为类变量代码:
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)
}