【问题标题】:How to implement zoom-in/zoom-out effect on a cocos2d sprite image?如何在 cocos2d 精灵图像上实现放大/缩小效果?
【发布时间】:2010-05-18 20:23:39
【问题描述】:

我正在开发模块,我从照片库中选择图像并放入精灵中。我想为精灵图像实现放大/缩小效果,就像相册图像放大/缩小效果一样。有人可以指导我如何实现它吗?

我在某处看到的是,我必须在ccTouchBegan 中检测两个触摸事件,然后根据两个手指触摸事件值的距离将精灵的缩放大小调整为向上或向下。

谁能告诉我:

  • 如何检测ccTouchBegan 中的两个手指触摸值?
  • 如何允许用户触摸和放大/缩小精灵图像?请给我样品。我已经尝试了来自 this URL 的一些东西,但不能满足我的要求。

谢谢。

【问题讨论】:

  • 有什么帮助吗?我还没有使用正确的代码成功?

标签: iphone ios cocos2d-iphone


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    BOOL canPinch; 
    float oldScale;
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-25
      • 1970-01-01
      • 2011-01-26
      • 1970-01-01
      • 2013-08-10
      • 2012-01-14
      • 2020-12-02
      • 1970-01-01
      相关资源
      最近更新 更多