Objective c - 使用弧度旋转轮子

发布于 2024-09-24 04:00:29 字数 598 浏览 2 评论 0原文

我正在尝试制作一个圆形图像,当用户拖动滚轮时该图像会旋转

这是我的代码:

CABasicAnimation *rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat:angleRadians];
rotationAnimation.duration = 10;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = 1.0;
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[self.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

我遇到的主要问题是图像在完成后被重置。

有什么帮助吗?

I am trying to make a circle image which rotates when the user drags the wheel

This is my code:

CABasicAnimation *rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat:angleRadians];
rotationAnimation.duration = 10;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = 1.0;
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[self.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

The main issue I am having though, that the image is being reset when it is finished.

Any help please?

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

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

发布评论

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

评论(1

墨小沫ゞ 2024-10-01 04:00:29

我认为你处理这个问题的方法是错误的。核心动画主要用于动画短暂效果,例如视图转换、淡入淡出等。对图层进行动画处理后,CA 会丢弃其工作值并恢复图层的原始状态,这就是重置图像的原因。虽然毫无疑问可以使用 CA 做你想做的事情,但我认为这是困难的方法。

要使图像跟踪用户的手指,我建议简单地计算当前触摸位置所需的角度,并在触摸位置发生变化时将图像转换为该角度:-

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
  double angle = /* calculate your angle in radians here */;
  imageView.transform = CGAffineTransformMakeRotation( angle );
}

I think you're approaching this problem wrong. Core Animation is primarily intended for animating transitory effects such as view transitions, fades, etc. After animating a layer, CA discards its working values and the original state of the layer is restored, which is why your image is resetting. Whilst it is no doubt possible to do what you want using CA, I think it is the hard way.

To make an image track the user's finger, I'd suggest simply calculating the angle you want from the current touch position, and transform the image for that angle whenever the touch position changes:-

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
  double angle = /* calculate your angle in radians here */;
  imageView.transform = CGAffineTransformMakeRotation( angle );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文