2013-07-25 5 views
1

CALayer의 경계를 애니메이션화하려고하는데 작동하지 않습니다. 여기 코드는 : 나는 initWithFrame 방법에서 사라 호출하면CALayer 불투명도 애니메이션이 작동하지만 경계 애니메이션이 적용되지 않습니다.

class CircleView < UIIVew 

    def initWithFrame(frame) 
    super 

    # determine the dimensions 
    radius = (bounds.size.width < bounds.size.height ? bounds.size.width : bounds.size.height)/2 

    # create the circle layer 
    @circle = CAShapeLayer.layer 
    @circle.bounds = bounds 
    @circle.path = UIBezierPath.bezierPathWithRoundedRect(bounds, cornerRadius: radius).CGPath 

    @circle.fillColor = UIColor.blueColor.CGColor 

    # add the circle to the view 
    self.layer.addSublayer(@circle) 

    shrink 

    self 
    end 

    private 

    # Applies the shrink animation to the provided circle layer. 
    def shrink 

    # determine the bounds 
    old_bounds = @circle.bounds 
    new_bounds = CGRectMake(old_bounds.size.width/2, old_bounds.size.height/2, 0, 0) 

    # animate the shrinking 
    animation = CABasicAnimation.animationWithKeyPath("bounds") 
    animation.fromValue = NSValue.valueWithCGRect(old_bounds) 
    animation.toValue = NSValue.valueWithCGRect(new_bounds) 
    animation.duration = 3.0 
    @circle.addAnimation(animation, forKey:"bounds") 

    # set the bounds so the animation doesn't bounce back when it's complete 
    @circle.bounds = new_bounds 
    end 

    # Applies the shrink animation to the provided circle layer. 
    def disappear 

    # animate the shirnk 
    animation = CABasicAnimation.animationWithKeyPath("opacity") 
    animation.fromValue = NSNumber.numberWithFloat(1.0) 
    animation.toValue = NSNumber.numberWithFloat(0.0) 
    animation.duration = 3.0 
    @circle.addAnimation(animation, forKey:"opacity") 

    # set the value so the animation doesn't bound back when it's completed 
    @circle.opacity = 0.0 
    end 
end 

는 애니메이션이 잘 작동합니다. 그러나 shrink를 호출하면 아무 일도 일어나지 않습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

답변

6

CAShapeLayer을 사용했기 때문입니다. pathbounds과 독립적이므로 별도로 애니메이션을 적용해야합니다.

내가이에 shrink 방법을 변경하면 대략 목적 C.로 변환 코드의 버전으로 문제를 재현 할 수

, 그것은 일 :

-(void)shrink 
{ 
    CGRect old_bounds = self.circle.bounds; 
    CGRect new_bounds = CGRectMake(old_bounds.size.width/2, old_bounds.size.height/2, 0, 0); 

    // bounds 
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath: @"bounds"]; 
    animation.fromValue = [NSValue valueWithCGRect: old_bounds]; 
    animation.toValue = [NSValue valueWithCGRect: new_bounds]; 
    animation.duration = 3.0; 
    [self.circle addAnimation: animation forKey: @"bounds"]; 

    // path 
    CGPathRef old_path = self.circle.path; 
    CGPathRef new_path = [UIBezierPath bezierPathWithRoundedRect:new_bounds cornerRadius:0].CGPath; 

    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath: @"path"]; 
    pathAnimation.fromValue = (__bridge id)(old_path); 
    pathAnimation.toValue = (__bridge id)(new_path); 
    pathAnimation.duration = 3.0; 
    [self.circle addAnimation: pathAnimation forKey: @"path"]; 

    //set the value so the animation doesn't bound back when it's completed 
    self.circle.bounds = new_bounds; 
    self.circle.path = new_path; 
} 

기술적으로, 당신도 애니메이션이 없을 수도 있습니다 범위. 을 circle 레이어에 설정하면 boundspath을 다소 독립적으로 설정하고 애니메이션 할 수있는 방법을 조금 더 쉽게 실험 할 수 있습니다 (예 : 원래 코드에서 경계가 실제로 볼 수 있음) 움직이지만 경로는 동일하게 유지됩니다.)

+0

완벽하게 작동했습니다! 도와 주셔서 감사합니다! 경계 애니메이션을 나갈 수있었습니다. – LandonSchropp

-2

루비를 모르는 상태에서 경계가 아닌 프레임을 조정하는 것이 좋습니다. 경계는 CALayers 자체의 좌표계에 있으며 계층 자체가 상위에 표시되는 방법에 영향을주지 않습니다.

+1

Apple Docs : frame : 이 속성은 애니메이션으로 만들 수 없습니다. 범위와 위치 속성에 애니메이션을 적용하여 동일한 결과를 얻을 수 있습니다. http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreAnimation_guide/AnimatableProperties/AnimatableProperties.html#//apple_ref/doc/uid/TP40004514-CH11-SW1 –

+0

죄송합니다. 현재 귀하는 부모를 기준으로 레이어의 원점을 설정하는 경계를 사용합니다. 이것은 프레임 자체가 아닌 콘텐츠에만 영향을줍니다. 반면 프레임은 원하는 것을 수행합니다. 사과 docs 당신이 위치가 animatable다는 것을 나타 내기 위하여 연결하십시오. 위치는 기본적으로 anchorPoint를 기준으로하며 경계의 중심입니다. 레이어의 크기를 애니메이션으로 만들려면 경계를 사용하십시오. [사과 dev에 더 많은 정보 링크] (http://developer.apple.com/library/mac/ipad/#qa/qa1620/_index.html) – user352891

관련 문제