我試圖開發過渡效果,當一個視圖分成兩部分,上部分向上動畫,下部分向下動畫以欣賞背後的視圖。 我使用的UIView和UIImageView的方式來實現這一目標:如何將UIView渲染爲兩部分
// 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等。 你能指點我正確的方向嗎?
「極其緩慢」是什麼意思?儀器在哪裏說它正在花費時間? –
單擊前視圖時會觸發轉場。點擊後有明顯的延遲,動畫本身是生澀的(在iPhone 4上測試)。 – Lukasz