Search code examples
iphoneiosuiviewuiviewanimationtransition

UIView transition animation not showing subviews


I'm using a container view as a superview to two subviews. I want to "flip" one subview to another subview. The first subview happens to be blank and the second subview is a UILabel that has some text.

The flip is happening but I do not see my subviews.

I want to transition blankLabelView to labelToTransition. Any ideas? Thanks!

The code:

-(void)viewDidLoad {
    CGRect labelFrame = CGRectMake(100.0, 217.0, 125.0, 34.0);

    self.labelContainerView = [[UIView alloc] initWithFrame:labelFrame];
    // If I change the background color to something other than clear
    // I can see that this view is flipping, otherwise it appears that
    // nothing happens.
    self.labelContainerView.backgroundColor = [UIColor clearColor];
    [self.someSuperview addSubview:self.labelContainerView];

    UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
    label.backgroundColor = [UIColor clearColor];
    label.lineBreakMode = UILineBreakModeWordWrap;
    label.numberOfLines = 2;
    label.textAlignment = UITextAlignmentCenter;
    self.labelToTransition = label;

    UIView *blankView = [[UIView alloc] initWithFrame:labelFrame];
    blankView.backgroundColor = [UIColor clearColor];
    self.blankLabelView = blankView;
}

-(void)viewWillAppear {
   self.labelToTransition.text = @"some text from a model object";

   [self.labelContainerView addSubview:self.blankLabelView];
}

-(void)flipViewAnimation {
    UIView *containerView = self.labelContainerView;

    [UIView transitionWithView:containerView duration:1.0 options:UIViewAnimationOptionTransitionFlipFromBottom 
                animations:^{ 
                    [self.blankLabelView removeFromSuperview];
                    [containerView addSubview:self.labelToTransition];
                }
                completion:NULL];
}

Solution

  • probably frame of UILabel is not set properly

    try

    CGRect labelFrame = CGRectMake(0, 0.0, 125.0, 34.0);
    

    For the function of flipping try following snippet

    -(void)LetUsFlip
    {
    [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:1.0];  
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:flipContainerView cache:YES];
    
        if ([self.blankLabelView superview])
        {
            [self.blankLabelView removeFromSuperview];
            [self.labelContainerView addSubview:self.labelToTransition];
            [self.labelContainerView sendSubviewToBack:self.blankLabelView];
        }
        else
        {
            [self.labelToTransition removeFromSuperview];
            [self.labelContainerView addSubview:self.blankLabelView];
            [self.labelContainerView sendSubviewToBack:self.labelToTransition];
        }
    
        [UIView commitAnimations];
    }