【问题标题】:How to flip an individual UIView (without flipping the parent view)如何翻转单个 UIView(不翻转父视图)
【发布时间】:2018-09-27 18:35:44
【问题描述】:

这是一个 iPad 项目,我有一个带有多个子视图的 UIView,我正在尝试使用 [UIView transitionFromView:toView:duration:options:completion] 为其中一个 UIView 设置动画,但是当我运行此方法时,整个父视图视图被翻转! 这是我正在使用的代码:

UIView *secondView = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 300, 300)];
[newView setBackgroundColor:[UIColor redColor]];
    [UIView transitionFromView:self.firstView.view toView:secondView duration:1.0 options:UIViewAnimationOptionTransitionFlipFromLeft completion:nil];

关于如何在不为整个父视图设置动画的情况下在这些视图之间切换的任何想法? 谢谢!

【问题讨论】:

    标签: objective-c ios uiviewanimation uiviewanimationtransition


    【解决方案1】:

    此代码可能对您有所帮助:

    将要翻转的两个视图放在一个大小相同的未命名视图中 并将IBOutlet UIView *newView,*oldView; 链接到视图并将新视图放在顶部

    bool a = NO;
    
    @implementation ViewController
    
    - (IBAction)flip:(id)sender 
    {
        if (a == NO) {
            [UIView transitionFromView:oldView toView:newView  
                      duration:1.0 
                      options:UIViewAnimationOptionTransitionFlipFromLeft 
                      completion:NULL];
            a = YES; // a = !a;
        }
        else {
            [UIView transitionFromView:newView toView:oldView  
                      duration:1.0 
                      options:UIViewAnimationOptionTransitionFlipFromLeft 
                      completion:NULL];
            a = NO; // a = !a;
        }
    }
    

    【讨论】:

    • 非常感谢!解决方案是使用容器视图。
    • @Floris497,你拯救了我的一天。非常感谢:)
    • 谢谢。我有一种感觉,这就是解决方案。我猜转换更像是一个技巧,它在父视图上做一个翻转动画,并且在中途换出你指定的子视图。再说一次,iPhone 上的大多数动画等都是一个把戏 :-)
    • 这对我有用。但是,视图在翻转期间被释放。我的解决方案是让参考更强大。
    • Floris497 字典可以帮助你;)
    【解决方案2】:

    我也遇到了问题。我使用了 Floris 编写的代码,但尽管它适用于第一个“翻转”,但下一个翻转开始变得奇怪,前端 UI 视图上的按钮和标签丢失了位置。

    我将下面的代码放在适当的位置,它工作正常。

    需要注意的几点:

    1. panelView 是 ViewController 上的 UIView 控件,其中嵌套了两个其他 UIView(子视图)。第一个称为frontView,第二个称为backView。 (见下图)

    1. 我在类中有一个名为 displayFront 的 bool 属性

    (在我的 .h 中)

    @property BOOL displayingFront;
    

    在我的 .m 中

    @synthesize displayingFront;
    

    在我的 viewDidLoad 方法中

    self.displayingFront = YES;
    

    这是 .m 中的代码,如果正面和背面 UIViews,我已将其绑定到两个按钮...

    - (IBAction)flip:(id)sender
    {
        [UIView transitionWithView:self.panelView
                          duration:1.0
                           options:(displayingFront ? UIViewAnimationOptionTransitionFlipFromRight :
                                    UIViewAnimationOptionTransitionFlipFromLeft)
                        animations: ^{
                            if(displayingFront)
                            {
                                self.frontView.hidden = true;
                                self.backView.hidden = false;
                            }
                            else
                            {
                                self.frontView.hidden = false;
                                self.backView.hidden = true;
                            }
                        }
    
                        completion:^(BOOL finished) {
                            if (finished) {
                                displayingFront = !displayingFront;
                            }
                        }];
    }
    

    【讨论】:

    • 我认为这比答案更干净
    • 这种方式的不同之处在于动画是在翻转之后发生的……所以frontView是隐藏的,而backView是在翻转之后显示的。上面详述的“transitionFromView”方法在中点添加视图,所以当翻转完成时,“toView”已经显示。
    • 谢谢!这解决了我的过渡视图(标签)的子视图错位的问题。它们散落在屏幕上。
    • 哇……!!精彩的答案。谢谢。
    • 太棒了...它节省了我的时间。
    【解决方案3】:

    使用容器视图来保存您的子视图,并使容器视图与您尝试设置动画的子视图大小相同。

    因此,您可以像这样设置视图。

    self.view->“容器视图”->子视图

    【讨论】:

      【解决方案4】:
      **//flipping view continuously**  
      
      
      
       bool a = NO;
      
      
      -(void)viewDidLoad
      {
      
      
      
      // THIS is the canvass holding the two views this view is flipping
       UIView *v=[[UIView alloc]initWithFrame:CGRectMake(40, 40, 150, 150)];
      
         v.backgroundColor=[UIColor clearColor];
      
          [self.view addSubview:v];
      
           [v addSubview:yourfirstview];
           [v addSubview:yoursecondview];
      
      // calling the method
      
      [NSTimer scheduledTimerWithTimeInterval:2.0
                                           target:self
                                         selector:@selector(flip)
                                         userInfo:nil
                                          repeats:YES];
      
      }
      
      
      
      //flipping view randomly
      
      -(void)flip 
      
      {
      
      if (a == NO) 
      {
      
      [UIView transitionFromView:yourfirstview toView:yoursecondview duration:1.0 options:UIViewAnimationOptionTransitionFlipFromLeft completion:NULL];
              a = YES;
          }
      
      else if(a==YES) 
      {
      
      [UIView transitionFromView:yoursecondview toView:yourfirstview duration:1.0 options:UIViewAnimationOptionTransitionFlipFromLeft completion:NULL];
           a = NO;
        }
      
      }
      

      【讨论】:

        【解决方案5】:

        我通过使用老式动画解决了这个问题:

        [UIView beginAnimations:@"Flip" context:nil];
        [UIView setAnimationDuration:1];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:firstView cache:YES];
        
        [self addSubview:secondView];
        
        [UIView commitAnimations];
        

        另外你可以删除firstView:

        [firstView removeFromSuperview];
        

        【讨论】:

          【解决方案6】:

          我想更新 Steve Parker 在 SWIFT 4 中的回答:-

          var displayBlankView:Bool = true
          
          UIView.transition(with:flipView, duration:1.0, options:displayBlankView ? .transitionFlipFromLeft:.transitionFlipFromRight, animations:{
                  if(self.displayBlankView){
                      self.view1.isHidden=true
                      self.view2.isHidden=false
                  }else{
                      self.view1.isHidden=false
                      self.view2.isHidden=true
                  }
              }, completion:{
                  (finished: Bool) in
                  self.displayBlankView = !self.displayBlankView;
          
              })
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-04-03
            • 2010-10-25
            • 2011-11-14
            • 1970-01-01
            相关资源
            最近更新 更多