简体   繁体   中英

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

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

In your viewDidLoad method you have

[self.someSuperview addSubview:self.labelContainerView];

In your viewWillAppear you have

[self.labelContainerView addSubview:self.blankLabelView];

Then in your animation you have

[self.blankLabelView removeFromSuperview];
[containerView addSubview:self.labelToTransition];

labelToTransition (label) not added to any view

I haven't used this transition effect before, but I'm trying to understand the view hierarchy:

Initially (viewDidLoad and viewWillAppear) somSuperView |__>labelToContainerView

labelContainerView
 |__>blankLabelView

Then through animation

containerView == labelContainerView
 |__>labelToTransition (label)

blankLabelView no longer in a view

The first thing I would say is your code is very hard to follow. Which view is the visible view? Are we supposed to assume it is someSuperView ? It looks like the label ends up in containerView , so maybe that is the visible view? It's not clear what's going on with the code as you have written it.

Can you update your question with the name of the visible "main" view and what subviews you expect to be visible before the transition? And then what is the visible "main" view after the transition, and what superviews you expect to be visible after the transition?

When you say the flip is happening, could it be that you see something flipping, but it might not be the views that you are expecting to flip? Could this account for you not being able to see the subviews after the flip? And why do you say you can't see the subviews, plural? What I read sounds like you only expect to see the label after your flip is done.

As i can see in your code that you already added both of your subviews to your main view. Transition steps are fine and happening without error. If you want the transition of one view on another i suggest you to remove the first one and then add the second view with transition effect you want and then again remove your current view from superview and add back your first view if you want functionality like to show two views alternatively.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM