使用动画同步或在串行队列中更新 UI



我有自定义构建菜单,可以推送或弹出视图。 例如,菜单 1 推送视图。 如果按下 Menu2,则删除上一个视图并显示其他视图。推送和弹出是动画的。如何使用动画连续更新/推送/弹出视图?它不应该忽略任何菜单选择

我使用了dispatch_queue_tNSOperationQueue无济于事,就好像它忽略了UIview动画一样。 self.uiOperationQueue的最大并发操作数为 1。

[self.uiOperationQueue addOperationWithBlock:^{
    dispatch_async(dispatch_get_main_queue(), ^{
            [self updateViewsForSelection:menuType];
    });
}];

使用调度队列:

self.uiUpdateQueue = dispatch_queue_create("ui-updateQueue", NULL);
dispatch_async(self.uiUpdateQueue, ^{
        [self updateViewsForSelection:menuType];
});

所以基本上[self updateViewsForSelection:menuType]是用不同的菜单和具有持续时间的UIview动画调用的。 我做错了什么?

可以将animation blocks添加到数组中,并使用completion block一个接一个地执行它们

-(void)executeAnimationsInArray:(NSArray *)array atIndex:(int)index
{
    [UIView animateWithDuration:1 animations:array[index] completion:^(BOOL finished) {
        if (index < array.count - 1)
        {
            [self executeAnimationsInArray:array atIndex:index+1];
        }
    }];
}

它可以像

id block1 = ^(void) {
    imgView.frame = CGRectOffset(imgView.frame, 10, 10);
};
id block2 = ^(void) {
    imgView.frame = CGRectOffset(imgView.frame, -10, -10);
};
[self executeAnimationsInArray:@[block1, block2] atIndex:0];

最新更新