[英]UIView: How to animate CALayer frame with shadow?
I added a shadow layer as a sublayer to UIView's layer. 我在UIView的图层中添加了一个阴影图层作为子图层。 Following is an added method for
UIView
subclass: 以下是
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];
}
I would like to keep it as a part of resizing animation of the an UIView
with shadowSubview. 我想保留它作为使用shadowSubview调整
UIView
动画大小的一部分。 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;
但是在使用
+ (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];
}];
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. 试图了解
CABasicAnimation
,但是找不到将其应用于这种情况的方法。
我建议您还设置阴影层的shadowPath
属性,然后参考Animating CALayer的shadowPath属性来实现阴影路径的动画。
CALayer layer frame and shadow animation: You can apply shadow on CALayer itself and adjust the frame. CALayer图层框架和阴影动画:您可以在CALayer本身上应用阴影并调整框架。 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)
Set the shadow directly on your shadowSubview like David say. 如David所说,将阴影直接设置在您的shadowSubview上。
You can do this by replacing: 您可以通过以下方式来实现:
- (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];
}
By this 这样
- (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.