UIView:如何为CALayer框架添加阴影动画? [英] UIView: How to animate CALayer frame with shadow?

查看:94
本文介绍了UIView:如何为CALayer框架添加阴影动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UIView的图层中添加了一个阴影图层作为子图层.以下是 UIView 子类的添加方法:

I added a shadow layer as a sublayer to UIView's layer. Following is an added method for UIView subclass:

- (void)addDefaultShadowSubview {
    self.shadowSubview = [[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.frame.size.width, self.frame.size.height)] autorelease];

    CALayer *shadowLayer = [CALayer layer];
    shadowLayer.backgroundColor = self.backgroundColor.CGColor;
    shadowLayer.shadowOffset = CGSizeMake(0, 3);
    shadowLayer.shadowRadius = 5.0;
    shadowLayer.shadowColor = [UIColor blackColor].CGColor;
    shadowLayer.shadowOpacity = 0.8;

    shadowLayer.frame = self.shadowSubview.frame;

    [self.shadowSubview.layer addSublayer:shadowLayer];

    self.shadowSubview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    [self addSubview:self.shadowSubview];
    [self sendSubviewToBack:self.shadowSubview];
}

我希望将其保留为带有shadowSubview的 UIView 的动画大小调整的一部分.但是在使用 +(void)animateWithDuration:(NSTimeInterval)duration动画:(void(^)(void))动画完成:(void(^)(BOOL完成))时找不到正确的方法完成;

I would like to keep it as a part of resizing animation of the an UIView with shadowSubview. But can't find the right way to do it while using + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion;

[UIView animateWithDuration:durationDefaultAnimation
                     animations:^{
                         [self.viewWithShadowSubview setFrame:enlargedFrame];
                     }
                     completion:^(BOOL finished) {
                         [self modifyInsetForCurrentImageviewWithAnimation:YES];
                     }];

请帮助我了解正确的方法.试图了解 CABasicAnimation ,但找不到将其应用于这种情况的方法.

Please help me to know to right way. Tried to learn about CABasicAnimation, but can't find the way to apply it to this case.

推荐答案

CALayer层框架和阴影动画:您可以在CALayer自身上应用阴影并调整框架.或为阴影和框架设置不同的图层,然后根据需要为其设置动画.

CALayer layer frame and shadow animation: You can apply shadow on CALayer itself and adjust the frame. Or have different layer for shadow and frame, then animate it according to your requirements..

我写了一个对我有用的示例代码.

i have written one sample code which worked for me..

    //change in CALayer frame
    let startFrame = CGRect.init(x: -5, y: -5, width: view.frame.size.width+10, height: view.frame.size.height+10)
    let endFrame = CGRect.init(x: -15, y: -15, width: view.frame.size.width+30, height: view.frame.size.height+30)

    //Frame
    let layer = CALayer.init()
    layer.frame = startFrame
    layer.borderWidth = 3.0
    layer.borderColor = UIColor.white.cgColor
    layer.backgroundColor = UIColor.clear.cgColor
    view.layer.addSublayer(layer)

    //Shadow
    view.layer.shadowRadius = 4.0
    view.layer.shadowColor = UIColor.black.cgColor
    view.layer.shadowOffset = CGSize.init(width: 3.0, height: 3.0)

    //Animaiton for shadow
    let animation = CABasicAnimation(keyPath: "shadowOpacity")
    animation.fromValue = 0.0
    animation.toValue = 0.9
    animation.duration = 5.0
    CATransaction.setCompletionBlock {

    }

    //Animation for Frame
    let baseAnimation = CABasicAnimation.init(keyPath: "bounds")
    baseAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
    baseAnimation.duration = 5.0
    baseAnimation.fromValue = NSValue.init(cgRect: startFrame)
    baseAnimation.toValue = NSValue.init(cgRect: endFrame)
    layer.frame = endFrame
    CATransaction.setCompletionBlock {

    }

    //Group animation - if u have multiple CA animation then group else no need to group it
    let animationGroup = CAAnimationGroup.init()
    animationGroup.duration = 5.0
    animationGroup.animations = [animation,baseAnimation]

    view.layer.add(animationGroup, forKey: nil)

这篇关于UIView:如何为CALayer框架添加阴影动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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