2010-08-04 81 views
6

因此,我目前正在测试UIView动画,并且我注意到CALayer呈现的阴影(使用[view.layer setShadowStuffHere])在动画开始时消失并重新出现在动画结束处。有没有什么办法可以保持这些阴影,并使阴影与UIView一起动画?我尝试使用没有边框路径的阴影,但这是一个糟糕的主意,因为在动画过程中,帧速率像石头一样下降,反正我也没有得到阴影。CALayer Shadows在UIView动画中消失

我现在使用的代码如下。最初你会看到一个红色的视图,当点击时,它会翻转成一个更大的蓝色视图。最终结果应该与iPad音乐应用程序类似;当选择一张专辑时,它会翻转显示背面的视图。

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    UITapGestureRecognizer * tapRec; 

    // Drop shadow Path creation 
    CGFloat x1 = 0; 
    CGFloat y1 = 0; 
    CGFloat x2 = 100; 
    CGFloat y2 = 100; 

    frontBorderPath = CGPathCreateMutable(); 
    CGPathMoveToPoint(frontBorderPath, NULL, x1, y1); 
    CGPathAddLineToPoint(frontBorderPath, NULL, x2, y1); 
    CGPathAddLineToPoint(frontBorderPath, NULL, x2, y2); 
    CGPathAddLineToPoint(frontBorderPath, NULL, x1, y2); 
    CGPathAddLineToPoint(frontBorderPath, NULL, x1, y1); 

    x2 = 400; 
    y2 = 400; 
    backBorderPath = CGPathCreateMutable(); 
    CGPathMoveToPoint(backBorderPath, NULL, x1, y1); 
    CGPathAddLineToPoint(backBorderPath, NULL, x2, y1); 
    CGPathAddLineToPoint(backBorderPath, NULL, x2, y2); 
    CGPathAddLineToPoint(backBorderPath, NULL, x1, y2); 
    CGPathAddLineToPoint(backBorderPath, NULL, x1, y1); 

    containerView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; 
    containerView.clipsToBounds = NO; 
    [containerView.layer setShadowPath:frontBorderPath]; 
    [containerView.layer setShadowOpacity:1]; 
    [containerView.layer setShadowOffset:CGSizeMake(0,0)]; 
    [containerView.layer setShadowRadius:3]; 
    [containerView.layer setShouldRasterize:NO]; 
    [self.view addSubview:containerView]; 

    tapRec = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(frontTap)] autorelease]; 
    frontView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
    frontView.backgroundColor = [UIColor redColor]; 
    [frontView addGestureRecognizer:tapRec]; 
    [containerView addSubview:frontView]; 

    tapRec = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backTap)] autorelease]; 
    backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 400, 400)]; 
    backView.backgroundColor = [UIColor blueColor]; 
    [backView addGestureRecognizer:tapRec];  
} 

- (void)frontTap{ 
    NSLog(@"fronttap"); 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.7]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:containerView cache:YES]; 

    [frontView removeFromSuperview]; 
    [containerView addSubview:backView]; 
    containerView.frame = CGRectMake(100, 100, 400, 400); 
    [containerView.layer setShadowPath:backBorderPath]; 

    [UIView commitAnimations]; 

} 

- (void)backTap{ 
    NSLog(@"backtap"); 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.7]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:containerView cache:YES]; 

    [backView removeFromSuperview]; 
    [containerView addSubview:frontView]; 
    containerView.frame = CGRectMake(100, 100, 100, 100); 
    [containerView.layer setShadowPath:frontBorderPath]; 

    [UIView commitAnimations]; 

} 

回答

12

事实证明,当你做一个UIView动画,它忽略了你的观点的clipsToBounds财产和剪辑它反正(至少在翻盖型动画),所以如果你想你的阴影始终可见,你需要有一个足够大的包含所有东西的视图框架。

+0

请您详细说明一下吗? – sole007 2015-05-12 08:11:30

+0

@ sole007:这个特殊的答案可能太老了,不适用于今天(我自此以后没有任何最近的解决方案),但是在需要的时候,问题是即使设置了clipsToBounds为false,并且您使用CALayers在视图的特定边界之外渲染阴影,依靠clipsToBounds对于显示的阴影为false,则无关紧要,并且这些阴影不会显示出来。那么解决方案是让视图边界包含阴影而不是边界之外的阴影。 – 2015-09-16 23:57:41