2012-04-08 22 views
7

Tôi gặp một số vấn đề với CABasicAnimation. Nó tương tự như bài đăng đó: CABasicAnimation rotate returns to original positionCABasicAnimation cách lấy góc quay hiện tại

Vì vậy, tôi có uiimageview xoay trong touchMove. Trong touchEnd gọi phương pháp đó làm "hình ảnh động quán tính":

-(void)animationRotation: (float)beginValue 
{ 
    CABasicAnimation *anim; 
    anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; 
    anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 
    anim.duration = 0.5; 
    anim.repeatCount = 1; 

    anim.fillMode = kCAFillModeForwards; 
    anim.fromValue = [NSNumber numberWithFloat:beginValue]; 
    [anim setDelegate:self];  

    anim.toValue = [NSNumber numberWithFloat:(360*M_PI/180 + beginValue)]; 
    [appleView.layer addAnimation:anim forKey:@"transform"]; 

    CGAffineTransform rot = CGAffineTransformMakeRotation(360*M_PI/180 + beginValue); 
    appleView.transform = rot; 
} 

hoạt hình này hoạt động tốt, nhưng nếu tôi gọi touchBegan trước animationRotation kết thúc, góc quay là beginValue. Tôi cần góc quay hiện tại của cath. Là một thử nghiệm, tôi khai báo phương thức

-(vod) animationDidStop:(CAAnimation *)anim finished:(BOOL)flag 
{ 
     NSLog(@"Animation finished!"); 
} 

và có vẻ như nó hoạt động. nhưng tôi không biết làm thế nào có được giá trị của góc hoặc CGAffineTransform cho UIImageView của tôi trong animationDidStop. Thậm chí có thể làm được? Cảm ơn.

Trả lời

9

bạn nên sử dụng phương thức presentationLayer để lấy thuộc tính lớp trong khi hoạt ảnh trong chuyến bay.

do đó, mã của bạn nên được như thế này,

#define RADIANS_TO_DEGREES(__ANGLE__) ((__ANGLE__)/(float)M_PI * 180.0f) 

    -(void)animationRotation: (float)beginValue 
    { 
     CABasicAnimation *anim; 
     anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 
     anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 
     anim.duration = 0.5; 
     anim.repeatCount = 1; 

     anim.fillMode = kCAFillModeForwards; 
     anim.fromValue = [NSNumber numberWithFloat:beginValue]; 
     [anim setDelegate:self];  



    //get current layer angle during animation in flight 
     CALayer *currentLayer = (CALayer *)[appleView.layer presentationLayer];  
     float currentAngle = [(NSNumber *)[currentLayer valueForKeyPath:@"transform.rotation.z"] floatValue]; 
     currentAngle = roundf(RADIANS_TO_DEGREES(currentAngle));   

     NSLog(@"current angle: %f",currentAngle); 



     anim.toValue = [NSNumber numberWithFloat:(360*M_PI/180 + beginValue)]; 
     [appleView.layer addAnimation:anim forKey:@"transform"]; 

     CGAffineTransform rot = CGAffineTransformMakeRotation(360*M_PI/180 + beginValue); 
     appleView.transform = rot; 
    } 
+0

Cảm ơn, tôi đã cố gắng này, nó hoạt động, nhưng không hoàn toàn như mong muốn. Tôi đã sử dụng NSTimer animation .. nhưng dù sao đi nữa :-) – frankWhite

+0

Upvoted cho phương thức lấy vòng quay hiện tại của layer. – geraldWilliam

Các vấn đề liên quan