0
everyone!NSImage -init中的繪圖處理程序不起作用(Swift)
我想爲awakeFromNib NSButton子類方法中的按鈕設置圖像。 在界面生成器中,我將按鈕類設置爲我的自定義類。當我把繪圖代碼放在覆蓋的drawRect方法中時,繪圖效果很好。
爲什麼繪圖處理器並不在此代碼的工作
import Cocoa
@IBDesignable class PreviousTrackButton: NSButton {
@IBInspectable var dx: CGFloat = 10
@IBInspectable var dy: CGFloat = 10
override func awakeFromNib() {
super.awakeFromNib()
var imageframe = NSInsetRect(self.bounds, dx, dy)
let buttonImage = NSImage(size: imageframe.size, flipped: false, drawingHandler: {imageframe in
NSGraphicsContext.saveGraphicsState()
var fillColor = NSColor.whiteColor()
println("000") // log does not in consol.
let point1 = NSPoint(x: NSMinX(imageframe), y: NSMidY(imageframe))
let point2 = NSPoint(x: NSMidX(imageframe), y: NSMaxY(imageframe))
let point3 = NSPoint(x: NSMidX(imageframe), y: NSMidY(imageframe))
let point4 = NSPoint(x: NSMaxX(imageframe), y: NSMaxY(imageframe))
let point5 = NSPoint(x: NSMaxX(imageframe), y: NSMinY(imageframe))
let point6 = NSPoint(x: NSMidX(imageframe), y: NSMinY(imageframe))
let path = NSBezierPath()
path.moveToPoint(point1)
path.lineToPoint(point2)
path.lineToPoint(point3)
path.lineToPoint(point4)
path.lineToPoint(point5)
path.lineToPoint(point3)
path.lineToPoint(point6)
path.lineToPoint(point1)
path.closePath()
fillColor.setFill()
path.fill()
NSGraphicsContext.restoreGraphicsState()
return true
})
self.image = buttonImage
println("buttonImage") // buttonImage not nil and have an appropriate size
}
}
的println( 「000」)的日誌不會出現在CONSOL。繪圖處理程序不會調用。 我做錯了什麼?
NSView的子類沒有「layoutSubviews」方法。是的,在我的情況下,實時渲染不起作用,但我現在只談論繪圖處理程序。只有這段代碼不會調用。正如你可能看到buttonImage實例不是零。 –