Objective c - 使用弧度旋转轮子
我正在尝试制作一个圆形图像,当用户拖动滚轮时该图像会旋转
这是我的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你处理这个问题的方法是错误的。核心动画主要用于动画短暂效果,例如视图转换、淡入淡出等。对图层进行动画处理后,CA 会丢弃其工作值并恢复图层的原始状态,这就是重置图像的原因。虽然毫无疑问可以使用 CA 做你想做的事情,但我认为这是困难的方法。
要使图像跟踪用户的手指,我建议简单地计算当前触摸位置所需的角度,并在触摸位置发生变化时将图像转换为该角度:-
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:-