2011-12-23 4 views
1

ピンチを実装してparallax scrolling nodeにズームするにはどうすればよいですか?ここでピンチしてCCParallaxNodeで

+1

...それが誰かを助けることを願っていますが、スクロールやピンチについてのご質問ですが、か、以前のを行う方法を知っていますが、どのように知りませんそれを視差層に適用しますか? – LearnCocos2D

+0

@ LearnCocos2D:それを視差ノードレイヤーに適用する方法を知らない –

答えて

1

は、ソリューションです:

- (void)handlePitchZoom:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch* touch1 = [[[event allTouches] allObjects] objectAtIndex:0]; 
    UITouch* touch2 = [[[event allTouches] allObjects] objectAtIndex:1]; 

    // calculate scale value 
    double prevDistance = ccpDistance([touch1 previousLocationInView:[touch1 view]], [touch2 previousLocationInView:[touch2 view]]); 
    double newDistance = ccpDistance([touch1 locationInView:[touch1 view]], [touch2 locationInView:[touch2 view]]); 

    CGFloat relation = newDistance/prevDistance; 
    CGFloat newScale = self.scale * relation; 

    if ((newScale >= minScale) && (newScale <= maxScale)) { 

     CGPoint touch1Location = [parallaxNode convertTouchToNodeSpace:touch1]; 
     CGPoint touch2Location = [parallaxNode convertTouchToNodeSpace:touch2]; 

     // calculate center point between two touches 
     CGPoint centerPoint = ccpMidpoint(touch1Location, touch2Location); 

     // store center point location (ScrollableView space) 
     CGPoint centerPointInParentNodeSpace = [self convertPoint:centerPoint fromNode:parallaxNode]; 
     CGPoint oldPoint = ccp(centerPointInParentNodeSpace.x * (self.scale), centerPointInParentNodeSpace.y * (self.scale)); 
     self.scale = newScale; 

     CGPoint newPoint = ccp(centerPointInParentNodeSpace.x * (self.scale), centerPointInParentNodeSpace.y * (self.scale)); 
     CGPoint diff = ccp(oldPoint.x - newPoint.x , oldPoint.y - newPoint.y); 

     [parallaxNode setPosition:ccp(parallaxNode.position.x + (diff.x*(1/self.scale)), parallaxNode.position.y + (diff.y*(1/self.scale)))]; 
    } 

関連する問題