簡體   English   中英

UIView:如何為CALayer框架添加陰影動畫?

[英]UIView: How to animate CALayer frame with shadow?

我在UIView的圖層中添加了一個陰影圖層作為子圖層。 以下是UIView子類的添加方法:

- (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 animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion;找不到正確的方法+ (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 ,但是找不到將其應用於這種情況的方法。

我建議您還設置陰影層的shadowPath屬性,然后參考Animating CALayer的shadowPath屬性來實現陰影路徑的動畫。

CALayer圖層框架和陰影動畫:您可以在CALayer本身上應用陰影並調整框架。 或為陰影和框架設置不同的層,然后根據您的要求對其進行動畫處理。

我寫了一個對我有用的示例代碼。

    //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)

如David所說,將陰影直接設置在您的shadowSubview上。

您可以通過以下方式來實現:

    - (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];
}

這樣

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM