2012-03-03 4 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; 
} 
0

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) 
관련 문제