In my UI, when a button is tapped, it calls a for loop that executes several tasks sequentially.
在我的UI中,当点击一个按钮时,它会调用一个for循环,按顺序执行几个任务。
// For Loop
for (int i = 1; i <= 3; i++)
{
// Perform Task[i]
}
// Results:
// Task 1
// Task 2
// Task 3
After each task, I would like add a user-defined delay. For example:
在完成每项任务后,我想添加一个用户定义的延迟。例如:
// For Loop
for (int i = 1; i <= 3; i++)
{
// Perform Task[i]
// Add Delay Here
}
// Results:
//
// Task 1
// Delay 2.5 seconds
//
// Task 2
// Delay 3 seconds
//
// Task 3
// Delay 2 seconds
In iOS, using Objective-C, is there a way to add such delays within a for loop, keeping in mind:
在iOS中,使用Objective-C,有没有办法在for循环中添加这样的延迟,请记住:
- The UI should remain responsive.
- The tasks must be performed in order, sequentially.
用户界面应保持响应。
必须按顺序执行任务。
A code example within the context of a for loop would be most helpful. Thank you.
for循环上下文中的代码示例将是最有帮助的。谢谢。
4 个解决方案
#1
5
Use GCD dispatch_after
. You can search its usage on *. Nice article is here
使用GCD dispatch_after。您可以在*上搜索其用法。好文章在这里
Brief example in Swift for 1.5 seconds delay:
Swift中的简短示例延迟1.5秒:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(Double(NSEC_PER_SEC) * 1.5)), dispatch_get_main_queue()) {
// your code here after 1.5 delay - pay attention it will be executed on the main thread
}
and objective-c:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^ {
// your code here after 1.5 delay - pay attention it will be executed on the main thread
});
#2
1
This sounds like an ideal job for NSOperationQueue
with the delay being implemented like this:
对于NSOperationQueue来说,这听起来像是一个理想的工作,延迟实现如下:
@interface DelayOperation : NSOperation
@property (NSTimeInterval) delay;
- (void)main
{
[NSThread sleepForTimeInterval:delay];
}
@end
#3
0
Would this solution work? Instead of using dispatch_after, I use dispatch_async with a [NSThread sleepForTimeInterval] block, which allows me to put a delay anywhere that I need in my custom queue.
这个解决方案有用吗?而不是使用dispatch_after,我使用dispatch_async和[NSThread sleepForTimeInterval]块,这允许我在我的自定义队列中的任何地方放置延迟。
dispatch_queue_t myCustomQueue;
myCustomQueue = dispatch_queue_create("com.example.MyQueue", NULL);
dispatch_async(myCustomQueue, ^ {
NSLog(@“Task1”);
});
dispatch_async(myCustomQueue, ^ {
[NSThread sleepForTimeInterval:2.5];
});
dispatch_async(myCustomQueue, ^ {
NSLog(@“Task2”);
});
dispatch_async(myCustomQueue, ^ {
[NSThread sleepForTimeInterval:3.0];
});
dispatch_async(myCustomQueue, ^ {
NSLog(@“Task3”);
});
dispatch_async(myCustomQueue, ^ {
[NSThread sleepForTimeInterval:2.0];
});
#4
0
Heres a Swift version:
这是一个Swift版本:
func delay(seconds seconds: Double, after: ()->()) {
delay(seconds: seconds, queue: dispatch_get_main_queue(), after: after)
}
func delay(seconds seconds: Double, queue: dispatch_queue_t, after: ()->()) {
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(seconds * Double(NSEC_PER_SEC)))
dispatch_after(time, queue, after)
}
How you call it:
你怎么称呼它:
print("Something")
delay(seconds: 2, after: { () -> () in
print("Delayed print")
})
print("Anotherthing")
#1
5
Use GCD dispatch_after
. You can search its usage on *. Nice article is here
使用GCD dispatch_after。您可以在*上搜索其用法。好文章在这里
Brief example in Swift for 1.5 seconds delay:
Swift中的简短示例延迟1.5秒:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(Double(NSEC_PER_SEC) * 1.5)), dispatch_get_main_queue()) {
// your code here after 1.5 delay - pay attention it will be executed on the main thread
}
and objective-c:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^ {
// your code here after 1.5 delay - pay attention it will be executed on the main thread
});
#2
1
This sounds like an ideal job for NSOperationQueue
with the delay being implemented like this:
对于NSOperationQueue来说,这听起来像是一个理想的工作,延迟实现如下:
@interface DelayOperation : NSOperation
@property (NSTimeInterval) delay;
- (void)main
{
[NSThread sleepForTimeInterval:delay];
}
@end
#3
0
Would this solution work? Instead of using dispatch_after, I use dispatch_async with a [NSThread sleepForTimeInterval] block, which allows me to put a delay anywhere that I need in my custom queue.
这个解决方案有用吗?而不是使用dispatch_after,我使用dispatch_async和[NSThread sleepForTimeInterval]块,这允许我在我的自定义队列中的任何地方放置延迟。
dispatch_queue_t myCustomQueue;
myCustomQueue = dispatch_queue_create("com.example.MyQueue", NULL);
dispatch_async(myCustomQueue, ^ {
NSLog(@“Task1”);
});
dispatch_async(myCustomQueue, ^ {
[NSThread sleepForTimeInterval:2.5];
});
dispatch_async(myCustomQueue, ^ {
NSLog(@“Task2”);
});
dispatch_async(myCustomQueue, ^ {
[NSThread sleepForTimeInterval:3.0];
});
dispatch_async(myCustomQueue, ^ {
NSLog(@“Task3”);
});
dispatch_async(myCustomQueue, ^ {
[NSThread sleepForTimeInterval:2.0];
});
#4
0
Heres a Swift version:
这是一个Swift版本:
func delay(seconds seconds: Double, after: ()->()) {
delay(seconds: seconds, queue: dispatch_get_main_queue(), after: after)
}
func delay(seconds seconds: Double, queue: dispatch_queue_t, after: ()->()) {
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(seconds * Double(NSEC_PER_SEC)))
dispatch_after(time, queue, after)
}
How you call it:
你怎么称呼它:
print("Something")
delay(seconds: 2, after: { () -> () in
print("Delayed print")
})
print("Anotherthing")