【问题标题】:UIView Flip transition not workingUIView 翻转过渡不起作用
【发布时间】:2012-12-17 07:35:49
【问题描述】:

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

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

【问题讨论】:

    标签: ios uiview transition viewflipper


    【解决方案1】:

    将另一个视图(我称之为容器)添加到 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];
    }
    

    【讨论】:

    • 非常感谢。但是你能解释一下为什么我们需要一个容器吗?
    • @HebitzLau,我希望我能 - 从方法读取的方式来看,它似乎应该在没有一个的情况下工作。
    【解决方案2】:

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

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

    整个视图被翻转的原因是因为“transitionFromView”在它被调用的地方寻找一个视图,并在那里做动画。在您的情况下,它在视图控制器中被调用,因此您的整个视图都被翻转了。如果您只想对特定视图进行动画处理,则必须在子视图中调用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-25
      • 1970-01-01
      • 2012-02-18
      • 2019-01-16
      • 1970-01-01
      相关资源
      最近更新 更多