我可以用动画在中心周围旋转UIImage吗?

时间:2014-09-26 06:14:45

标签: ios xcode animation uiimage

我想在圆形动画中旋转图像。我怎么能以简单的方式做到这一点? 我知道这不难,但我是iOS的新手。 谁能告诉我正确的代码?

3 个答案:

答案 0 :(得分:1)

[[self.<imageviewinstance> layer] addAnimation:[self rotationAnimation] forKey:nil];

添加上面的动画,下面是动画代码。

- (CABasicAnimation *)rotationAnimation
{
    CABasicAnimation *rotAnim = [CABasicAnimation animationWithKeyPath:@"position"];
    [rotAnim setFromValue:[NSValue valueWithCGPoint:CGPointMake(100.0, 400.0)]];
    rotAnim.toValue = [NSValue valueWithCGPoint:CGPointMake(300.0, 150.0)];
   // rotAnim.duration = 5.0;
    rotAnim.autoreverses = YES;
   // rotAnim.repeatCount = HUGE_VAL;
    rotAnim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    return rotAnim;
}

如果以上不起作用,请使用此

    - (CABasicAnimation *)rotationAnimation
    {
    CABasicAnimation *move = [CABasicAnimation animationWithKeyPath:@"transform.rotation" ];
    move.delegate = self;
    [move setFromValue:[NSNumber numberWithFloat:0]];
    [move setToValue:[NSNumber numberWithFloat:50]];
    [move setDuration:10.0f];
    move.autoreverses = YES;
 }

答案 1 :(得分:1)

我的解决方案:

- (void)startAnimation
{
 [UIView animateWithDuration:.4 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
     [self.loadingRingImageView setTransform:CGAffineTransformRotate(self.loadingRingImageView.transform, M_SQRT2)];
 }                completion:^(BOOL finished) {
     if (finished)
     {
         [self startAnimation];
     }
 }];
}

答案 2 :(得分:0)

我想知道的问题的答案是 - &gt;

UIImageView *imageViewForAnimation = [[UIImageView alloc] initWithImage:imageToAnimate];
    imageViewForAnimation.alpha = 1.0f;
    CGRect imageFrame = imageViewForAnimation.frame;
    //Your image frame.origin from where the animation need to get start
    CGPoint viewOrigin = imageViewForAnimation.frame.origin;
    viewOrigin.y = viewOrigin.y + imageFrame.size.height / 2.0f;
    viewOrigin.x = viewOrigin.x + imageFrame.size.width / 2.0f;

    imageViewForAnimation.frame = imageFrame;
    imageViewForAnimation.layer.position = viewOrigin;
    [self.view addSubview:imageViewForAnimation];

// Set up fade out effect
CABasicAnimation *fadeOutAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
[fadeOutAnimation setToValue:[NSNumber numberWithFloat:0.3]];
fadeOutAnimation.fillMode = kCAFillModeForwards;
fadeOutAnimation.removedOnCompletion = NO;

// Set up scaling
CABasicAnimation *resizeAnimation = [CABasicAnimation animationWithKeyPath:@"bounds.size"];
[resizeAnimation setToValue:[NSValue valueWithCGSize:CGSizeMake(40.0f, imageFrame.size.height * (40.0f / imageFrame.size.width))]];
resizeAnimation.fillMode = kCAFillModeForwards;
resizeAnimation.removedOnCompletion = NO;

// Set up path movement
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
//Setting Endpoint of the animation
CGPoint endPoint = CGPointMake(480.0f - 30.0f, 40.0f);
//to end animation in last tab use 
//CGPoint endPoint = CGPointMake( 320-40.0f, 480.0f);
CGMutablePathRef curvedPath = CGPathCreateMutable();
CGPathMoveToPoint(curvedPath, NULL, viewOrigin.x, viewOrigin.y);
CGPathAddCurveToPoint(curvedPath, NULL, endPoint.x, viewOrigin.y, endPoint.x, viewOrigin.y, endPoint.x, endPoint.y);
pathAnimation.path = curvedPath;
CGPathRelease(curvedPath);

CAAnimationGroup *group = [CAAnimationGroup animation]; 
group.fillMode = kCAFillModeForwards;
group.removedOnCompletion = NO;
[group setAnimations:[NSArray arrayWithObjects:fadeOutAnimation, pathAnimation, resizeAnimation, nil]];
group.duration = 0.7f;
group.delegate = self;
[group setValue:imageViewForAnimation forKey:@"imageViewBeingAnimated"];

[imageViewForAnimation.layer addAnimation:group forKey:@"savingAnimation"];
相关问题