首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在cocos2d精灵图像上实现放大/缩小效果?

如何在cocos2d精灵图像上实现放大/缩小效果?
EN

Stack Overflow用户
提问于 2010-05-18 20:23:39
回答 2查看 4.5K关注 0票数 0

我正在开发模块,在其中,我从照片库中选择图像,并将其放入精灵中。我想对精灵图像实现放大/缩小效果,就像相机相册图像放大/缩小效果一样。有人能指点我如何实施吗?

我发现,我必须在ccTouchBegan中检测两个触摸事件,然后根据两个手指触摸事件值的距离来调整精灵的缩放大小。

谁能告诉我:

  • 如何在ccTouchBegan中检测两个手指触摸值
  • 如何允许用户触摸和放大精灵图像?请给我样品。我已经尝试了一些来自这个URL的东西,但是不适合我的需求。

谢谢。

EN

回答 2

Stack Overflow用户

发布于 2012-05-07 08:34:26

使用手势识别器进行缩放会更简单:

代码语言:javascript
复制
    // 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
EN

Stack Overflow用户

发布于 2012-05-21 10:43:32

1)第一步,您需要创建两个变量。

代码语言:javascript
复制
BOOL canPinch; 
float oldScale;

2)使用brigadir的:)答案并将其添加到init方法中

代码语言:javascript
复制
UIPinchGestureRecognizer* pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action: @selector (zoom:)];
[[[CCDirector sharedDirector] openGLView] addGestureRecognizer:pinchGesture];

3)

代码语言:javascript
复制
- (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; 
        } 
    } 
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2860842

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档