代码之家  ›  专栏  ›  技术社区  ›  ron

基本关键帧动画(旋转)

  •  14
  • ron  · 技术社区  · 17 年前

    我试图创建一个非常简单的关键帧动画,通过一个给定的中点,一个图形从一个角度旋转到另一个角度。

    (目的是能够通过 钝角 弧角大于180度,而不是动画“欺骗”和走最短路线,即通过相反方向。 ,急性 较小的角度——当只有一个关键帧(即目的地)时会发生这种情况。为了走“远”的路,我想我需要一个额外的关键帧在中间,沿着 渴望的 弧)

    以下是我迄今为止的成果(通过最锐利的角度使图形达到所需的旋转角度):

    #define DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) / 180.0 * M_PI)
    
    ...
    
    [UIView beginAnimations:nil context:nil];
    CGAffineTransform cgCTM = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(desiredEndingAngle));
    [UIView setAnimationDuration:0.5];
    graphic.transform = cgCTM;
    [UIView commitAnimations];
    

    据我所知,我并不想沿着路径寻找动画(因为这是为了翻译,而不是旋转)。

    不管怎样,任何帮助都是非常感谢的!事先谢谢。

    3 回复  |  直到 14 年前
        1
  •  18
  •   ron    17 年前

    我想我明白了。

    这里是s代码,它(在本例中)执行完整的270度旋转(1.5*pi弧度),包括可以进一步定制的各种参数:

    CALayer *layer = rotatingImage.layer;
    CAKeyframeAnimation *animation;
    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.duration = 0.5f;
    animation.cumulative = YES;
    animation.repeatCount = 1;
    animation.values = [NSArray arrayWithObjects:       // i.e., Rotation values for the 3 keyframes, in RADIANS
          [NSNumber numberWithFloat:0.0 * M_PI], 
          [NSNumber numberWithFloat:0.75 * M_PI], 
          [NSNumber numberWithFloat:1.5 * M_PI], nil]; 
    animation.keyTimes = [NSArray arrayWithObjects:     // Relative timing values for the 3 keyframes
          [NSNumber numberWithFloat:0], 
          [NSNumber numberWithFloat:.5], 
          [NSNumber numberWithFloat:1.0], nil]; 
    animation.timingFunctions = [NSArray arrayWithObjects:
          [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn],    // from keyframe 1 to keyframe 2
          [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut], nil]; // from keyframe 2 to keyframe 3
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    
    [layer addAnimation:animation forKey:nil];
    

    谢谢!

        2
  •  1
  •   Nathan de Vries    17 年前

    试试这个:

    UIImageView* rotatingImage = [[UIImageView alloc] init]];
    [rotatingImage setImage:[UIImage imageNamed:@"someImage.png"]];
    
    CATransform3D rotationTransform = CATransform3DMakeRotation(1.0f * M_PI, 0, 0, 1.0);
    CABasicAnimation* rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
    
    rotationAnimation.toValue = [NSValue valueWithCATransform3D:rotationTransform];
    rotationAnimation.duration = 0.25f;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = 1;
    
    [rotatingImage.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    
        3
  •  1
  •   Vinoth    14 年前
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 100, 100);
    CGPathAddQuadCurveToPoint(path, NULL, 100, 100, 100, 615);
    CGPathAddQuadCurveToPoint(path, NULL, 100, 615, 900, 615);
    CGPathAddQuadCurveToPoint(path, NULL, 900, 615, 900, 100);
    CGPathAddQuadCurveToPoint(path, NULL, 900, 100, 100, 80);
    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    pathAnimation.path = path;
    pathAnimation.duration = 10.0;
    [someLayer addAnimation:pathAnimation forKey:nil];
    
    推荐文章