你如何翻转非全屏UIView的超级查看与圆angular?

我翻转UIViews类似的天气应用程序的页面翻转。 虽然视图不是全屏的,而且超视图有圆angular。 问题是,在翻转animation中,超视图的圆angular用黑色填充到方angular。

以下是我如何设置angular落:

self.view.layer.cornerRadius = 15.0f; self.view.clipsToBounds = YES; 

以下是我翻转视图( frontViewbackView都保留):

 UIView *toView; UIView *fromView; UIViewAnimationOptions animationType; if (toFront) { toView = self.frontView; fromView = self.backView; animationType = UIViewAnimationOptionTransitionFlipFromLeft; } else { toView = self.backView; fromView = self.frontView; animationType = UIViewAnimationOptionTransitionFlipFromRight; } [UIView transitionFromView:self.fromView toView:self.toView duration:1 options:animationType completion:nil]; 

当我这样做的时候, self.view的圆angular用黑色填充到矩形边angular。 这可以避免吗? 我不认为我对Core Animation有足够的了解来解决这个问题。

如果你需要在核心animation中做到这一点,你可以通过使animation独立于replace视图来实现。 这不是不可能的困难。 如何做到这一点和一些示例代码的说明如下:

replace视图

replace视图可以使用例如:

 [UIView transitionFromView:self.fromView toView:self.toView duration:0.0 options:UIViewAnimationOptionTransitionNone completion:nil]; 

animation翻转

困难的部分显然是animation(如果你以前从未做过的话,把它们放在一起)。

只显示两个视图的前面

既然你想把这两个视图翻转,就好像它们是一张纸的两面,你不希望它们中的任何一个在它们面对的时候出现。 这是通过在两个图层doubleSided属性设置为NO来完成的。 (不是双面意味着是单面的)。

翻转

您想要将前视图的变换从一个标识变换(无变换)变换为围绕y轴的180度旋转变换(朝后),以便从左到右翻转。

与此同时,您想要将背视图 -180度旋转变换为恒等变换。 这将使翻转。 这两个animation都可以做成“基本”animation:

 CABasicAnimation *flipAnimation = [CABasicAnimation animationWithKeyPath:@"transform"]; [flipAnimation setFromValue:[NSValue valueWithCATransform3D:CATransform3DIdentity]]; // No rotation [flipAnimation setToValue:[NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0,1,0)]]; // Rotate PI around Y [flipAnimation setFillMode:kCAFillModeBoth]; [flipAnimation setRemoveOnCompletion:NO]; [flippingView layer] addAnimation:flipAnimation forKey:@"myFlipAnimation"]; 

然后你为另一个视图做同样的animation。 (填充模式可防止animation在完成后跳回原始状态)

把它放在一起

既然您希望两个animation同时运行,并希望在animation完成时运行视图replace代码,则可以使用animation事务。 这也可以让你configuration两个animation看起来在一个地方:

 [CATransaction begin]; [CATransaction setAnimationDuration:2.0]; // 2 seconds [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; [CATransaction setCompletionBlock:^{ // Your view replacing code here... }]; // Add the animations to both views here... [CATransaction commit]; 

这是我写的。 似乎工作正常,但是,当然,自己testing一下。

注意事项:

  • 调用此方法之前,您应该添加您正在animation的视图。

  • 该方法不会删除任何视图,因此如果需要,可以使用完成块来删除它们。

  • 该方法也不会修改视图的“图层模型”值,因此它们将保持与animation之后的一致。

`

 + (void)flipRight:(BOOL)flipRight fromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration completion:(void (^)(void))completionBlock { // Save original values BOOL fromViewDoubleSided = fromView.layer.doubleSided; BOOL toViewDoubleSided = toView.layer.doubleSided; BOOL fromViewUserInteractionEnabled = fromView.userInteractionEnabled; BOOL toViewUserInteractionEnabled = toView.userInteractionEnabled; // We don't want to see the other side of the layer fromView.layer.doubleSided = NO; toView.layer.doubleSided = NO; // We don't want user to fire off animation while its already going fromView.userInteractionEnabled = NO; toView.userInteractionEnabled = NO; [CATransaction begin]; [CATransaction setCompletionBlock:^{ // Restore original values fromView.layer.doubleSided = fromViewDoubleSided; toView.layer.doubleSided = toViewDoubleSided; fromView.userInteractionEnabled = fromViewUserInteractionEnabled; toView.userInteractionEnabled = toViewUserInteractionEnabled; if (completionBlock) completionBlock(); }]; CATransform3D flipOutTransform3D = CATransform3DMakeRotation(M_PI, 0,1,0); flipOutTransform3D.m34 = 1.0/(300.0*(flipRight ? -1 : 1)); CABasicAnimation *flipOutAnimation = [CABasicAnimation animationWithKeyPath:@"transform"]; flipOutAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity]; flipOutAnimation.toValue = [NSValue valueWithCATransform3D:flipOutTransform3D]; flipOutAnimation.duration = duration; CATransform3D flipInTransform3D = CATransform3DMakeRotation(M_PI, 0,1,0); flipInTransform3D.m34 = 1.0/(300.0*(flipRight ? 1 : -1)); CABasicAnimation *flipInAnimation = [CABasicAnimation animationWithKeyPath:@"transform"]; flipInAnimation.fromValue = [NSValue valueWithCATransform3D:flipInTransform3D]; flipInAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DIdentity]; flipInAnimation.duration = duration; [fromView.layer addAnimation:flipOutAnimation forKey:@"flipOutAnimation"]; [toView.layer addAnimation:flipInAnimation forKey:@"flipInAnimation"]; [CATransaction commit]; };