Search code examples
iosuiviewanimationnsoperationqueue

update UI with animation synchronously or in serial queue


I have custom build menu which pushes or pops views. for example menu1 pushes a view. if menu2 is pressed the removes previous view and show other view. pushing and popping are animated. how to serially update/push/pop views with animation? it should no ignore any menu selection

i have used dispatch_queue_t and NSOperationQueue to no avail as if it ignores uiview animation. the self.uiOperationQueue has 1 as max concurrent operation.

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

with dispatch queue:

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

so basically the [self updateViewsForSelection:menuType] is called with different menu and uiview animation with duration. what am i doing wrong?


Solution

  • Its possible to add animation blocks into an array and execute them one after another using the 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];
            }
        }];
    
    }
    

    It can be used like

    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];