ios - Pinching/Panning a CCNode in Cocos2d 3.0 -


i want zoom in out ccnode pinching , panning screen. node has background large portionof shown on screen. node contains other sprites.

what have done first register uipinchgesturerecognizer

uipinchgesturerecognizer * pinchrecognizer = [[uipinchgesturerecognizer alloc] initwithtarget:self action:@selector(handlepinchfrom:)]; [[[ccdirector shareddirector] view] addgesturerecognizer: pinchrecognizer];

-(void)handlepinchfrom:(uipinchgesturerecognizer *) pinch {     if(pinch.state == uigesturerecognizerstateended) {         prevscale = 1;     }     else {         cgfloat dscale = [self scale] - prevscale + pinch.scale;         if(dscale > 0)         {             deltascale = dscale;         }         cgaffinetransform transform = cgaffinetransformscale(pinch.view.transform, deltascale, deltascale);         [pinch.view settransform: transform];  //        [_contentnode setscale:deltascale];          prevscale = pinch.scale;     } } 

the problem scalw whole uiview not ccnode. have tried setting scale of _contentnode.

**edit

i ave tried

- (void)handlepinchgesture:(uipinchgesturerecognizer*)apinchgesturerecognizer {  if (pinch.state == uigesturerecognizerstatebegan || pinch.state == uigesturerecognizerstatechanged) {     cgpoint midpoint = [pinch locationinview:[ccdirector shareddirector].view];     cgsize winsize = [ccdirector shareddirector].viewsize;     float x = midpoint.x/winsize.width;     float y = midpoint.y/winsize.height;     _contentnode.anchorpoint = cgpointmake(x, y);     float scale = [pinch scale];     _contentnode.scale *= scale;     pinch.scale = 1; } } 

but zoom bottom left of screen.

i had same problem. use ccscrollview, contains ccnode larger device screen. want scroll , zoom it, node shouldnt scroll out of screen, , scale smaller screen. so, create subclass of ccscrollview, handle pinch. has strange glitches, works fine @ all.

when pinch began set anchor point of node pinch center on node space. need change position of node proportional shift of anchor point, moving anchor point doesn't change nodes location on view:

   - (void)handlepinch:(uipinchgesturerecognizer*)recognizer {     if (recognizer.state == uigesturerecognizerstateended) {         _previousscale = self.contentnode.scale;     }     else if (recognizer.state == uigesturerecognizerstatebegan) {         float x = [recognizer locationinnode:self.contentnode].x / self.contentnode.contentsize.width;         float y = [recognizer locationinnode:self.contentnode].y / self.contentnode.contentsize.height;          float positionx = self.contentnode.position.x + self.contentnode.boundingbox.size.width * (x - self.contentnode.anchorpoint.x);         float positiony = self.contentnode.position.y + self.contentnode.boundingbox.size.height * (y - self.contentnode.anchorpoint.y);          self.contentnode.anchorpoint = ccp(x, y);         self.contentnode.position = ccp(positionx, positiony);     }     else {         cgfloat scale = _previousscale * recognizer.scale;          if (scale >= maxscale) {             self.contentnode.scale = maxscale;         }         else if (scale <= [self minscale]) {             self.contentnode.scale = [self minscale];         }         else {             self.contentnode.scale = scale;         }     } } 

also need change ccscrollview min , max scroll, node never scroll out of view. default anchor point (0,1), need shift min , max scroll proportional new anchor point.

- (float) maxscrollx {     if (!self.contentnode) return 0;      float maxscroll = self.contentnode.boundingbox.size.width - self.contentsizeinpoints.width;     if (maxscroll < 0) maxscroll = 0;      return maxscroll - self.contentnode.boundingbox.size.width * self.contentnode.anchorpoint.x; }  - (float) maxscrolly {     if (!self.contentnode) return 0;      float maxscroll = self.contentnode.boundingbox.size.height - self.contentsizeinpoints.height;     if (maxscroll < 0) maxscroll = 0;      return maxscroll - self.contentnode.boundingbox.size.height * (1 - self.contentnode.anchorpoint.y); }  - (float) minscrollx {     float minscroll = [super minscrollx];      return minscroll - self.contentnode.boundingbox.size.width * self.contentnode.anchorpoint.x; }  - (float) minscrolly {     float minscroll = [super minscrolly];     return minscroll - self.contentnode.boundingbox.size.height * (1 - self.contentnode.anchorpoint.y); } 

uigesturerecognizerstateended doesn't have locationinnode: method, added category. return touch location on node space:

#import "uigesturerecognizer+locationinnode.h"  @implementation uigesturerecognizer (locationinnode)  - (cgpoint) locationinnode:(ccnode*) node {     ccdirector* dir = [ccdirector shareddirector];      cgpoint touchlocation = [self locationinview: [self view]];     touchlocation = [dir converttogl: touchlocation];     return [node converttonodespace:touchlocation]; }  - (cgpoint) locationinworld {     ccdirector* dir = [ccdirector shareddirector];      cgpoint touchlocation = [self locationinview: [self view]];     return [dir converttogl: touchlocation]; }  @end 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -