在UIView动画期间,CALayer Shadows消失

Dav*_*Liu 6 iphone cocoa-touch calayer uikit ipad

所以我正在测试UIView动画,我注意到CALayer渲染的阴影(使用[view.layer setShadowStuffHere])在动画开始时消失,并在动画结束时重新出现.有什么方法可以保持这些阴影,并让阴影与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];

}
Run Code Online (Sandbox Code Playgroud)

Dav*_*Liu 12

事实证明,当你进行UIView动画时,它会忽略你视图的clipsToBounds属性并且无论如何都会剪辑它(至少对于翻转类型的动画),所以如果你想让你的阴影始终可见,你需要有一个足够大的视图框,包含所有内容.