Swift-NSTimer& setNeedDisplay

时间:2015-04-16 04:43:28

标签: ios swift setneedsdisplay

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(setNeedsDisplay) userInfo:self repeats:YES];

我想知道如何在swift.thank u。

中编写上面的代码

4 个答案:

答案 0 :(得分:0)

示例使用NSTimer's计时器计划事件 -

// create a timer instance 
let timer = NSTimer(timeInterval: 1.0, target: self, selector: "timerEventTriggered:", userInfo: nil, repeats: true)

// adding timer to current run loop
NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)

// implement the event which is triggered by scheduled timer
func timerEventTriggered(timer:NSTimer!) {
    ...
}

注意:这只是示例用法,您可以根据您的上下文更改变量和方法名称。

答案 1 :(得分:0)

var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("yourFunction"), userInfo: nil, repeats: true)


func yourFunction() {
    // Do something
 }


/////other way.......
 dispatch_after(timer, dispatch_get_main_queue()) { () -> Void in

     //do same like in yourFunction()

   }
 }

答案 2 :(得分:0)

代码可以写成

var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("setNeedsDisplay"), userInfo: self, repeats: true)

答案 3 :(得分:0)

var timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.setNeedsDisplay), userInfo: self, repeats: true)