2010-05-18 5 views
0

사진 라이브러리에서 이미지를 선택하여 스프라이트에 넣는 모듈을 개발 중입니다. 스프라이트 이미지에 줌인/​​줌 아웃 종류의 효과를 구현하고 싶습니다. 카메라 앨범 이미지의 확대/축소 효과와 같습니다. 누군가 나를 인도 해 주시겠습니까?cocos2d 스프라이트 이미지에 확대/축소 효과를 구현하는 방법은 무엇입니까?

어딘가에 나는 ccTouchBegan에서 두 번의 터치 이벤트를 감지하고 두 손가락 터치 이벤트 값의 거리를 기준으로 스프라이트의 눈금 크기를 위 또는 아래로 조정해야합니다.

은 누군가가 말해 주 시겠어요 :

  • 을 내가 두 손가락 ccTouchBegan에 값을 터치 감지 방법은?
  • 사용자가 스프라이트 이미지를 터치하거나 확대/축소하는 것을 허용하는 방법은 무엇입니까? 샘플주세요. this URL에서 이미 일부 항목을 시도했지만 제 요구 사항에 맞지 않습니다.

감사합니다.

+0

어떤 도움이 추가? 아직 올바른 코드로이 작업을 수행하지 못했습니까? – Getsy

답변

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

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; 
     } 
    } 
} 
관련 문제