21

这是一个 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];

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

4

6 回答 6

71

此代码可以帮助您:

将要翻转的两个视图放在具有相同大小的未命名视图中,并将其链接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;
    }
}
于 2012-03-01T22:00:24.207 回答
37

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

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

需要注意的几点:

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

IB 中的视图和子视图的图像

  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;
                        }
                    }];
}
于 2013-04-07T01:20:49.193 回答
3

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

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

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

于 2012-03-01T21:52:05.067 回答
2
**//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;
  }

}
于 2013-01-19T12:52:55.080 回答
2

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

[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];
于 2013-08-30T14:31:05.250 回答
2

我想更新史蒂夫帕克在 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;

    })
于 2018-09-27T10:35:44.207 回答