小编Ale*_*dro的帖子

NSTimer在此函数中失效时不会停止

我正在尝试使用NTTimer,但它不起作用.

它从这里开始:

timer = NSTimer.scheduledTimerWithTimeInterval(0.003, target: self, selector: "openFrameAnimation", userInfo: nil, repeats: true)
Run Code Online (Sandbox Code Playgroud)

它激发了这个功能:

func openFrameAnimation(){
    Swift.print("Animation Goes... ",NSDate().timeIntervalSince1970)
    self.frame = NSRect(x: self.frame.origin.x,
        y: self.frame.origin.y-12,
        width: self.frame.width,
        height: self.frame.height + 12)
    if(self.frame.height >= self.finalFrame.height){
        Swift.print("STOP")
        self.frame = self.finalFrame
        timer.invalidate()
        // timer = nil //ERROR: nil cannot be assigned to type "NSTimer"
    }
    self.needsDisplay = true
}
Run Code Online (Sandbox Code Playgroud)

即使反复打印"STOP",计时器仍继续调用openFrameAnimation,我无法阻止它.如果验证条件,我该如何停止计时器?

日志:

[...]
*Animation Goes...  1456613095.27813
STOP
[...]
Animation Goes...  1456613095.51233
STOP
Animation Goes...  1456613095.54458
[...]
STOP
Animation Goes...  1456613095.5938
STOP*
Run Code Online (Sandbox Code Playgroud)

nstimer swift

2
推荐指数
1
解决办法
3011
查看次数

Cocoa NSScrollView不滚动

我尝试创建一个scrollview.通过Xib创建了一个scrollview,以编程方式添加了scrolledView(一个包含scrollView中所有子视图的NSView).

以下代码显示了子视图,但scrollView未滚动.为什么?

class LevelScrollController: NSViewController {

@IBOutlet var scrollView: NSScrollView!

override func viewDidLoad() {
    super.viewDidLoad()

    let scrolledView = NSView(frame: NSRect(x: 0, y: 0, width: scrollView.frame.size.width, height: 300))

    // Inserisco pulsanti di esempio
    for i in 1 ... 10{
        scrolledView.addSubview(NSButton(frame: NSRect(x: 0, y: i*30, width: 100, height: 30)))
    }
    scrollView.addSubview(scrolledView)



}
Run Code Online (Sandbox Code Playgroud)

将代码放在viewDidLayout而不是viewDidLoad中不会更改结果:不滚动

macos cocoa nsscrollview swift

1
推荐指数
1
解决办法
999
查看次数

标签 统计

swift ×2

cocoa ×1

macos ×1

nsscrollview ×1

nstimer ×1