我有以下设想:
dispatch_after_delta(0.1, ^{
[self checkForTodaysBonus]; // It contains animation methods.
});和
-(void) checkForTodaysBonus {
// Prepare View and other data and then animate UIView
[Animations moveDown:self.view andAnimationDuration:0.3 andWait:YES andLength:self.view.frame.size.height];
}其中,moveDown方法类似于:
+ (void) moveDown: (UIView *)view andAnimationDuration: (float) duration andWait:(BOOL) wait andLength:(float) length{
__block BOOL done = wait; //wait = YES wait to finish animation
[UIView animateWithDuration:duration animations:^{
view.center = CGPointMake(view.center.x, view.center.y + length);
} completion:^(BOOL finished) {
// This never happens if I call this method from dispatch_after.
done = NO;
}];
// wait for animation to finish
// This loop will allow wait to complete animation
while (done == YES) { // Application unable to break this condition
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
}
}和
void dispatch_after_delta(float delta, dispatch_block_t block){
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delta * NSEC_PER_SEC), dispatch_get_main_queue(), block);
}因此,发出,每当从dispatch_after_delta调用animation方法时,animation方法never gets its completion block。
什么是可能的解决办法?
发布于 2014-04-29 13:51:11
我给你的建议是使用performSelector: withObject: afterDelay:。
将当前的dispatch_after替换为:
[self performSelector:@selector(checkForTodaysBonus) withObject:nil afterDelay:1.0f];发布于 2014-12-24 03:26:15
而不是因为你提交了块
^{
[self checkForTodaysBonus]; // It contains animation methods.
}); 对于主队列,并且主队列是一个串行队列,所以动画完成块要到上面的块返回时才会执行。
要解决这个问题,您可以:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delta * NSEC_PER_SEC), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), block);在我看来,最好不要在分派块中显式地使用NSThread和NSRunLoop。
https://stackoverflow.com/questions/23363970
复制相似问题