如何设置图层shadowOpacity的动画? [英] How to animate layer shadowOpacity?

查看:105
本文介绍了如何设置图层shadowOpacity的动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个将layerOpacity设置为1的视图.

I have a view on which I've set the layerOpacity to 1.

    theView.layer.shadowOpacity = 1.0;

当视图位于屏幕下方时,这看起来不错.当我将该视图向上移动以与另一个具有阴影的视图齐平时,它们看起来并不好.有没有一种方法可以将图层上的shadowOpacity设置为0?我尝试使用动画块,但似乎此属性无法设置动画.

This looks fine when the view is farther down the screen. When I move this view up to be flush with another view that has a shadow, they don't look good. Is there a way I can animate the shadowOpacity on my layer to be 0? I tried using an animation block but it seems as if this property is not animatable.

编辑:请求无效的代码:

[UIView animateWithDuration:1.0 animations:^{
    splitView2.layer.shadowOpacity = 0;}
                 completion:NULL];

推荐答案

这将正常工作:

#import <QuartzCore/CAAnimation.h>

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"shadowOpacity"];
anim.fromValue = [NSNumber numberWithFloat:1.0];
anim.toValue = [NSNumber numberWithFloat:0.0];
anim.duration = 1.0;
[vv.layer addAnimation:anim forKey:@"shadowOpacity"];
vv.layer.shadowOpacity = 0.0;

对于Swift 3.0:

 let animation = CABasicAnimation(keyPath: "shadowOpacity")
 animation.fromValue = layer.shadowOpacity
 animation.toValue = 0.0
 animation.duration = 1.0
 view.layer.add(animation, forKey: animation.keyPath)
 view.layer.shadowOpacity = 0.0

这篇关于如何设置图层shadowOpacity的动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆