草庐IT

iOS 7 - 带有 UIViewcontrollerAnimatedTransitioning 的模态弹出窗口

coder 2024-01-19 原文

我正在尝试使用 UIViewcontrollerAnimatedTransitioning 为 PopUp ViewController 呈现动画。我已经创建了一个从 TableViewCell 到我的 Viewcontroller 的 Modal Segue

在 PopupPresentAnimationController(实现 UIViewcontrollerAnimatedTransitioning)中我有

-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

    [fromViewController addChildViewController:toViewController];
    toViewController.view.frame = fromViewController.view.frame;
    [fromViewController.view addSubview:toViewController.view];

    [toViewController didMoveToParentViewController:fromViewController];

    NSTimeInterval duration = [self transitionDuration:transitionContext];

    [UIView animateWithDuration:duration delay:0.0 usingSpringWithDamping:0.6 initialSpringVelocity:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
        fromViewController.view.alpha = 0.5;
    } completion:^(BOOL finished) {
        fromViewController.view.alpha = 1.0;
        [transitionContext completeTransition:YES];
    }];
}

PopUpViewController 的背景为黑色,不透明度为 50%,当它看起来所有“有效”但在动画结束后,屏幕变为黑色。

更新 1:

UIViewController *fromViewController =[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *container = [transitionContext containerView];

    CGRect initialFrame = toViewController.view.frame;
    initialFrame.origin.y = toViewController.view.frame.size.height;

    toViewController.view.frame = initialFrame;
    [container insertSubview:toViewController.view aboveSubview:fromViewController.view];

    NSTimeInterval duration = [self transitionDuration:transitionContext];

    [UIView animateWithDuration:duration delay:0 options:0 animations:^{
        CGRect newFrame = toViewController.view.frame;
        newFrame.origin.y = 0;
        toViewController.view.frame = newFrame;
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];

更新 2 在 prepareForSegue 中,我添加了以下行

controller.modalPresentationStyle = UIModalPresentationCustom; // controller is the destination

有了这条线,黑色就不再是黑色了!

最佳答案

您遇到的问题是您将toViewController 添加到fromViewController。当动画结束时,toViewController 被移除,您的 fromViewController 也被移除。处理这个问题的正确方法是使用上下文提供的容器 View :

UIView *container = [transitionContext containerView];

这是一个如何进行弹出转换的示例:

-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
  UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
  UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
  UIView *container = [transitionContext containerView];

  if (!self.beingDismissed) {
    //Make controller hidden so it can slide in
    CGRect initialFrame = toViewController.view.frame;
    initialFrame.origin.y = toViewController.view.frame.size.height;

    toViewController.view.frame = initialFrame;
    [container insertSubview:toViewController.view aboveSubview:fromViewController.view];
  }
  [UIView animateKeyframesWithDuration:0.5 delay:0 options:0 animations:^{
    if (!self.beingDismissed) {
        //Show view controller
        CGRect newFrame = toViewController.view.frame;
        newFrame.origin.y = 0;
        toViewController.view.frame = newFrame;
    } else {
        //Hide view controller
        CGRect newFrame = fromViewController.view.frame;
        newFrame.origin.y = fromViewController.view.frame.size.height;
        fromViewController.view.frame = newFrame;
    }
  } completion:^(BOOL finished) {
    [transitionContext completeTransition:finished];
  }];
}

关于iOS 7 - 带有 UIViewcontrollerAnimatedTransitioning 的模态弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24990949/

有关iOS 7 - 带有 UIViewcontrollerAnimatedTransitioning 的模态弹出窗口的更多相关文章

  1. ruby - 多次弹出/移动 ruby​​ 数组 - 2

    我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby​​数组,我们在StackOverflow上找到一

  2. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  3. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  4. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  5. ruby-on-rails - 带有 Zeus 的 RSpec 3.1,我应该在 spec_helper 中要求 'rspec/rails' 吗? - 2

    使用rspec-rails3.0+,测试设置分为spec_helper和rails_helper我注意到生成的spec_helper不需要'rspec/rails'。这会导致zeus崩溃:spec_helper.rb:5:in`':undefinedmethod`configure'forRSpec:Module(NoMethodError)对thisissue最常见的回应是需要'rspec/rails'。但这是否会破坏仅使用spec_helper拆分rails规范和PORO规范的全部目的?或者这无关紧要,因为Zeus无论如何都会预加载Rails?我应该在我的spec_helper中做

  6. ruby - (Ruby || Python) 窗口管理器 - 2

    我想用这两种语言中的任何一种(最好是ruby​​)制作一个窗口管理器。老实说,除了我需要加载某种X模块外,我不知道从哪里开始。因此,如果有人有线索,如果您能指出正确的方向,那就太好了。谢谢 最佳答案 XCB,X的下一代API使用XML格式定义X协议(protocol),并使用脚本生成特定语言绑定(bind)。它在概念上与SWIG类似,只是它描述的不是CAPI,而是X协议(protocol)。目前,C和Python存在绑定(bind)。理论上,Ruby端口只是编写一个从XML协议(protocol)定义语言到Ruby的翻译器的问题。生

  7. Ruby:如何使用带有散列的 'send' 方法调用方法? - 2

    假设我有一个类A,里面有一些方法。假设stringmethodName是这些方法之一,我已经知道我想给它什么参数。它们在散列中{'param1'=>value1,'param2'=>value2}所以我有:params={'param1'=>value1,'param2'=>value2}a=A.new()a.send(methodName,value1,value2)#callmethodnamewithbothparams我希望能够通过传递我的哈希以某种方式调用该方法。这可能吗? 最佳答案 确保methodName是一个符号,而

  8. ruby-on-rails - 带有 Pry 的 Rails 控制台 - 2

    当我进入Rails控制台时,我已将pry设置为加载代替irb。我找不到该页面或不记得如何将其恢复为默认行为,因为它似乎干扰了我的Rubymine调试器。有什么建议吗? 最佳答案 我刚发现问题,pry-railsgem。忘记了它的目的是让“railsconsole”打开pry。 关于ruby-on-rails-带有Pry的Rails控制台,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/question

  9. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  10. 带有 attr_accessor 的类上的 Ruby instance_eval - 2

    我了解instance_eval和class_eval之间的基本区别。我在玩弄时发现的是一些涉及attr_accessor的奇怪东西。这是一个例子:A=Class.newA.class_eval{attr_accessor:x}a=A.newa.x="x"a.x=>"x"#...expectedA.instance_eval{attr_accessor:y}A.y="y"=>NoMethodError:undefinedmethod`y='forA:Classa.y="y"=>"y"#WHATTT?这是怎么回事:instance_eval没有访问我们的A类(对象)然后它实际上将它添加到

随机推荐