Pergunta

I have found a solution here How to flip an individual UIView (without flipping the parent view)

but it seems that this doesn't work for me. In Interface Builder i have set up all with a uiview and inside that two uiviews, with the same size and the first view to be shown on top and i have created three outlets for this 3 views.

In viewDidLoad:

 [self.view addSubview:self.contentView]
 [self.contentView addSubview:firstView]
 [self.contentView addSubview:secondView]

i have a uigesture. When the user taps the screen, the IBAction executes the flip:

-(IBAction)flip:(id *)sender
{
 if(faceUp){

       // execute transition from firstview to second
 }else{

      //execute transition from secondview to first
 }

 faceUp = !faceUp;
}

this work perfectly the first time i click and the flips happens from the first to second, then if i tap again the flip starts and then it ends with nothing, the view disappears and remains the container view showing nothing because its color is set to clear. How can i resolve this?

This is the controller with the tap gesture method and the three outlets:

  #import "ContactViewController.h"
  #import "InfoContactView.h"
  #import "ImageLogoView.h"

  @interface ContactViewController ()
  @property (strong, nonatomic) IBOutlet InfoContactView *contactInfoView;
  @property (strong, nonatomic) IBOutlet ImageLogoView *imageLogoView;
  @property (nonatomic) BOOL faceUp;
  @property (strong, nonatomic) IBOutlet UIView *parentView;


  @end

 @implementation ContactViewController

 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
 {
  self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  if (self) {
    // Custom initialization
  }
 return self;
 }

- (void)viewDidLoad
{
  [super viewDidLoad];
 // Do any additional setup after loading the view.

  self.parentView.backgroundColor =[UIColor clearColor];
  [self.view addSubview:self.parentView];
  [self.parentView addSubview:self.contactInfoView];
    [self.parentView addSubview:self.imageLogoView];


    self.faceUp = NO;
 }

- (void)didReceiveMemoryWarning
{
     [super didReceiveMemoryWarning];
   // Dispose of any resources that can be recreated.
}


-(void)setFaceUp:(BOOL)faceUp
{
    _faceUp = faceUp;

}

-(void)setParentView:(UIView *)parentView
{
    _parentView =parentView;
}

-(void)setContactInfoView:(InfoContactView *)contactInfoView
{
 _contactInfoView = contactInfoView;
}

-(void)setImageLogoView:(ImageLogoView *)imageLogoView
{
  _imageLogoView = imageLogoView;
}

- (IBAction)showInfo:(UITapGestureRecognizer *)sender {




  if (self.faceUp == NO) {
      [UIView transitionFromView:self.imageLogoView toView:self.contactInfoView
                        duration:1.5
                         options:UIViewAnimationOptionTransitionFlipFromLeft
                      completion:NULL];
      NSLog(@"Pass from logo to info : imageLogoView = %@",self.imageLogoView.description);

      NSLog(@"dettagli = %@",self.contactInfoView);



  }
   else if(self.faceUp == YES){
      [UIView transitionFromView:self.contactInfoView toView:self.imageLogoView
                        duration:1.5
                         options:UIViewAnimationOptionTransitionFlipFromRight
                      completion:NULL];

      NSLog(@"Pass from info to logo: imageLogoView = %@",self.imageLogoView.description);

      NSLog(@"dettagli = %@",self.contactInfoView);

  }

  self.faceUp = !self.faceUp;

 }




 @end
Foi útil?

Solução

I have tried to reconstruct your problem in Xcode. I added some lines of code to log parentView's subviews before and after each transition. Judging from the logs your initial view's frame (self.imageLogoView) is reset to a width and height of 0 at some point:

<UIView: 0x8b84310; frame = (159.5 284.5; 0 0); autoresize = W+H; layer = <CALayer: 0x8b84370>>

which means it is still there but you just can't see it.

Apparently Autolayout is responsible for resizing the view's frame. If you deactivate Autolayout for your View Controller the transition works just fine. (To deactivate Autolayout select the respective View Controller in your Storyboard. In the right column (Utilities) go to the first tab first tab and deselect the checkbox "Use Autolayout". use Autolayout)

On the other hand Autolayout is said to be a nice thing or at least it is promoted by Apple as such. So if you want to use Autolayout you have to take care of your view's frames yourself by adding appropriate constraints. First select your imageLogoView and add constraints for width and height by checking the respective boxes as shown in the screenshot below. Then do the same with your contactInfoView.

Screenshot Xcode Constraints

That should do the trick.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top