iPhone UIImageView 旋转

发布于 2024-08-04 11:50:56 字数 1436 浏览 5 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

独守阴晴ぅ圆缺 2024-08-11 11:50:56

您可以将视图旋转一定的弧度,无论它是小于完整旋转还是完整旋转的许多倍,而不必将旋转分割成多个部分。例如,以下代码将每秒旋转一次视图指定的秒数。您可以轻松修改它以将视图旋转一定数量的旋转或一定数量的弧度。

- (void) runSpinAnimationWithDuration:(CGFloat) duration;
{
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 /* full rotation*/ * rotations * duration ];
    rotationAnimation.duration = duration;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = 1.0; 
    rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];

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

You can rotate a view, by some number of radians, regardless of whether it is less than a full rotation or many multiples of a full rotation, without having to split the rotation into pieces. As an example, the following code will spin a view, once per second, for a specified number of seconds. You can easily modify it to spin a view by a certain number of rotations, or by some number of radians.

- (void) runSpinAnimationWithDuration:(CGFloat) duration;
{
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 /* full rotation*/ * rotations * duration ];
    rotationAnimation.duration = duration;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = 1.0; 
    rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];

    [myView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}
南汐寒笙箫 2024-08-11 11:50:56

根据 mahboudz 的答案,您可以通过设置将其设置为无限旋转:
rotationAnimation.repeatCount = HUGE_VALF;

based on mahboudz's answer, you can set it to infinitely spin by setting:
rotationAnimation.repeatCount = HUGE_VALF;

梦毁影碎の 2024-08-11 11:50:56

如果您的意思是“如何使图像遵循圆形路径?”,那么伪代码将是:

image.x = circleCentre.x + cos(angle) * circleRadius;
image.y = circleCentre.y + sin(angle) * circleRadius;
angle += 5;

If you mean, "How do you make an image follow a circular path?", then the pseudocode would be:

image.x = circleCentre.x + cos(angle) * circleRadius;
image.y = circleCentre.y + sin(angle) * circleRadius;
angle += 5;
未央 2024-08-11 11:50:56

这个问题询问同一件事,尽管用不同的话说。该问题以及此问题中的答案应提供一种使 UIView(UIImageView 只是其子类)绕其中心旋转 360 度或更多的动画的方法。

This question asks the same thing, although in different words. The answers in that question, as well as in this question should provide a way to animate a UIView (UIImageView is simply a subclass of this) about its center for 360 degrees or more.

江湖彼岸 2024-08-11 11:50:56

创建动画

- (CABasicAnimation *)spinAnimationWithDuration:(CGFloat)duration clockwise:(BOOL)clockwise repeat:(BOOL)repeats
{
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    anim.toValue = clockwise ? @(M_PI * 2.0) : @(M_PI * -2.0);
    anim.duration = duration;
    anim.cumulative = YES;
    anim.repeatCount = repeats ? CGFLOAT_MAX : 0;
    return anim;
}

将其添加到视图

CABasicAnimation *animation = [self spinAnimationWithDuration:1.0 clockwise:YES repeat:YES];
[self.spinningView.layer addAnimation:animation forKey:@"rotationAnimation"];

这个答案与 @mahboudz 的答案有何不同?如果大多数函数都返回对象而不是仅仅在这里或那里操作一些对象,那么您将拥有更清晰的代码。

Creating the animation

- (CABasicAnimation *)spinAnimationWithDuration:(CGFloat)duration clockwise:(BOOL)clockwise repeat:(BOOL)repeats
{
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    anim.toValue = clockwise ? @(M_PI * 2.0) : @(M_PI * -2.0);
    anim.duration = duration;
    anim.cumulative = YES;
    anim.repeatCount = repeats ? CGFLOAT_MAX : 0;
    return anim;
}

Add it to a view

CABasicAnimation *animation = [self spinAnimationWithDuration:1.0 clockwise:YES repeat:YES];
[self.spinningView.layer addAnimation:animation forKey:@"rotationAnimation"];

How is this answer different from @mahboudz's answer? You will have way cleaner code if most of your functions returns objects instead of just manipulating some objects here and there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文