如何通过缩放显示UIViewController? [英] How do I present a UIViewController by zooming?

查看:259
本文介绍了如何通过缩放显示UIViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的iPad应用程序中,我有一个带有小表格视图的视图控制器.当您点击表格视图时,它会打开一个模式视图控制器,该控制器是小型表格视图的更大,更精致的版本.我想从大视图控制器的预渲染图像创建动画,方法是将图像缩小到小表格视图的大小并将其缩放到全屏大小,然后将图像替换为真实"视图控制器.

In my iPad app I have a view controller with a small table view. When you tap on the table view it opens a modal view controller that is a larger and more refined version of the small table view. I would like to create an animation from a pre-rendered image of the large view controller by scaling the image down to be the size of the small table view and zoom it to full screen size and then replace the image with the "real" view controller.

类似的东西:

LargeViewController* lvc = [[LargeViewController alloc] init];
[self presentModalViewController:lvc byZoomingFromRect:CGRectMake(50,50,200,300)];

我知道您可以从视图生成图像:

I know you can produce an image from a view:

- (UIImage *) imageWithView:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, [[UIScreen mainScreen] scale]); 
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

但是如何使视图控制器绘制自身(屏幕外),以便可以查看视图并缩放动画中的图像以填充屏幕?

But how do I make the view controller draw itself (offscreen) so I can take it's view and scale the image in an animation to fill screen?

先谢谢了.

推荐答案

我想您想创建自己的动画.上个月,我玩了类似的游戏.我的解决方案是将自定义视图(可能从视图控制器中获取)作为叠加层添加到当前视图.这也适用于图层.

I suppose you want to create your very own animation. Last month I played around with something like that. My solution was adding a custom view (maybe taken from a view controller) to the current view as an overlay. This works with layers, too.

首先,您需要从未来"或当前"视图控制器中获取图像,就像在上面的代码示例中所做的一样.通常,在呈现到上下文时,视图控制器的内容应该可用.

First you fetch the Image from your "future" or "present" view controller, like you did in your code example above. Normally the view controllers content should be available while rendering to the context.

现在您有了图像.图像的操作必须由您完成.

Now you have the image. The manipulation of the image must be done by you.

将图像添加到UIImageView.该ImageView可以添加为子视图或图层.现在,您可以在其中的图层上自由绘制实际的用户界面.有时,您必须四处移动图层或视图,以使其完全覆盖您的视图.这取决于您的视图设置.如果要处理Tableview,则添加子视图并不是那么容易.因此,最好使用该层.

Add the image to a UIImageView. This ImageView can be added as subview or layer. Now you have a layer where you can freely draw above your actual user interface. Sometimes you have to move the layer or view around, so that it perfectly overlays your view. This depends on your view setup. If you are dealing with Tableviews, adding a subview is not that easy. So better use the layer.

完成所有工作后,展示没有动画的新视图控制器,使其立即显示.

After all the work was done, present the new view controller without animation, so that it appears immediately.

工作完成后,从父视图中删除图层或视图,然后进行清理.

Remove the layer or view from your parent view after the work was done, and clean up.

这听起来很复杂,但是一旦完成,您就可以拥有一个模板.在"WWDC 2011,会议309介绍Interface Builder故事板"中,苹果介绍了自定义脚本",您可以在其中找到确切的机制.下面的代码是一个较旧的项目的一部分,有些混乱,必须将其清除.但是,为了显示该原理,该方法应该起作用:

This sounds complicated, but once you've done that you have a template for that. In "WWDC 2011, Session 309 Introducing Interface Builder Storyboarding" apple introduced 'custom segues', where you'll find a mechanism for exactly what you want to do. The code below is a cut out of an older project and is somehow messy and must be cleaned up. But for showing the principle this should work:

-(void) animate {

      static LargeViewController* lvc = [[LargeViewController alloc] init];

      UIGraphicsBeginImageContextWithOptions(self.bounds.size, view.opaque, [[UIScreen mainScreen] scale]); 
     [lvc.view.layer renderInContext:UIGraphicsGetCurrentContext()];

      // Create a ImageView to display your "zoomed" image
      static UIImageView* displayView = [[UIImageView alloc] initWithFrame:self.view.frame];
      static UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();

      // Add your image to the view
      displayView.image = img;

      // insert the view above your actual view, adjust coordinates in the
      // frame property of displayView if overlay is misaligned
      [[self.view] addSubview:displayView];

      // alternatively you can use the layer
      // [self.view.layer addSublayer:displayView.layer];

      // draw the imageView
      [displayView setNeedsDisplay];

      // do something in background. You may create your own
      // construction, i.e. using a timer
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

          NSDate *now = [NSDate date];
          NSTimeInterval animationDuration = 3.;
          NSTimeInterval t = -[now timeIntervalSinceNow];
          while (t < animationDuration) {

              t = -[now timeIntervalSinceNow];

              // Do some animation here, by manipulation the image
              // or the displayView

              // <calculate animation>, do something with img
              // you have exact timing information in t,
              // so you can set the scalefactor derived from t
              // You must not use an UIImage view. You can create your own view
              // and do sth. in draw rect. Do whatever you want,
              // the results will appear
              // in the view if you added a subview
              // or in a layer if you are using the layer

              dispatch_sync(dispatch_get_main_queue(), ^{

                  // display the result
                  displayView.image = img;
                  [displayView setNeedsDisplay];
              });
          }
       });

      // now the animation is done, present the real view controller
      [self presentModalViewController:lvc animated:NO];

      // and clean up here
}

这篇关于如何通过缩放显示UIViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆