如何将 UIView 渲染成两部分
我正在尝试开发过渡效果,当一个视图分成两部分时,上部动画向上,下部动画向下,以陶醉其后面的视图。 我正在使用 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];
但是,此代码在设备上速度非常慢,我确信有一种更有效的方法来实现这种转换。最有可能使用 CALayers 等。 你能指出我正确的方向吗?
I am trying to develop transition effect, when one view splits into 2 parts with upper part animated upwards and lower part animated downwards to revel the view behind it.
I am using UIView and UIImageView approach to accomplish that:
// 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];
This code however, is extremely slow on the device and I am sure there is a more efficient way to implement such a transition. Most probably using CALayers, etc..
Can you point me to the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是动画。你的绘图代码很慢。如果您进行分析,您会发现
renderInContext:
(顺便说一句,始终在主线程上执行)和CGImageCreateWithImageInRect
是限制因素。你的观点(你想要分裂的观点)是关于什么的?是否可以选择从一开始就创建两个视图而不是一个视图?This is not animation. Your drawing code is slow. If you profile, you will see that
renderInContext:
(btw. always executed on the main thread) andCGImageCreateWithImageInRect
are the limiting factors. What your view (the one you want to split) is about? Is it an option to create two views instead of one from start?动画本身不应该很慢。您真正需要做的就是更改动画的两个视图的 Y 位置。
为什么不尝试将静态图像放入 UIImageViews 中并查看动画的执行情况?
如果在创建图像时发生滞后,也许您应该考虑将该代码移至单独的线程,以便主线程不会冻结。当图像创建完成后,通知主线程,将它们放入 UIImageViews 中,并执行动画。
The animation itself shouldn't be slow. All you really need to be doing is changing the Y position of the two views for the animation.
Why don't you try putting static images into the UIImageViews and seeing how the animation performs then?
If the lag is happening when you are creating the images, maybe you should consider moving that code to a separate thread so that the main thread doesn't freeze up. When the images are done being created, notify the main thread, put them in the UIImageViews, and perform the animation.