Jonathan
Jonathan

Reputation: 2383

UIGestureRecognizer Will Not Rotate Sprite - Cocos2d

Does anyone know why it will not rotate the sprite?

- (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 is the sprite in [g setRotation:angleInRadians];

Then, when I add in this

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

It rotates the entire scene! Any help, please?

Thank You!

Upvotes: 2

Views: 393

Answers (2)

nur farazi
nur farazi

Reputation: 1207

in the init method pass the following code. this will create a gesture recognizer

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

this is the vent that will trigger

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

Upvotes: 1

NIKHIL
NIKHIL

Reputation: 2719

Dude

below is the link for the ui-kit but it will work fine for cocos2d as well You need to take care of the coordination system

1)For cocos2d origin is bottom left corner

2)For UIKit origin is upper left corner

"Rotate image by Dragging"

Hope This help You Good Luck

Upvotes: 5

Related Questions