UIView Flip过渡不起作用

时间:2012-12-17 07:35:50

标签: ios uiview transition viewflipper

最近我正在做一些类似翻转卡的项目。我在视图控制器中有两个子视图,我想点击一个按钮翻转子视图。我现在能够实现的是整个视图都在翻转,但我只想要子视图=(请让我知道我的代码有什么问题.....;(谢谢你们)

- (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];
}

2 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分:0)

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

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

整个视图被翻转的原因是“transitionFromView”查找被调用的视图,并在那里进行动画制作。在你的情况下,它在viewcontroller中调用,因此整个视图都被翻转。如果您只希望对特定视图进行动画处理,则必须在子视图中调用它。