I am developing module where-in I pick the image from photo library and put into a sprite. I want to implement zoom-in/zoom-out kind of effect for a spri开发者_JAVA技巧te image, same like camera album images zoom in/out effect. Could someone please guide me how do i implement it?
I see somewhere is that, I have to detect two touch events in ccTouchBegan
and then adjust the sprite's scale size to up or down based on the distance of two fingers touch event values.
Could someone please tell me:
- How do i detect two fingers touch values in
ccTouchBegan
? - How to allow to touch and zoom-in/out of sprite image by user? Please give me samples. I tried already some stuff from this URL, but doesn't work for my requirement.
Thank you.
It would be simpler to use gesture recognizer for zooming:
// on initializing your scene:
UIPinchGestureRecognizer* PinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget: self action: @selector (zoom:)];
[[[Director sharedDirector] openGLView] addGestureRecognizer: PinchGesture];
...
/// zoom callback:
-(void) zoom: (UIPinchGestureRecognizer*) gestureRecognizer
{
if (([gestureRecognizer state] == UIGestureRecognizerStateBegan) || ([gestureRecognizer state] == UIGestureRecognizerStateChanged))
yourSprite.scale = [gestureRecognizer scale];
}
1) First step you need create two variables.
BOOL canPinch;
float oldScale;
2) Use the brigadir's :) answer and add this in your init method
UIPinchGestureRecognizer* pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action: @selector (zoom:)];
[[[CCDirector sharedDirector] openGLView] addGestureRecognizer:pinchGesture];
3)
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSArray* allTouches = [[event allTouches] allObjects];
UITouch* touch = [touches anyObject];
CGPoint location = [self convertTouchToNodeSpace:touch];;
CGRect particularSpriteRect = CGRectMake(spriteToZoom.position.x-[spriteToZoom boundingBox].size.width/2, spriteToZoom.position.y-[spriteToZoom boundingBox].size.height/2, [spriteToZoom boundingBox].size.width, [spriteToZoom boundingBox].size.height);
if(CGRectContainsPoint(particularSpriteRect, location))
{
if ([allTouches count] == 2)
{
canPinch = YES;
return;
}
else if ([allTouches count] == 1)
{
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
CGPoint oldTouchLocation = [touch previousLocationInView:touch.view];
oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation];
oldTouchLocation = [self convertToNodeSpace:oldTouchLocation];
CGPoint translation = ccpSub(touchLocation, oldTouchLocation);
[self panForTranslation:translation];
}
}
canPinch = NO;
}
- (void)panForTranslation:(CGPoint)translation
{
CGPoint newPos = ccpAdd(spriteToZoom.position, translation);
spriteToZoom.position = newPos;
}
- (void)zoom: (UIPinchGestureRecognizer*) gestureRecognizer
{
if (canPinch)
{
if (([gestureRecognizer state] == UIGestureRecognizerStateBegan) || ([gestureRecognizer state] == UIGestureRecognizerStateChanged))
{
spriteToZoom.scale = oldScale + [gestureRecognizer scale]-(oldScale != 0 ? 1 : 0);
}
if ([gestureRecognizer state] == UIGestureRecognizerStateEnded)
{
oldScale = spriteToZoom.scale;
}
}
}
精彩评论