2012-03-03 17 views
0

UIViewのレイヤーにサブレイヤとしてシャドーレイヤーを追加しました。私はshadowSubviewとUIViewのアニメーションをリサイズの一部としてそれを維持したいと思いますUIView:影でCALayerフレームをアニメーション化する方法は?

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

UIViewサブクラスのための追加の方法を示します。正しい方法を知っている私を助けてください

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

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion;を使用しながら、しかし、それを行うための正しい方法を見つけることができません。 CABasicAnimationについて学習しようとしましたが、このケースに適用する方法が見つかりません。

+2

なぜあなたは上の影を設定されていませんshadowSubviewレイヤー? –

答えて

0

ダビデのようにあなたのシャドウに影を直接設定してください。

あなたは置き換えることでこれを行うことができます:

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

ことで、私はあなたにも影層のshadowPathプロパティを設定し、シャドウパスのアニメーションを実装するためのAnimating CALayer's shadowPath propertyを参照してください示唆しています。

0

CALayerの層のフレームとシャドウのアニメーション: あなたはCALayerの自体に影を適用し、フレームを調整することができます。それとも...あなたの要件に応じて、その後、影やフレームに異なる層を有していても、それをアニメーション私は私のために働いた1つのサンプルコードを書かれている

...

//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) 
関連する問題