UIGestureRecognizer不会旋转Sprite - Cocos2d

时间:2011-06-07 01:29:53

标签: iphone cocos2d-iphone

有谁知道为什么它不会旋转精灵?

- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];

        CGPoint center = CGPointMake(CGRectGetMidX([[touch view] bounds]), CGRectGetMidY([[touch view] bounds]));

        CGPoint firstLocation = [touch previousLocationInView:[touch view]];
        CGPoint location = [touch locationInView:[touch view]];

        CGPoint currentTouchPoint = [[CCDirector sharedDirector] convertToGL:location];
        CGPoint previousTouchPoint = [[CCDirector sharedDirector] convertToGL:firstLocation];

        CGPoint line2Start = currentTouchPoint;
        CGPoint line1Start = previousTouchPoint;
        CGPoint line2End = CGPointMake(center.x + (center.x - line2Start.x), center.y + (center.y - line2Start.y));
        CGPoint line1End = CGPointMake(center.x + (center.x - line1Start.x), center.y + (center.y - line1Start.y));

        CGFloat a = line1End.x - line1Start.x;
        CGFloat b = line1End.y - line1Start.y;
        CGFloat c = line2End.x - line2Start.x;
        CGFloat d = line2End.y - line2Start.y;

        CGFloat line1Slope = (line1End.y - line1Start.y) / (line1End.x - line1Start.x);
        CGFloat line2Slope = (line2End.y - line2Start.y) / (line2End.x - line2Start.x);

        CGFloat degs = acosf(((a*c) + (b*d)) / ((sqrt(a*a + b*b)) * (sqrt(c*c + d*d))));

        CGFloat angleInRadians = (line2Slope > line1Slope) ? degs : -degs;

        [g setRotation:angleInRadians];
}

- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
    UIGestureRecognizer *recognizer;
    [recognizer setState:UIGestureRecognizerStateEnded];
}

G是[g setRotation:angleInRadians]中的精灵;

然后,当我加入

[[touch view] setTransform:CGAffineTransformRotate([[touch view] transform], [grinder rotation])];

它旋转整个场景!有什么帮助吗?

谢谢!

2 个答案:

答案 0 :(得分:5)

多德

下面是ui-kit的链接,但它也适用于cocos2d 你需要照顾协调系统

1)对于cocos2d,原点是左下角

2)UIKit原点是左上角

"Rotate image by Dragging"

希望这有助于你 祝你好运

答案 1 :(得分:1)

在init方法中传递以下代码。这将创建一个手势识别器

UIRotationGestureRecognizer *rotationGesture = 
[[UIRotationGestureRecognizer alloc] initWithTarget:self
                                             action:@selector(handleRotate:)];
rotationGesture.delegate = self;
   [myImageView addGestureRecognizer:rotationGesture];
  [rotationGesture release];

这是将触发的通风口

- (void)handleRotate:(UIRotationGestureRecognizer *)recognizer {
 if(recognizer.state == UIGestureRecognizerStateBegan || 
 recognizer.state == UIGestureRecognizerStateChanged)
 {
recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, 
                                                    recognizer.rotation);
[recognizer setRotation:0];
 }
}