【问题标题】:Getting transform.rotation.z value from an animated view从动画视图中获取 transform.rotation.z 值
【发布时间】:2014-04-04 22:52:09
【问题描述】:

我正在使用CABasicAnimation 连续旋转UIImageView,我希望能够获得旋转角度。

-(void)viewDidAppear:(BOOL)animated{

    [self spinAnimationOnView:self.myImageView
                        duration:2
                          repeat:HUGE_VAL];
}

- (void)spinAnimationOnView:(UIView*)view duration:(CGFloat)duration repeat:(float)repeat{
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0];
    rotationAnimation.duration = duration;
    rotationAnimation.repeatCount = repeat;

    [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}

动画按预期运行。但后来我尝试得到这样的旋转角度:

CGFloat angle = [(NSNumber *)[self.myImageView valueForKeyPath:@"layer.transform.rotation.z"] floatValue];

NSLog(@"ROTATION = %f", angle);

输出始终为 0。有什么想法吗?

【问题讨论】:

    标签: ios objective-c rotation cabasicanimation


    【解决方案1】:

    在动画进行时,您需要使用 CALayer 的presentationLayer 来获取图层的状态。请参阅CALayer docs

    CGFloat angle = [[self.myImageView valueForKeyPath:@"layer.presentationLayer.transform.rotation.z"] floatValue];
    
    NSLog(@"ROTATION = %f", angle);
    

    【讨论】:

      猜你喜欢
      • 2018-02-18
      • 2012-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多