How to make UIView in 2 parts

I am trying to develop a transition effect when one view is divided into 2 parts, and the upper part is animated up and the lower part is animated down to look into it. I use UIView and UIImageView to achieve this:

// 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]; 

However, this code is very slow on the device, and I'm sure there is a more efficient way to implement such a transition. Most likely using CALayers, etc. Can you point me in the right direction?

+4
source share
2 answers

This is not an animation. Your drawing code is slow. If you are a profile, you will see that renderInContext: (by the way, is always executed in the main thread) and CGImageCreateWithImageInRect are limiting factors. What is your look (the one you want to break)? Is it possible to create two views instead of one of them?

+2
source

The animation itself should not be slow. All you really need to do is change the Y-position of the two kinds of animation.

Why don't you try static images in UIImageViews and see how the animation works?

If lag occurs when creating images, you might want to consider moving this code to a separate thread so that the main thread does not freeze. When images are created, notify the main thread, put them in UIImageViews and do the animation.

+2
source

Source: https://habr.com/ru/post/1385418/


All Articles