Search code examples
iosautolayoutcalayeruiviewanimation

iOS update CALayer in UIView animation block


So i have the following function

- (void)setMoreMenuHidden:(BOOL)hidden animated:(BOOL)animated completion:(void(^)(BOOL))completion
{
    if (!hidden)
    {
        [self.view layoutIfNeeded];
        [self.view setNeedsUpdateConstraints];
        [UIView animateWithDuration:0.5f animations:^
         {
             [self.view layoutIfNeeded];
             [_profileInfoVC viewWillLayoutSubviews];
         }];
    }
}

and within _profileInfoVC i have it doing

-(void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];

    [self.view layoutIfNeeded];
    self.pictureImageView.layer.cornerRadius = self.pictureImageView.frame.size.width/2;
    self.pictureImageView.clipsToBounds = YES;
}

So i am aware that layers are ignored within the animation blocks but I assume this also means updating at all since it remains quite square during and after the animation. Is there a solution to this that does not require me also creating a Core Animation for just the layer?


Solution

  • So it seems the block you are interested in this regard is the Completion block.

    - (void)setMoreMenuHidden:(BOOL)hidden animated:(BOOL)animated completion:(void(^)(BOOL))completion
    {
        if (!hidden)
        {
            [self.view layoutIfNeeded];
            [self.view setNeedsUpdateConstraints];
            [UIView animateWithDuration:0.5f animations:^
             {
                 [self.view layoutIfNeeded];
             }
             completion:^(BOOL finished)
             {
                 [_profileInfoVC viewWillLayoutSubviews];
             }];
        }
    }
    

    the above seems to have fixed my issue, also i was never really a fan of directly calling viewWillLayoutSubviews directly so that line has been changed to [_profileInfoVC.view setNeedsLayout];