2011-06-07 79 views
2

有誰知道爲什麼它不會旋轉雪碧?UIGestureRecognizer不會旋轉雪碧 - Cocos2d

- (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])]; 

添加它轉動整個場景!請幫忙嗎?

謝謝!

回答

5

哥們

下面

是鏈接的用戶界面,工具包,但它會工作的優良的cocos2d以及 你需要採取協調系統

1)cocos2d的起源護理左下角

2)對於UIKit的原點是左上角

"Rotate image by Dragging"

何pe這個幫助你 好運

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]; 
} 
}