UIImageView旋转动画轻松搞定

时间:2012-06-29 01:45:45

标签: iphone ios uiimageview cabasicanimation

我正在转动一个圆形的圆形箭头圆圈。这很酷,但我希望有一些更精致的东西,当它结束像其他视图,你可以动画他们轻松无论是在内还是外,我都在想你是否可以为此做同样的事情......这是我现在的代码......

- (IBAction)animator:(id)sender
{
    [arrowRotation removeFromSuperview];

    //arrow animation
    arrowRotation = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow.png"]];
    arrowRotation.frame = CGRectMake(148.0, 20.0, 30.0, 30.0);
    [self.view addSubview:arrowRotation];



    CABasicAnimation *fullRotation;
    fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    fullRotation.fromValue = [NSNumber numberWithFloat:0];
    fullRotation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
    fullRotation.duration = 0.75f;
    fullRotation.repeatCount = 3;

    [arrowRotation.layer addAnimation:fullRotation forKey:@"360"];
}

所以我的问题是,当涉及3次旋转时,我可以让这个动画变慢......就像UIView的轻松一样。如果这是有道理的..

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

首先,在此行fullRotation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];中,您的浮动((360*M_PI)/180)会将圆圈旋转360度,但您的数字有点过分。我的意思是你可以将它简化为2*M_PI

第二:如果您想进一步自定义,可以使用CAKeyframeAnimation

CAKeyframeAnimation *rotationAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];

NSArray *rotationValues = [NSArray arrayWithObjects: [NSNumber numberWithFloat: 0], [NSNumber numberWithFloat: (2*M_PI)], nil];

[rotationAnimation setValues:rotationValues];

NSArray *rotationTimes = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f],[NSNumber numberWithFloat:1.0f], nil];
[rotationAnimation setKeyTimes:rotationTimes];

NSArray *rotationTimingFunctions = [NSArray arrayWithObjects:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut],  nil];
[rotationAnimation setTimingFunctions:rotationTimingFunctions];

rotationAnimation.fillMode = kCAFillModeForwards;
rotationAnimation.removedOnCompletion = NO;
rotationAnimation.duration = 1;

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

希望这有帮助!

答案 1 :(得分:0)

使用UIView animateWithDuration:delay:options:animations:completion:制作动画并使用UIViewAnimationOptionCurveEaseOut选项。这是iOS上更新的,首选的UIView动画方法,可以让您轻松完成所需的操作。

文档:http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html

如果您有任何疑问,请与我联系!