简体   繁体   English

iPhone Cocos2D - 手指旋转精灵

[英]iPhone Cocos2D - finger rotating a sprite

I have subclassed CCSprite class and added a UIRotationGestureRecognizer to it. 我已经将CCSprite类子类化,并为其添加了UIRotationGestureRecognizer。 So, in my init method I have this 所以,在我的init方法中,我有这个

UIRotationGestureRecognizer *rot = [[[UIRotationGestureRecognizer alloc]
                         initWithTarget:self action:@selector(handleRotation:)] autorelease];
[rot setDelegate:self];
[[[CCDirector sharedDirector] openGLView] addGestureRecognizer:rot];

and then I have the method 然后我有了方法

- (void)handleRotation:(UIRotationGestureRecognizer *)recognizer {

    float rotation = [recognizer rotation];
    self.rotation += rotation;
}

it works perfectly but it has a huge lag between the actual gesture and the rotation itself. 它工作得很好,但它在实际手势和旋转本身之间存在巨大的滞后。 I would say almost 0.5 seconds between the gesture and the sprite response. 我会说手势和精灵响应之间差不多0.5秒。

How do I solve that? 我该如何解决? thanks. 谢谢。


NOTE: After the first comment, I have added two more recognizers to the sprite: UIPinchGestureRecognizer and UIPanGestureRecognizer. 注意:在第一个评论之后,我为精灵添加了两个识别器:UIPinchGestureRecognizer和UIPanGestureRecognizer。 I have also added the delegate method shouldRecognizeSimultaneouslyWithGestureRecognizer and set that to YES. 我还添加了委托方法shouldRecognizeSimultaneouslyWithGestureRecognizer并将其设置为YES。

After doing this and checking, pinch and pan gestures are fast as hell. 在做完这个并且检查之后,捏合和平移手势很快就像地狱一样。 On the other hand, rotation continues slow. 另一方面,旋转继续缓慢。 There was not reduction on the rotation speed by adding these two other gestures recognizer. 通过添加这两个其他手势识别器,没有降低旋转速度。 The other two respond fluid and fast, UIRotationGestureRecognizer is slow. 另外两个响应流畅而快速,UIRotationGestureRecognizer很慢。

Gesture rotation is in radians while Cocos2D rotation is in degrees. 手势旋转以弧度为单位,而Cocos2D旋转以度为单位。 So you need to convert this as follows: 所以你需要将其转换如下:

- (void)handleRotation:(UIRotationGestureRecognizer *)recognizer 
{
    float rotation = CC_RADIANS_TO_DEGREES([recognizer rotation]);
    self.rotation += rotation;
}

You can also save yourself these troubles and use Kobold2D, which not only adds an easy to use interface for gestures (and other input types) to Cocos2D but also converts the values accordingly to Cocos2D view coordinates and degrees. 你也可以节省这些麻烦并使用Kobold2D,它不仅为Cocos2D增加了一个易于使用的手势 (和其他输入类型) 界面,而且还相应地将值转换为Cocos2D视图坐标和度数。 You'll never have to think about converting values again. 你永远不必考虑再次转换价值观。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM