简体   繁体   English

翻转两个子视图不能连续工作

[英]flip two subview doesn't work continuosly

I have found a solution here How to flip an individual UIView (without flipping the parent view) 我在这里找到了一个解决方案, 如何翻转单个UIView(不翻转父视图)

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. 在Interface Builder中,我都设置了一个uiview,并在两个uiview中都设置了相同的大小,并且第一个视图显示在顶部,并且我为这3个视图创建了三个出口。

In viewDidLoad: 在viewDidLoad中:

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

i have a uigesture. 我有一个ugesture。 When the user taps the screen, the IBAction executes the flip: 当用户点击屏幕时,IBAction执行翻转:

-(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

I have tried to reconstruct your problem in Xcode. 我试图用Xcode重建您的问题。 I added some lines of code to log parentView 's subviews before and after each transition. 我在每次转换之前和之后添加了一些代码行来记录parentView的子视图。 Judging from the logs your initial view's frame ( self.imageLogoView ) is reset to a width and height of 0 at some point: 从日志中判断,初始视图的框架( self.imageLogoView )在某个点被重置为宽度和高度0:

<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. 如果您为View Controller停用自动布局,则过渡效果很好。 (To deactivate Autolayout select the respective View Controller in your Storyboard. In the right column (Utilities) go to the first tab (要停用自动布局,请在情节提要中选择相应的View Controller。在右列(实用程序)中转到第一个标签 第一个标签 and deselect the checkbox "Use Autolayout" . 并取消选中“使用自动版式”复选框。 使用自动版式 )

On the other hand Autolayout is said to be a nice thing or at least it is promoted by Apple as such. 另一方面,Autolayout被认为是一件好事,或者至少由Apple这样推广。 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. 首先选择您的imageLogoView并通过选中相应的框来添加宽度和高度的约束,如下面的屏幕快照所示。 Then do the same with your contactInfoView . 然后对您的contactInfoView进行相同的contactInfoView

屏幕快照Xcode约束

That should do the trick. 这应该够了吧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UIButton作为MKAnnotationView中的子视图不起作用 - UIButton as a subview in MKAnnotationView doesn't work 手势识别器不适用于子视图 - gesture recognizer doesn't work on subview FirstViewController不适用于删除SecondViewController的子视图 - FirstViewController doesn't work on removing the subview of SecondViewController UITableViewCell和在layoutSubviews中设置子视图框架第一次不起作用 - UITableViewCell and setting subview frames in layoutSubviews doesn't work first time 自定义uitableviewcell重置子视图框架不起作用 - custom uitableviewcell reset subview frame doesn't work as I wanted touchesbegan在第二个viewcontroller的子视图上不起作用 - touchesbegan doesn't work on subview from second viewcontroller 为什么UITapGestureRecognizer在keyWindow(全屏)的子视图中不起作用? - Why doesn't UITapGestureRecognizer work in subview of keyWindow (full screen)? 为什么在viewDidLoad中添加子视图不起作用 - Why does adding subview in viewDidLoad doesn't work 添加子视图后,UITableView动画不起作用 - UITableView animation doesn't work after adding a subview SwiftUI 动态添加子视图,但 animation 不起作用 - SwiftUI add subview dynamically but the animation doesn't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM