我发现这很有趣,所以我尝试了一下。一种可能的解决方案是在主视图下构建第二个清晰视图,使用原始视图的路径为其(底部视图)提供阴影。然后您可以将动画应用到两个视图。我使用简单的矩形视图完成了此操作,但我认为没有理由不能使用更复杂的路径或使用 2 个 CALayers 而不是 2 个 UIView 来完成此操作。
我的观点就是这样设定的……
testView = [[UIView alloc] initWithFrame:CGRectMake(40, 40, 100, 100)];
testView.backgroundColor = [UIColor redColor];
//increase y origin of second view to simulate shadow offset
testViewShadow = [[UIView alloc] initWithFrame:CGRectMake(40, 50, 100, 100)];
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, CGRectMake(0, 0, testView.frame.size.width, testView.frame.size.height));
testViewShadow.layer.shadowPath = path;
testViewShadow.layer.shadowOpacity = 1.0;
testViewShadow.layer.shadowOffset = CGSizeMake(0, 0);
testViewShadow.layer.shadowRadius = 10.0;
CFRelease(path);
[self.view addSubview:testViewShadow];
[self.view addSubview:testView];
..还有我的动画(刚刚将您的 CAAnimation 作为 IBAction 连接到按钮并应用于两个视图)...
- (IBAction)rotate:(id)sender{
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0];
rotationAnimation.duration = 5.0;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = 1.0;
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[testView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
[testViewShadow.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}
这就是结果的样子......
如果将任何(2D)转换应用于两个视图,它们应该看起来可信。
希望有帮助!