如何对图层阴影不透明度进行动画处理?

发布于 2024-10-09 02:29:14 字数 480 浏览 8 评论 0原文

我有一个视图,我已将其图层不透明度设置为 1。

    theView.layer.shadowOpacity = 1.0;

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

alt text

编辑: 请求代码不起作用:

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

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

    theView.layer.shadowOpacity = 1.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.

alt text

EDIT: Request for code that doesn't work:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

今天小雨转甜 2024-10-16 02:29:14

这将正常工作:

#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

This will work properly:

#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;

For 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
月牙弯弯 2024-10-16 02:29:14

我已将上面的代码放在 UIView 的一个小扩展中:

extension UIView {

func animateLayer<Value>(_ keyPath: WritableKeyPath<CALayer, Value>, to value:Value, duration: CFTimeInterval) {

    let keyString = NSExpression(forKeyPath: keyPath).keyPath
    let animation = CABasicAnimation(keyPath: keyString)
    animation.fromValue = self.layer[keyPath: keyPath]
    animation.toValue = value
    animation.duration = duration
    self.layer.add(animation, forKey: animation.keyPath)
    var thelayer = layer
    thelayer[keyPath: keyPath] = value
}
}

用法如下:

animateLayer(\.shadowOffset, to: CGSize(width: 3, height: 3), duration:1)
animateLayer(\.shadowOpacity, to: 0.4, duration: 1)

它尚未经过彻底测试。但为我工作。
(还发布在此处

I've put above code in a little extension of UIView:

extension UIView {

func animateLayer<Value>(_ keyPath: WritableKeyPath<CALayer, Value>, to value:Value, duration: CFTimeInterval) {

    let keyString = NSExpression(forKeyPath: keyPath).keyPath
    let animation = CABasicAnimation(keyPath: keyString)
    animation.fromValue = self.layer[keyPath: keyPath]
    animation.toValue = value
    animation.duration = duration
    self.layer.add(animation, forKey: animation.keyPath)
    var thelayer = layer
    thelayer[keyPath: keyPath] = value
}
}

Usage like:

animateLayer(\.shadowOffset, to: CGSize(width: 3, height: 3), duration:1)
animateLayer(\.shadowOpacity, to: 0.4, duration: 1)

It's not thoroughly tested. but worked for me.
(Also posted here)

甜`诱少女 2024-10-16 02:29:14

下面的代码对我有用

1)添加 QuartzCore 框架工作
2)导入QuartzCore框架,

在需要的地方添加以下代码

UIImageView * glowimageview = [[[UIImageView alloc]init]autorelease];
    [glowimageview setFrame:CGRectMake(500,400,200,200)];
    [glowimageview setImage:[UIImage imageNamed:@"144.png"]];
    [sender addSubview:glowimageview];

    glowimageview.layer.shadowColor = [UIColor redColor].CGColor;
    glowimageview.layer.shadowRadius = 10.0f;
    glowimageview.layer.shadowOpacity = 1.0f;
    glowimageview.layer.shadowOffset = CGSizeZero;

    CABasicAnimation *shadowAnimation = [CABasicAnimation animationWithKeyPath:@"shadowOpacity"];
    shadowAnimation.duration=1.0;
    shadowAnimation.repeatCount=HUGE_VALF;
    shadowAnimation.autoreverses=YES;
    shadowAnimation.fromValue = [NSNumber numberWithFloat:1.0];
    shadowAnimation.toValue = [NSNumber numberWithFloat:0.0];
    [glowimageview.layer addAnimation:shadowAnimation forKey:@"shadowOpacity"];

就可以了。根据您的要求更改代码格式

The below code work for me

1)Add QuartzCore frame work
2)Import QuartzCore frame work

Add the following Code in the required place

UIImageView * glowimageview = [[[UIImageView alloc]init]autorelease];
    [glowimageview setFrame:CGRectMake(500,400,200,200)];
    [glowimageview setImage:[UIImage imageNamed:@"144.png"]];
    [sender addSubview:glowimageview];

    glowimageview.layer.shadowColor = [UIColor redColor].CGColor;
    glowimageview.layer.shadowRadius = 10.0f;
    glowimageview.layer.shadowOpacity = 1.0f;
    glowimageview.layer.shadowOffset = CGSizeZero;

    CABasicAnimation *shadowAnimation = [CABasicAnimation animationWithKeyPath:@"shadowOpacity"];
    shadowAnimation.duration=1.0;
    shadowAnimation.repeatCount=HUGE_VALF;
    shadowAnimation.autoreverses=YES;
    shadowAnimation.fromValue = [NSNumber numberWithFloat:1.0];
    shadowAnimation.toValue = [NSNumber numberWithFloat:0.0];
    [glowimageview.layer addAnimation:shadowAnimation forKey:@"shadowOpacity"];

It will works. Change the format of the code as per your requirement

余生共白头 2024-10-16 02:29:14

这是一个材料设计,就像上面的一些动画一样
它也可以通过 Carthage 作为框架在此处使用 https://github.com/sevenapps/SVNMaterialButton

public init(frame: CGRect, color: UIColor) {
    super.init(frame: frame)
    self.backgroundColor = color
    self.layer.masksToBounds = false
    self.layer.borderWidth = 1.0
    self.layer.shadowColor = UIColor.black.cgColor
    self.layer.shadowOpacity = 0.8
    self.layer.shadowRadius = 8
    self.layer.shadowOffset = CGSize(width: 8.0, height: 8.0)
}

required public init?(coder aDecoder: NSCoder) {
    fatalError("This class is not set up to be instaciated with coder use init(frame) instead")
}

public override func layoutSubviews() {
    super.layoutSubviews()
    self.layer.cornerRadius = self.frame.height / 4
}

public override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.animate(to: 0.5, and: CGSize(width: 5.0, height: 5.0), with: 0.5)
    super.touchesBegan(touches, with: event)
}

public override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.animate(to: 0.8, and: CGSize(width: 8.0, height: 8.0), with: 0.5)
    super.touchesBegan(touches, with: event)
}

private func animate(to opacity: Double, and offset: CGSize, with duration: Double){
    CATransaction.begin()
    let opacityAnimation = CABasicAnimation(keyPath: "shadowOpacity")
    opacityAnimation.toValue = opacity
    opacityAnimation.duration = duration
    opacityAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
    opacityAnimation.fillMode = kCAFillModeBoth
    opacityAnimation.isRemovedOnCompletion = false

    let offsetAnimation = CABasicAnimation(keyPath: "shadowOffset")
    offsetAnimation.toValue = offset
    offsetAnimation.duration = duration
    offsetAnimation.timingFunction = opacityAnimation.timingFunction
    offsetAnimation.fillMode = opacityAnimation.fillMode
    offsetAnimation.isRemovedOnCompletion = false

    self.layer.add(offsetAnimation, forKey: offsetAnimation.keyPath!)
    self.layer.add(opacityAnimation, forKey: opacityAnimation.keyPath!)
    CATransaction.commit()
}

Here's a material design like take on some of the above with animations
It's also available here as framework through Carthage https://github.com/sevenapps/SVNMaterialButton

public init(frame: CGRect, color: UIColor) {
    super.init(frame: frame)
    self.backgroundColor = color
    self.layer.masksToBounds = false
    self.layer.borderWidth = 1.0
    self.layer.shadowColor = UIColor.black.cgColor
    self.layer.shadowOpacity = 0.8
    self.layer.shadowRadius = 8
    self.layer.shadowOffset = CGSize(width: 8.0, height: 8.0)
}

required public init?(coder aDecoder: NSCoder) {
    fatalError("This class is not set up to be instaciated with coder use init(frame) instead")
}

public override func layoutSubviews() {
    super.layoutSubviews()
    self.layer.cornerRadius = self.frame.height / 4
}

public override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.animate(to: 0.5, and: CGSize(width: 5.0, height: 5.0), with: 0.5)
    super.touchesBegan(touches, with: event)
}

public override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.animate(to: 0.8, and: CGSize(width: 8.0, height: 8.0), with: 0.5)
    super.touchesBegan(touches, with: event)
}

private func animate(to opacity: Double, and offset: CGSize, with duration: Double){
    CATransaction.begin()
    let opacityAnimation = CABasicAnimation(keyPath: "shadowOpacity")
    opacityAnimation.toValue = opacity
    opacityAnimation.duration = duration
    opacityAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
    opacityAnimation.fillMode = kCAFillModeBoth
    opacityAnimation.isRemovedOnCompletion = false

    let offsetAnimation = CABasicAnimation(keyPath: "shadowOffset")
    offsetAnimation.toValue = offset
    offsetAnimation.duration = duration
    offsetAnimation.timingFunction = opacityAnimation.timingFunction
    offsetAnimation.fillMode = opacityAnimation.fillMode
    offsetAnimation.isRemovedOnCompletion = false

    self.layer.add(offsetAnimation, forKey: offsetAnimation.keyPath!)
    self.layer.add(opacityAnimation, forKey: opacityAnimation.keyPath!)
    CATransaction.commit()
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文