如何渲染UIView成2部分



我正在尝试开发过渡效果,当一个视图分成2部分时,上部向上动画,下部向下动画,以揭示其背后的视图。我正在使用UIView和UIImageView方法来完成它:

// 1. Make a screenshot:
    UIGraphicsBeginImageContext(parentView.frame.size);
    [parentView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    // 2. Calculate rectangles for top and bottom part:
    CGRect rectTop, rectBottom;
    CGFloat W = rectBig.size.width;
    CGFloat H = rectBig.size.height;

    rectTop = CGRectMake(0, 0, W, y_cutoff);
    rectBottom = CGRectMake(0, y_cutoff, W, H - y_cutoff);
    // 3. Create top and bottom images:
    CGImageRef imageRefTop      = CGImageCreateWithImageInRect([screenshot CGImage], rectTop);
    CGImageRef imageRefBottom   = CGImageCreateWithImageInRect([screenshot CGImage], rectBottom);

    UIImage *imageTop = [UIImage imageWithCGImage:imageRefTop];
    UIImage *imageBottom = [UIImage imageWithCGImage:imageRefBottom];

// 4. Assign images to image views:
imageViewTop.image = imageTop;
imageViewBottom.image = imageBottom;
// 5. Animate image views:
[UIView beginAnimation:nil context:NULL];
.... animation code here
[UIView commitAnimations];
然而,这段代码在设备上非常慢,我相信有更有效的方法来实现这样的转换。最有可能使用calayer等。你能告诉我正确的方向吗?

这不是动画。你的绘图代码很慢。如果您进行概要分析,您将看到renderInContext:(顺便说一下)。总是在主线程上执行)和CGImageCreateWithImageInRect是限制因素。你的观点(你想要分裂的观点)是关于什么的?是否可以从一开始就创建两个视图而不是一个视图?

动画本身不应该很慢。你真正需要做的就是改变动画的两个视图的Y轴位置。

你为什么不试着把静态图像放入UIImageViews,看看动画是如何执行的呢?

如果延迟发生在您创建图像时,也许您应该考虑将该代码移动到一个单独的线程,以便主线程不会冻结。当图像创建完成后,通知主线程,将它们放入UIImageViews中,并执行动画

最新更新