【问题标题】:Flip View Iphone翻转视图 Iphone
【发布时间】:2010-10-25 00:27:11
【问题描述】:

请考虑下面的代码,并告诉我我做错了什么。

我想在两个 UIView 之间切换。

不知何故,当我从初始视图翻转时,我只是得到翻转的视图,没有动画。当我向后翻转时,动画显示得很好。

翻转由视图本身的按钮触发。

- (IBAction)showMoreInfo:(id)sender
{
    UIView *moreInfo = self.flipView;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2.0];
    [UIView setAnimationBeginsFromCurrentState:NO];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];

    UIView *parent = self.view.superview;
    [self.view removeFromSuperview];

    [parent addSubview:moreInfo];

    [UIView commitAnimations];

}



- (IBAction)showLessInfo:(id)sender
{
    UIView *lessInfo = self.view;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2.0];
    [UIView setAnimationBeginsFromCurrentState:NO];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.flipView cache:YES];

    UIView *parent = self.flipView.superview;
    [self.flipView removeFromSuperview];

    [parent addSubview:lessInfo];

    [UIView commitAnimations];

}

【问题讨论】:

    标签: iphone flip


    【解决方案1】:

    这可能是因为您没有使用容器视图作为过渡视图。参考setAnimationTransition:forView:cache:上的文档

    如果您想在过渡期间更改视图的外观(例如,从一个视图翻转到另一个视图),请使用容器视图,即 UIView 的实例,如下所示:

    1. 开始一个动画块。
    2. 在容器视图上设置过渡。
    3. 从容器视图中移除子视图。
    4. 将新的子视图添加到容器视图中。
    5. 提交动画块。

    尝试在showMoreInfo:的动画过渡视图中使用self.view.superview

    showLessInfo: 方法起作用的原因是您使用的是容器视图。

    【讨论】:

    • 亲爱的 Jason,当我执行 self.view.superview 时,它在 showMoreInfo: 中翻转得很好,但现在翻转回来(使用 showLessInfo:) 行为异常:它只显示没有动画的初始视图。将 self.view.superview 放入此方法没有帮助。我敢肯定,我只是很愚蠢,但只要再给我一行代码,我将不胜感激。
    • 对不起,我太笨了。我把 self.flipView.superview 和现在一切都很好!
    【解决方案2】:

    你可以将你的 MainWindow (UIWindow) 用作 UIView 继承的 UIWindow 的容器视图吗?

    iPhone 3.0 也通过 presentModalViewController 方法引入了翻转事务:

    CustomViewController *vc = [[CustomViewController alloc]
        initWithNibName:@"CustomViewController" bundle:nil];
    
    vc.delegate = self;
    
    // The magic statement. This will flip from right to left.
    // present the modal view controller then when you dismissModalViewController
    // it will transition flip from left to right. Simple and elegant.
    vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    
    [self presentModalViewController:vc animated:YES];
    
    [vc release];
    

    【讨论】:

    • 用这个方法移除视图控制器使用:dismissModalViewControllerAnimated
    【解决方案3】:

    在 iOS 4.0 之后,您可以通过以下方式在视图之间切换:

    [UIView transitionFromView:sourceView toView:destinationView duration:0.35f options:UIViewAnimationOptionTransitionFlipFromRight completion:^(BOOL finished) {
        NSLog(@"I just flipped!");
    }];
    

    正如 Jason 所说,您需要在容器视图中执行此操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-28
      • 2014-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多