0

最近我正在做一些类似翻转卡的项目。我在视图控制器中有两个子视图,我想通过单击按钮来翻转子视图。我现在可以实现的是整个视图都在翻转,但我只想要子视图=(请让我知道我的代码有什么问题.....;(谢谢大家

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

2 回答 2

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];
}
于 2012-12-17T07:58:42.643 回答
0

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

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

The reason why whole view is being flipped is because "transitionFromView" looks for a view where it is being called, and does animation there. In your case it is called in viewcontroller, so whole of your view is being flipped. You have to call it in subview, if you want only particular view to be animated.

于 2012-12-17T11:18:41.050 回答