2012-04-08 17 views
7

Ich habe ein Problem mit CABasicAnimation. Es ist ähnlich zu diesem Beitrag: CABasicAnimation rotate returns to original positionCABasicAnimation Wie bekomme ich den aktuellen Drehwinkel

Also, ich habe uiimageview, dass in TouchMove drehen. In touchEnd aufrufen Methode, die „Animation Trägheits“ tun:

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

Diese Animation funktioniert gut, aber wenn ich touchBegan aufrufen, bevor animationRotation beendet, Drehwinkel ist Beginvalue. Ich brauche den aktuellen Drehwinkel des Katheters. Als ein Experiment erkläre ich Methode

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

und es scheint zu arbeiten. aber ich weiß nicht wie ich diesen Wert von angle oder CGAffineTransform für meine UIImageView in animationDidStop bekomme. Es ist sogar möglich zu tun? Vielen Dank.

Antwort

9

Sie sollten die presentationLayer-Methode verwenden, um Layer-Eigenschaften während der Animation im Flug zu erhalten.

so Ihr Code so sein sollte,

#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

Danke, habe ich versucht, diese, es funktioniert, aber nicht ganz, wie es sollte. Ich habe NSTimer animation benutzt .. aber danke trotzdem :-) – frankWhite

+0

Upvoted für die Methode um die aktuell sichtbare Rotation der Ebene zu bekommen. – geraldWilliam

Verwandte Themen