在我的Swift OS X 应用程序中,我有一个自定义视图和一个按钮。当我启动我的应用程序时,我的视图显示一个红色的椭圆形,当我点击按钮时,我需要将该绘图更改为drawRectangle()
方法。
我的自定义视图类MyView
如下所示:
import Cocoa
import AppKit
class MyView: NSView {
var isTrue = true
override func drawRect(dirtyRect: NSRect) {
super.drawRect(dirtyRect)
// Drawing code here.
if isTrue {
DrawingMethods.drawOval()
} else {
DrawingMethods.drawRectangle()
}
}
@IBAction func buttonPressed(sender: AnyObject) {
isTrue = false
// Now I need to update the view, so it draws rectangle isntead of oval. How I do that?
}
}
我有一个名为DrawingMethods
的类:
import Cocoa
public class DrawingMethods: NSObject {
public class func drawOval() {
let color = NSColor(calibratedRed: 1, green: 0, blue: 0, alpha: 1)
let ovalPath = NSBezierPath(ovalInRect: NSMakeRect(64, 54, 50, 45))
color.setFill()
ovalPath.fill()
}
public class func drawRectangle() {
let color = NSColor(calibratedRed: 1, green: 0, blue: 0, alpha: 1)
let rectanglePath = NSBezierPath(rect: NSMakeRect(136, 12, 34, 34))
color.setFill()
rectanglePath.fill()
}
}
那么我该如何让我的自定义视图绘制矩形而不是椭圆形呢?