-2

I have a UIView with several UILabels added. I am simply moving them all to the center of the screen with an animation, and then attempting to remove them from their superview in the animation completion handler.

for (label in [self.view subviews])
{
    if([label isKindOfClass:[UILabel class]])
    {
        CGRect frame = CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/2, label.frame.size.width, label.frame.size.height);
        [UIView animateWithDuration:2.0
                              delay:0.0
                            options: UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             [self->label setFrame:frame];
                         }
                         completion:^(BOOL finished){
                             dispatch_async(dispatch_get_main_queue(),^{
                                 [self->label removeFromSuperview];
                             });
                         }
         ];
    }
}

The problem that I am having is that at the end of the animation the UILabels remain. If I put the removeFromSuperView call outside of the animation block then it works, but of course then they are removed before the animation has a chance to complete.

Scooter
  • 4,068
  • 4
  • 32
  • 47
  • 1
    You've got `label` as the variable in the for-in and `self->label` in the blocks. Are you sure you're operating on the label you think you are? – Ken Thomases Jun 26 '19 at 02:34
  • Nope...I was not, and you Sir are a genius. Post this as an answer and I will be glad to accept it. Getting sloppy with variable names will burn you every time! Thank you! – Scooter Jun 26 '19 at 02:38

1 Answers1

1

You've got label as the variable in the for-in and self->label in the blocks. Apparently, you weren't operating on the label you thought you were.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • This answer is spot on. I also had a @property named label. Once I renamed the local variable (label) to something else it started working as expected. This code is just in a small experimental proof-of-concept that I was playing around with, and as such I was lazy with my naming conventions. Thanks again! – Scooter Jun 26 '19 at 08:49