首页 文章

围绕其中心旋转UIView几次

提问于
浏览
16

我试图围绕它的中心旋转一些 UIView ,所以简单的代码就像(伪代码):

[UIView beginAnimations:@"crazyRotate" context:nil];
[UIView setAnimationDuration:1.0];
someview.transform = CGAffineTransformMakeRotation(angle);
[UIView commitAnimations]

现在,如果我设置角度来说M_PI / 2,那么事情就会很好地旋转 . 如果我将它设置为2 * M_PI,它确实是"nothing" . 我可以理解,矩阵转化为无效的东西(在某种意义上旋转360意味着"stay"),然而,我想将它旋转5次(想想报纸的旋转刻度会对你有影响 - 我不擅长描述,希望有人理解) . 所以,我尝试将设置角度添加到180度(M_PI)并添加嵌套的 animatationBlock . 但我想,因为我再次设置相同的属性( someview.transition ),它会以某种方式忽略它 . 我尝试用角度M_PI将动画的重复计数设置为2,但它似乎只是旋转180,回到直线位置,然后再次启动旋转 .

所以,我有点想法,任何帮助赞赏! --t

4 回答

  • 1

    您可以在UIView的图层属性上使用以下动画 . 我测试过了 .

    UIView *viewToSpin = ...;    
    CABasicAnimation* spinAnimation = [CABasicAnimation
                                      animationWithKeyPath:@"transform.rotation"];
    spinAnimation.toValue = [NSNumber numberWithFloat:5*2*M_PI];
    [viewToSpin.layer addAnimation:spinAnimation forKey:@"spinAnimation"];
    
  • 6

    正如Brad Larson指出的那样,你可以用 CAKeyframeAnimation 做到这一点 . 例如,

    CAKeyframeAnimation *rotationAnimation;
    rotationAnimation = 
       [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
    
    rotationAnimation.values = [NSArray arrayWithObjects:
                                [NSNumber numberWithFloat:0.0 * M_PI], 
                                [NSNumber numberWithFloat:0.75 * M_PI], 
                                [NSNumber numberWithFloat:1.5 * M_PI], 
                                [NSNumber numberWithFloat:2.0 * M_PI], nil]; 
    rotationAnimation.calculationMode = kCAAnimationPaced;
    rotationAnimation.removedOnCompletion = NO;
    rotationAnimation.fillMode = kCAFillModeForwards;
    rotationAnimation.timingFunction = 
       [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    rotationAnimation.duration = 10.0;
    
    CALayer *layer = [viewToSpin layer];
    [layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    

    您可以使用 rotationAnimation.duration 属性控制总动画的持续时间,并使用 rotationAnimation.timingFunction 属性控制加速和减速(以及两者之间的步骤计算) .

  • 36

    获得连续旋转效果有点棘手,但我描述了一种方法来实现它here . 是的,Core Animation似乎优化了转换到单位圆内最接近的结束位置 . 我描述的方法将一些半旋转动画链接在一起以进行完全旋转,尽管你注意到从一个动画到下一个动画的切换中有轻微的断言 .

    也许用这些半旋转值构造的CAKeyframeAnimation是正确的方法 . 然后你也可以控制加速和减速 .

  • 1
    CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.fromValue = [NSNumber numberWithFloat:0.0f];
    animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
    animation.duration = 8.0f;
    animation.repeatCount = INFINITY;
    [self.myView.layer addAnimation:animation forKey:@"SpinAnimation"];
    

相关问题