简体   繁体   English

在UIView动画期间,CALayer Shadows消失

[英]CALayer Shadows disappearing during a UIView animation

So I'm currently testing out the UIView animations, and I noticed that the shadows rendered by CALayer (using [view.layer setShadowStuffHere] ) disappear at the start of the animation and reappear at the end of the animation. 所以我目前正在测试UIView动画,我注意到CALayer渲染的阴影(使用[view.layer setShadowStuffHere] )在动画开始时消失,并在动画结束时重新出现。 Is there any way I can keep these shadows and have the shadows animate along with the UIView? 有什么方法可以保持这些阴影并让阴影与UIView一起动画? I tried using shadows without border paths, but that was a terrible idea, since the frame rate dropped like a rock during the animation, and I didn't get shadows anyways. 我尝试使用没有边框路径的阴影,但这是一个糟糕的主意,因为在动画期间帧速率像摇滚一样下降,而且我没有得到阴影。

The code I'm using right now is below. 我现在使用的代码如下。 Initially you see a red view, and when tapped, it'll flip over into a larger blue view. 最初你看到一个红色的视图,当点击时,它会翻转成一个更大的蓝色视图。 The end result should be something similar to the iPad music app; 最终结果应该类似于iPad音乐应用程序; when an album is selected, it flips over to reveal a view on the backside. 当选择一张专辑时,它会翻转以显示背面的视图。

- (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];

}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM