使用UIViewAnimationTransitionFlipFromRight翻转时使UIView变暗

时间:2010-04-15 11:28:42

标签: iphone objective-c core-animation

我正在使用标准动画块从一个视图翻转到另一个视图,如下所示:

[UIView beginAnimations:@"FlipAnimation" context:self];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:NO];
[UIView setAnimationBeginsFromCurrentState:NO];
[containerView exchangeSubviewAtIndex:1 withSubviewAtIndex:0];
[UIView commitAnimations];

在动画过程中,“从”视图开始翻转时会变暗。由于我在两侧都使用几乎相同的视图而不覆盖整个视图(这意味着代表翻转的实体卡),这看起来非常可怕。使用[UIColor clearColor]作为每个关联backgroundColor的{​​{1}}属性并没有帮助,因为透明区域似乎也变暗了。

关于如何摆脱这种变暗效果的任何想法?

1 个答案:

答案 0 :(得分:6)

似乎你必须使用Core Animation变换“手动”动画。 我将动画分为两部分。首先,我将'viewOne'旋转到动画的一半,将'viewTwo'旋转到另一个方向,没有动画。 当动画的前半部分完成后,我将在委托方法中完成剩下的工作。 您的参数可能会有所不同:)

Skewing是我发现的其他一些StackOverflow答案的礼貌。

- (IBAction)flip:(id)sender
{
  UIView* viewOne = [self.view.subviews objectAtIndex:0];
  UIView* viewTwo = [self.view.subviews objectAtIndex:1];

  viewOne.hidden = YES;

  CATransform3D matrix = CATransform3DMakeRotation (M_PI / 2, 0.0, 1.0, 0.0);
  CATransform3D matrix2 = CATransform3DMakeRotation (-M_PI / 2 , 0.0, 1.0, 0.0);
  matrix = CATransform3DScale (matrix, 1.0, 0.975, 1.0);
  matrix.m34 = 1.0 / -500;

  matrix2 = CATransform3DScale (matrix2, 1.0, 0.975, 1.0);
  matrix2.m34 = 1.0 / -500;

  viewOne.layer.transform = matrix2;

  [UIView beginAnimations:@"FlipAnimation1" context:self];
  [UIView setAnimationDuration:1];
  [UIView setAnimationDelegate:self];
  [UIView setAnimationDidStopSelector:@selector(animationPartOneDone)];
  [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];

  viewTwo.layer.transform = matrix;

  [UIView commitAnimations];    
}

-(void)animationPartOneDone
{   
  UIView* viewOne = [self.view.subviews objectAtIndex:0];
  UIView* viewTwo = [self.view.subviews objectAtIndex:1];


  viewOne.hidden = NO;
  viewTwo.hidden = YES;

  CATransform3D matrix = CATransform3DMakeRotation (2 * M_PI, 0.0, 1.0, 0.0);

  matrix = CATransform3DScale (matrix, 1.0, 1.0, 1.0);

  [UIView beginAnimations:@"FlipAnimation2" context:self];
  [UIView setAnimationDuration:1];
  [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

  viewOne.layer.transform = matrix;

  [UIView commitAnimations];    

  [self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];

}