【问题标题】:Custom Segue doesn't animate自定义 Segue 没有动画
【发布时间】:2015-01-08 10:50:19
【问题描述】:

我已经设置了一个自定义对话框,当容器视图控制器中的按钮被按下时,通过自定义 segue 显示,执行函数被调用,除了动画之外,一切看起来都很好。

这是故事板上的转场:

这里是按钮:

这是转场的代码:

#import "TimerDialogSegue.h"

@implementation TimerDialogSegue

-(void)perform
{
    UIViewController *dst = [self destinationViewController];
    //Doesn't appear work with the Timer View Controller as the source 
    //  so I use the parent Home View Controller
    UIViewController *src = [self sourceViewController];
    src = src.parentViewController;

    // set the view frame
    CGRect frame;
    frame.size.height = src.view.frame.size.height;
    frame.size.width = src.view.frame.size.width;
    frame.origin.x = src.view.bounds.origin.x;
    frame.origin.y = src.view.bounds.origin.y;
    dst.view.frame = frame;

    // add the view to the hierarchy and bring to front
    [src addChildViewController:dst];
    [src.view addSubview:dst.view];
    [src.view bringSubviewToFront:dst.view];

    [UIView animateWithDuration:0.3f
        animations:^
        {
            dst.view.alpha = 1.0f;
        }];
}

@end

这是 Dialog 中关闭它的代码,这部分动画效果很好

- (IBAction)cancelButtonPressed:(id)sender
{
    [self dismiss:sender];
}

- (IBAction)dismiss:(id)sender
{
    [UIView animateWithDuration:0.3f
        animations:^
        {
            self.view.alpha = 0.0f;
        }
        completion:^(BOOL finished)
        {
            [self.view removeFromSuperview];
            [self removeFromParentViewController];
        }];
}

总结对话框出现并正确运行,唯一的问题是它出现时没有动画。当它消失时它会动画。任何人都知道我可以如何解决此问题或在此对话框出现时为其设置动画?

提前感谢您的帮助。

【问题讨论】:

  • 顺便说一句,我会质疑你在这里做什么。你想做什么?这似乎是对嵌入式/子视图控制器的错误使用。在我看来,它更像是一个普通的呈现视图控制器,甚至只是一个出现的视图。你可以让你的生活更简单,更好地控制动画。如果您描述您想要达到的整体效果,我可能会向您展示更好的方法......!
  • 计时器视图由顶部的时钟按钮切换,因此我使用容器将布局更改隔离到单个帧,并将计时器功能与其余代码分开。
  • 很酷(对封装有意义),但是为什么它不能是呈现的视图控制器而不是嵌入式视图控制器呢?
  • 我向您推送此功能的原因是自定义呈现的视图控制器动画既简单又正常,而对初始嵌入进行动画处理的需要迫使您走这条自定义的 segue 道路,这似乎是不必要的.
  • 不,在 iOS 7/8 中,呈现的视图控制器可以只覆盖界面的一部分。但是,如果您希望用户能够在计时器存在时与界面的其余部分进行交互,那么您可能做对了。

标签: ios objective-c segue uicontainerview


【解决方案1】:

你是说:

[UIView animateWithDuration:0.3f
    animations:^
    {
        dst.view.alpha = 1.0f;
    }];

但是dst.view.alpha1 已经所以什么都没有发生。您只能为更改设置动画。

例如,当您关闭时,您会为以下内容设置动画:

  self.view.alpha = 0.0f;

这很有效,因为 self.view.alpha1,所以实际上对动画进行了更改。

【讨论】:

  • 所以如果我在调用之前将 dst.view.alpha 设置为 0.0f,它应该可以工作吗?
  • 试试看 - 无需推测什么“应该起作用”。我什么都不保证! :) 我只是告诉你代码中明显有缺陷的地方,这可能是导致这种现象的原因。
  • 很酷——感谢接受——但让我们继续讨论这个架构(如果你喜欢的话)。
猜你喜欢
  • 1970-01-01
  • 2012-06-02
  • 1970-01-01
  • 2013-10-15
  • 1970-01-01
  • 1970-01-01
  • 2015-09-30
  • 2019-12-29
相关资源
最近更新 更多