So in my Swift OS X app I have one custom view and one button. When I start my app, my view shows a red oval and I need to change that drawing to drawRectangle()
-method when i click my button.
在我的Swift OS X应用中,我有一个自定义视图和一个按钮。当我启动应用程序时,我的视图会显示一个红色的椭圆,当我点击按钮时,我需要将绘图更改为drawrectang()方法。
My custom view's class MyView
looks like following:
我的自定义视图的类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?
}
}
And I have my DrawingMethods
class:
我有我的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()
}
}
So how can i get my custom views draw rectangle instead of oval?
那么如何让我的自定义视图绘制矩形而不是椭圆呢?
1 个解决方案
#1
1
Call setNeedsDisplayInRect()
on the view after setting isTrue = false
. This will notify the view that it needs to redraw and drawRect
will be called again.
设置isTrue = false后,在视图上调用setNeedsDisplayInRect()。这将通知视图需要重新绘制,并再次调用drawRect。
Your buttonPressed
function should be defined in the ViewController
that contains the custom view
你的buttonPressed函数应该在包含自定义视图的ViewController中定义
import Cocoa
class ViewController: NSViewController {
@IBOutlet customView: MyView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func buttonPressed(sender: AnyObject) {
customView.isTrue = false
customView.setNeedsDisplayInRect(customView.bounds)
}
}
Alternatively, you can just set the needsDisplay
property of the view to true
to redraw the entire view:
或者,您可以将视图的需要显示属性设置为true,以重新绘制整个视图:
customView.needsDisplay = true
#1
1
Call setNeedsDisplayInRect()
on the view after setting isTrue = false
. This will notify the view that it needs to redraw and drawRect
will be called again.
设置isTrue = false后,在视图上调用setNeedsDisplayInRect()。这将通知视图需要重新绘制,并再次调用drawRect。
Your buttonPressed
function should be defined in the ViewController
that contains the custom view
你的buttonPressed函数应该在包含自定义视图的ViewController中定义
import Cocoa
class ViewController: NSViewController {
@IBOutlet customView: MyView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func buttonPressed(sender: AnyObject) {
customView.isTrue = false
customView.setNeedsDisplayInRect(customView.bounds)
}
}
Alternatively, you can just set the needsDisplay
property of the view to true
to redraw the entire view:
或者,您可以将视图的需要显示属性设置为true,以重新绘制整个视图:
customView.needsDisplay = true