0
我試圖讓rectangle
跟蹤與滑塊(不能代替拇指)NSMakeRect跟蹤滑塊斯威夫特3
要做到這一點我使用所謂gatebox
有NSMakeRect (80,(sliderValue),80,40)
自定義類。
我已經使用了一個全局變量只是爲了方便閱讀,但已嘗試從主控制器調用該函數,結果仍然相同。所以我認爲使用全球性更容易。
我也嘗試把推子放在自定義視圖上。
矩形確實使用滑塊進行跟蹤,但只能根據此圖片顯示拇指寬度中包含的部分。
這裏是我的代碼,它有點粗糙和準備,但顯示了問題
感謝閱讀,如果你有這麼遠。
這裏是AppDelegate中:
// AppDelegate.swift
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var mainWindowController: MainWindowController?
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
let mainWindowController = MainWindowController(windowNibName: "MainWindowController")
mainWindowController.showWindow(self)
self.mainWindowController = mainWindowController
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}}
這裏是mainController:
// MainWindowController.swift
var globalVariable = 10.00
import Cocoa
class MainWindowController: NSWindowController {
override func windowDidLoad() {
super.windowDidLoad()
}
@IBAction func slider001(_ sender: AnyObject) {
let data = sender.doubleValue!
globalVariable = data
print("data", data)
}
}//end of the world
下面是自定義視圖自定義類:
// GateBox.swift
import Cocoa
class gateBoxView: NSView {
override func draw(_ dirtyRect: NSRect) {
Swift.print("did you fire")
self.needsDisplay = true //New addition
let mybackgroundColor = NSColor.init(colorLiteralRed: 0.9, green: 0.4, blue: 0.4, alpha: 0.2)
mybackgroundColor.set()
NSBezierPath.fill(bounds)
//just using global variable for testing
let myY = globalVariable * 0.85
Swift.print("myY",myY)
//Draw rectangle and track slider
let myRectangleColor = NSColor(red: 1.0, green: 1.0, blue: 0.0, alpha: 1.0)
let myRectangle = NSMakeRect(75.0, CGFloat(myY),40.0 , 20.0)
let cPath: NSBezierPath = NSBezierPath(rect: myRectangle)
myRectangleColor.set()
cPath.fill()
}//eo overide
您是不是要調用super.draw(rect)?這可能會在每幀之後清理圖紙。嘗試把它放在繪圖函數 – Luke
的開始,我很抱歉,但我不確定在哪裏添加超級。我嘗試了各種可以詳細說明的地方。 – oldman
就在Swift.print(「你開火了」)之上。值得注意的是,你也不需要Swift部分,'print(「你開火了」)'也可以。 – Luke