0
我試圖從一個視圖轉換到另一個視圖。我知道這裏有「[UIView transitionWithView ...」,但是我會將更復雜的東西添加到動畫層,並且需要自己編寫代碼。核心動畫:翻轉視圖
下面的代碼工作,但有三個問題:
當背面視圖顯示,這是翻轉水平。
動畫完成後,我添加背面視圖並刪除正面。在動畫過程中,由於透視圖(m34),兩個視圖都變得更大一些,所以當completionBlock將新視圖添加到屏幕上時,會有一個難看的回彈到原始大小。
我需要延遲CATransaction的一個塊(runBlockAfterDelay)。如果我沒有該塊,動畫將無法運行。
在此先感謝
對於額外的信用:),是否有可能在一類方法中的代碼?例如+(void)flippFromView ...
- (void)flipFromView:(UIView *)fromView toView:(UIView *)toView spaceBetweenLayers: (CGFloat)spaceBetweenLayers duration:(CGFloat)duration delay:(CGFloat)delay (void))block
{
// Move flipp view back, behind front view
toView.layer.transform = CATransform3DMakeTranslation(0.0, 0.0, -spaceBetweenLayers);;
// Layer that will handle the animation
CALayer *rootLayer = [CALayer layer];
rootLayer.frame = self.frame;
rootLayer.position = fromView.layer.position;
[self.layer addSublayer:rootLayer];
//Transform Layer
CATransformLayer *transformLayer = [CATransformLayer layer];
[transformLayer addSublayer:fromView.layer];
[transformLayer addSublayer:toView.layer];
[rootLayer addSublayer:transformLayer];
[self runBlockAfterDelay:delay block:^{
[CATransaction begin];
[CATransaction setValue:@(duration) forKey:kCATransactionAnimationDuration];
// Completion block
[CATransaction setCompletionBlock:^{
// App crashes with EXC_BAD_ACCESS if we don't move the layer back to the super layer before removing the rootLayer
[self.layer addSublayer:fromView.layer];
[self.layer addSublayer:toView.layer];
// Add new, and remove old layer
[self addSubview:toView];
[fromView removeFromSuperview];
// Set Z position
fromView.layer.zPosition = -10;
toView.layer.zPosition = 10;
// Clean up
[rootLayer removeFromSuperlayer];
}];
// Flipp
CATransform3D flippTransform = CATransform3DMakeRotation(DegreesToRadians(180), 0, 1, 0);
flippTransform.m34 = 1.0f/500.0f;
rootLayer.sublayerTransform = flippTransform;
[CATransaction commit];
}];
}
如果這對你有幫助,你應該接受答案嗎? – Paul