在早期版本的Swift中,可以使用以下代码创建延迟:
let time = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), 4 * Int64(NSEC_PER_SEC))
dispatch_after(time, dispatch_get_main_queue()) {
//put your code which should be executed with a delay here
}
Run Code Online (Sandbox Code Playgroud)
但现在,斯威夫特3时,Xcode自动改变6个不同的东西,但随后出现以下错误:"无法转换DispatchTime.now
到预期值dispatch_time_t
又名UInt64
".
如何在Swift 3中运行一系列代码之前创建延迟?
我试过了
var timer = NSTimer()
timer(timeInterval: 0.01, target: self, selector: update, userInfo: nil, repeats: false)
Run Code Online (Sandbox Code Playgroud)
但是,我说错了
'(timeInterval: $T1, target: ViewController, selector: () -> (), userInfo: NilType, repeats: Bool) -> $T6' is not identical to 'NSTimer'
Run Code Online (Sandbox Code Playgroud) 我想暂时停止我的应用程序.换句话说,我希望我的应用程序执行代码,但在某个时刻,暂停4秒,然后继续执行其余代码.我怎样才能做到这一点?
我正在使用Swift.
我在Objective C中有一个应用程序,我正在转换到Swift.在Objective C中,我有这个方法:
[self.view performSelector:@selector(someSelector) withObject:self afterDelay:0.1f];
我正在使用Swift,我无法弄清楚如何做到这一点.我试过了:
self.view.performSelector(Selector("someSelector"), withObject: self, afterDelay: 0.1)
这是我得到的错误: 'performSelector' is unavailable: 'performSelector' methods are unavailable
我会用什么电话来调用方法afterDelay
?
UPDATE
这是我最终得到的:
extension NSObject {
func callSelectorAsync(selector: Selector, object: AnyObject?, delay: NSTimeInterval) -> NSTimer {
let timer = NSTimer.scheduledTimerWithTimeInterval(delay, target: self, selector: selector, userInfo: object, repeats: false)
return timer
}
func callSelector(selector: Selector, object: AnyObject?, delay: NSTimeInterval) {
let delay = delay * Double(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(time, dispatch_get_main_queue(), {
NSThread.detachNewThreadSelector(selector, toTarget:self, withObject: object) …
Run Code Online (Sandbox Code Playgroud) I'm setting a new text value to a UILabel
. Currently, the new text appears just fine. However, I'd like to add some animation when the new text appears. I'm wondering what I can do to animate the appearance of the new text.
我没有代码可以采样或任何东西,因为我不知道该怎么做,但有人可以请告诉我如何使用swift延迟一段时间的功能?
许多Cocoa和CocoaTouch方法都有完成回调,实现为Objective-C中的块和Swift中的闭包.但是,在Playground中尝试这些时,永远不会调用完成.例如:
// Playground - noun: a place where people can play
import Cocoa
import XCPlayground
let url = NSURL(string: "http://stackoverflow.com")
let request = NSURLRequest(URL: url)
NSURLConnection.sendAsynchronousRequest(request, queue:NSOperationQueue.currentQueue() {
response, maybeData, error in
// This block never gets called?
if let data = maybeData {
let contents = NSString(data:data, encoding:NSUTF8StringEncoding)
println(contents)
} else {
println(error.localizedDescription)
}
}
Run Code Online (Sandbox Code Playgroud)
我可以在我的Playground时间轴中看到控制台输出,但是println
我的完成块永远不会被调用...
如何在基于用户交互(或缺少用户交互)的iOS应用程序中添加计时器?换句话说,如果2分钟内没有用户交互,我想让应用程序执行某些操作,在这种情况下导航到初始视图控制器.如果在1:55有人触摸屏幕,则计时器重置.我认为这需要一个全局计时器,所以无论你在哪个视图,缺乏交互启动计时器.虽然,我可以在每个视图上创建一个独特的计时器.有没有人有任何建议,链接或示例代码,以前这样做过?
我想在一个事件的10秒内运行一段代码,但是我希望能够取消它,这样如果在那10秒之前发生了某些事情,代码将在10秒后不再运行.
我一直在使用它,但它不可取消:
static func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure
)
}
Run Code Online (Sandbox Code Playgroud)
我怎么能做到这一点?
我试图在一段时间后调用一个方法.
我知道有一个解决方案:
[self performSelector:@selector(myMethod) withObject:nil afterDelay:delay];
Run Code Online (Sandbox Code Playgroud)
但我的问题是:我怎样才能调用一个带两个参数的方法?
例如:
- (void) MoveSomethigFrom:(id)from To:(id)to;
Run Code Online (Sandbox Code Playgroud)
我将如何使用延迟调用此方法 performSelector:withObject:afterDelay:
谢谢