如何在UIImageView旋转期间动态调整阴影? [英] How to adjust drop shadow dynamically during an UIImageView rotation?
问题描述
我使用下面的代码添加阴影:
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;
并旋转以下内容:
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?
推荐答案
我发现这很有趣,所以我给了它一枪。一个可能的解决方案是在主视图下创建第二个清除视图,使用原始视图的路径给它(底部视图)一个阴影。然后,您可以将动画应用于这两个视图。我用简单的矩形视图,但我没有看到没有理由为什么这不能用更复杂的路径或使用2 CALAYERS而不是2 UIViews。
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...
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, view)...
..and my animation (just hooked up your CAAnimation to a button as an IBAction and applied to both views)...
- (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"];
}
这是结果的样子...
This is what the result looks like...
任何(2D)转换都应该看起来可信。
Any (2D) transformations should look believable if you apply them to both views.
希望有所帮助!
这篇关于如何在UIImageView旋转期间动态调整阴影?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!