代码之家  ›  专栏  ›  技术社区  ›  David Liu

在UIView动画中,CALayer阴影消失

  •  6
  • David Liu  · 技术社区  · 14 年前

    所以我正在测试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];
    
    }
    
    1 回复  |  直到 14 年前
        1
  •  12
  •   David Liu    14 年前

    结果是,在执行UIView动画时,它会忽略视图的clipsToBounds属性,并对其进行剪辑(至少对于翻转类型的动画是这样),因此如果希望阴影始终可见,则需要具有足够大的视图帧,以包含所有内容。