objective-c – 试图通过UIRotationGestureController模拟动量惯性

objective-c – 试图通过UIRotationGestureController模拟动量惯性,第1张

概述好吧,所以我试图在iOS中制作一个带有轮子形状的“财富之轮”类型的效果,在那里我可以抓住并旋转一个轮子.我现在可以拖动并旋转轮子到我心脏的内容,但在释放我的手指后,它停止了.我需要对它施加一些动量惯性,以模拟车轮自然旋转. 我已经进行了速度计算,所以当我抬起手指时,我会发出一个速度(介于最小值1和最大值100之间),其范围从1到1800之间(在我最难的一次! ),现在我只想确定如何将该速度转换为 好吧,所以我试图在iOS中制作一个带有轮子形状的“财富之轮”类型的效果,在那里我可以抓住并旋转一个轮子.我现在可以拖动并旋转轮子到我心脏的内容,但在释放我的手指后,它停止了.我需要对它施加一些动量或惯性,以模拟车轮自然旋转.

我已经进行了速度计算,所以当我抬起手指时,我会发出一个速度(介于最小值1和最大值100之间),其范围从1到1800之间(在我最难的一次! ),现在我只想确定如何将该速度转换为实际旋转以应用于对象,以及我将如何随着时间的推移减慢速度.

我最初的想法是这样的:开始以与给定速度相同的速度在一个循环上旋转整圆,然后在每次后续旋转时,将速度减慢一些小百分比.这应该会产生这样的效果:更难旋转更快并且需要更长时间才能减速.

我不是数学家,所以我的方法可能是错的,但是如果有人知道如何让它工作,至少在基本状态下,我会非常感激.这里有一个非常有用的答案:iPhone add inertia/momentum physics to animate “wheel of fortune” like rotating control,但它更具理论性,缺乏关于如何将计算出的速度应用到对象等方面的实际信息.我想我也需要一些动画帮助.

编辑:如果他们顺时针或逆时针拖动轮子我也需要锻炼.

非常感谢!

解决方法 我已经为我的程序Bit写了类似的东西,但我认为我的情况有点复杂,因为我在3D中旋转: https://itunes.apple.com/ua/app/bit/id366236469?mt=8

基本上我所做的是设置一个定期调用某些方法的NSTimer.我只是采取方向和速度来创建一个旋转矩阵(正如我所说,3D有点肮脏:P),我将速度乘以一些小于1的数字,因此它会下降.乘以而不是减去的原因是,如果来自用户的旋转是硬的两倍,则不希望对象旋转两倍,因为等待我发现变得烦人.

至于确定车轮旋转的方向,只需将其存储在touchesEnded:withEvent:方法中,其中包含所有信息.既然你说你已经有了跟踪功能,只要用户手指向下,这应该是显而易见的.

我在3D中拥有的是:

// MyVIEw.h@interface MyVIEw : UIVIEw {    NSTimer *animationTimer;}- (voID) startAnimation;@end// MyAppDelegate.h@implementation MyAppDelegate- (voID) applicationDIDFinishLaunching:(UIApplication *)application {    [myVIEw startAnimation];}@end// MyVIEw.mGLfloat rotationMomentum = 0;GLfloat rotationDeltaX  = 0.0f;GLfloat rotationDeltaY  = 0.0f;@implementation MyVIEw- (voID)startAnimation {    animationTimer = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)((1.0 / 60.0) * animationFrameInterval) target:self selector:@selector(drawVIEw:) userInfo:nil repeats:TRUE];}- (voID) drawVIEw:(ID)sender {    addRotationByDegree(rotationMomentum);    rotationMomentum /= 1.05;    if (rotationMomentum < 0.1)        rotationMomentum = 0.1; // never stop rotating completely    [renderer render];}- (voID)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{}- (voID)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event{    UItouch *atouch = [touches anyObject];    CGPoint loc = [atouch locationInVIEw:self];    CGPoint prevloc = [atouch prevIoUsLocationInVIEw:self];    rotationDeltaX = loc.x - prevloc.x;    rotationDeltaY = loc.y - prevloc.y;    GLfloat distance = sqrt(rotationDeltaX*rotationDeltaX+rotationDeltaY*rotationDeltaY)/4;    rotationMomentum = distance;    addRotationByDegree(distance);    self->moved = TRUE;}- (voID)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event{}- (voID)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event{}

我省略了addRotationByDegree函数,但它做的是它使用全局变量rotationDeltaX和rotationDeltaY并将旋转矩阵应用于已存储的矩阵,然后保存结果.在你的例子中,你可能想要更简单的东西,比如(我现在假设只有X方向的运动才能转动轮子):

- (voID)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event{    UItouch *atouch = [touches anyObject];    CGPoint loc = [atouch locationInVIEw:self];    CGPoint prevloc = [atouch prevIoUsLocationInVIEw:self];    GLfloat distance = loc.x - prevloc.x;    rotationMomentum = distance;    addRotationByDegree(distance);    self->moved = TRUE;}voID addRotationByDegree(distance) {    angleOfWheel += distance; // probably need to divIDe the number with something reasonable here to have the spin be nicer}
总结

以上是内存溢出为你收集整理的objective-c – 试图通过UIRotationGestureController模拟动量/惯性全部内容,希望文章能够帮你解决objective-c – 试图通过UIRotationGestureController模拟动量/惯性所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1230817.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存