如何在 UIImageView 旋转期间动态调整阴影?
我使用以下代码来添加投影:
letterE.layer.shadowColor = [[UIColor blackColor] CGColor];
letterE.layer.shadowOffset = CGSizeMake(2.5, 2.5);
letterE.layer.shadowRadius = 3.0;
letterE.layer.shadowOpacity = 0.95;
并使用以下代码来旋转:
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];
[letterE.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
在动画期间,阴影是静态的,看起来很奇怪:
如何使阴影在动画过程中动态更新?
I use the following to code to add drop shadow:
letterE.layer.shadowColor = [[UIColor blackColor] CGColor];
letterE.layer.shadowOffset = CGSizeMake(2.5, 2.5);
letterE.layer.shadowRadius = 3.0;
letterE.layer.shadowOpacity = 0.95;
and the following to rotate:
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];
[letterE.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
During the animation, the shadow is static which looks weird:
How can I make the shadow dynamically updated during the animation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现这很有趣,所以我尝试了一下。一种可能的解决方案是在主视图下构建第二个清晰视图,使用原始视图的路径为其(底部视图)提供阴影。然后您可以将动画应用到两个视图。我使用简单的矩形视图完成了此操作,但我认为没有理由不能使用更复杂的路径或使用 2 个 CALayers 而不是 2 个 UIView 来完成此操作。
这就是我设置视图的方式...
以及我的动画(只需将 CAAnimation 作为 IBAction 连接到按钮并应用于两个视图)...
这就是结果的样子...
任何(2D)变换都应该如果将它们应用到这两种视图中,看起来会很可信。
希望有帮助!
I found this interesting so I gave it a shot. A possible solution is to build a second clear view under the main view, giving it (the bottom view) a shadow using the original view's path. Then you can apply the animation to both views. I did this with simple rectangular views but I see no reason why this can't be done with more complex paths or using 2 CALayers instead of 2 UIViews.
This is how I set up my views...
..and my animation (just hooked up your CAAnimation to a button as an IBAction and applied to both views)...
This is what the result looks like...
Any (2D) transformations should look believable if you apply them to both views.
Hope that helps!