2012-12-17 76 views
0

最近我正在做一些像翻转卡的项目。我在视图控制器内有两个子视图,我想在点击按钮时翻转子视图。我现在能做到的,是整个视图翻转,但我只是想子视图ONLY =(请让我知道什么是错我的代码......(感谢球员UIView翻转过渡不起作用

- (void)test: (id)sender{ 
[UIView transitionFromView:firstView 
        toView:secondView 
        duration:0.5f 
        options:UIViewAnimationOptionTransitionFlipFromRight 
       completion:^(BOOL finished){ 
        /* do something on animation completion */ 
       }]; 

} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    firstView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 250, 250)]; 
    secondView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 250, 250)]; 
    firstView.backgroundColor = [UIColor redColor]; 
    secondView.backgroundColor = [UIColor blueColor]; 
    UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    button.frame = CGRectMake(0, 0, 25, 25); 
    [button addTarget:self action:@selector(test:)forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:button]; 
    [self.view addSubview:secondView]; 
    [self.view addSubview:firstView]; 
} 

回答

0

添加另一种观点(我把它称为容器)self.view,并添加firstView而不是直接self.view。你根本不需要添加第二个视图 - 转换为你做。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    UIView *container = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 250, 250)]; 
    firstView = [[UIView alloc] initWithFrame:CGRectMake(0,0, 250, 250)]; 
    secondView = [[UIView alloc] initWithFrame:CGRectMake(0,0, 250, 250)]; 
    firstView.backgroundColor = [UIColor redColor]; 
    secondView.backgroundColor = [UIColor blueColor]; 
    UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    button.frame = CGRectMake(0, 0, 25, 25); 
    [button addTarget:self action:@selector(test:)forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:button]; 
    [container addSubview:firstView]; 
    [self.view addSubview:container]; 
} 
+0

非常感谢。但是,你能否解释一下为什么我们需要一个容器? – HebitzLau

+0

@ HebitzLau,我希望我可以 - 看来它应该在方法读取方式中没有一个工作。 – rdelmar

0

我建议创建一个UIView子类(如前面提到的容器),它有两个属性:firstView & secondView。 然后实现一个“flip”方法在该子类是这样的:

- (void)flip 
{ 
    [UIView transitionFromView:_firstView toView:_secondView ...]; 
} 

为什么全视图被翻转是因为“transitionFromView”寻找一个在那里被称为视图,并做动画有其原因。在你的情况下,它是在viewcontroller中调用的,所以你的整个视图都被翻转了。你必须在子视图中调用它,如果你只想让特定的视图变成动画。