【发布时间】:2014-03-05 09:07:31
【问题描述】:
我正在尝试从一种视图转换到另一种视图。我知道有“[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];
}];
}
【问题讨论】:
-
如果这对您有帮助,您应该接受答案吗?
标签: ios objective-c xcode core-animation