【问题标题】:CABasicAnimation how to get current angle of rotationCABasicAnimation 如何获取当前的旋转角度
【发布时间】:2012-04-08 10:44:24
【问题描述】:

我对 CABasicAnimation 有一些问题。和那个帖子类似:CABasicAnimation rotate returns to original position

所以,我有在 touchMove 中旋转的 uiimageview。在做“惯性动画”的 touchEnd 调用方法中:

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

这个动画效果很好,但是如果我在 animationRotation 结束之前调用 touchBegan,旋转的角度就是 beginValue。我需要当前的旋转角度。作为一个实验,我声明了方法

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

它似乎工作。但我不知道如何在animationDidStop 中为我的UIImageView 获取角度或CGAffineTransform 的值。 甚至有可能做到吗?谢谢。

【问题讨论】:

    标签: ios xcode4 rotation caanimation cabasicanimation


    【解决方案1】:

    你应该使用presentationLayer方法在动画飞行期间获取图层属性。

    所以你的代码应该是这样的,

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

    【讨论】:

    • 谢谢,我试过了,它可以工作,但不是它应该的那样。我使用了 NSTimer 动画.. 不过还是谢谢 :-)
    • 赞成获取图层当前可见旋转的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 2015-08-23
    相关资源
    最近更新 更多