2016-07-26 45 views
0

我试图让rectangle跟踪与滑块(不能代替拇指)NSMakeRect跟踪滑块斯威夫特3

要做到这一点我使用所谓gateboxNSMakeRect (80,(sliderValue),80,40)自定义类。

我已经使用了一个全局变量只是为了方便阅读,但已尝试从主控制器调用该函数,结果仍然相同。所以我认为使用全球性更容易。

我也尝试把推子放在自定义视图上。
矩形确实使用滑块进行跟踪,但只能根据此图片显示拇指宽度中包含的部分。

enter image description here

这里是我的代码,它有点粗糙和准备,但显示了问题
感谢阅读,如果你有这么远。

这里是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 
+0

您是不是要调用super.draw(rect)?这可能会在每帧之后清理图纸。尝试把它放在绘图函数 – Luke

+0

的开始,我很抱歉,但我不确定在哪里添加超级。我尝试了各种可以详细说明的地方。 – oldman

+0

就在Swift.print(“你开火了”)之上。值得注意的是,你也不需要Swift部分,'print(“你开火了”)'也可以。 – Luke

回答

0

我需要添加

self.needsDisplay = true 

修改为GateBox.swift文件