2014-06-12 1 views
0

지금은 iOS 용 Cocos2D 게임에서 핀치 - 줌 기능을 구현하려고하는데 실제로 이상한 동작이 발생합니다. 내 목표는 UIPinchGestureRecognizer에 대한 핸들러를 사용하여 플레이어가 화면을 집어 넣을 때 게임 레벨을 나타내는 CCNode 중 하나를 확장하는 것입니다. 이것은 확대의 효과가 있습니다.핀치 확대를위한 레이어의 앵커 포인트 설정이 예상대로 작동하지 않습니다.

문제는 앵커를 .5, .5 (레벨 CCNode의 중심)와 같은 임의의 값으로 설정하면 레벨 중앙에서 완벽하게 비율이 조정되지만, 플레이어의 시야 가운데. 내가 앵커를 설정할 때 나는 0.5, 0.5에 기준점을 설정하고 잘 보이는 CCLog을 사용하여 계산 된 고정 점 (areaLocationX, areaLocationY)를 인쇄하지만 경우

- (void) handlePinchFrom:(UIPinchGestureRecognizer*) recognizer 
{ 
if(recognizer.state == UIGestureRecognizerStateEnded) 
{ 
    _isScaling = false; 
    _prevScale = 1.0; 
} 
else 
{ 
    _isScaling = true; 
    float deltaScale = 1.0 - _prevScale + recognizer.scale; 

    // Obtain the center of the camera. 
    CGPoint center = CGPointMake(self.contentSize.width/2, self.contentSize.height/2); 
    CGPoint worldPoint = [self convertToWorldSpace:center]; 
    CGPoint areaPoint = [_area convertToNodeSpace:worldPoint]; 

    // Now set anchor point to where areaPoint is relative to the whole _area contentsize 
    float areaLocationX = areaPoint.x/_area.contentSize.width; 
    float areaLocationY = areaPoint.y/_area.contentSize.height; 

    [_area moveDebugParticle:areaPoint]; 
    [_area setAnchorPoint:CGPointMake(areaLocationX, areaLocationY)]; 

    if (_area.scale*deltaScale <= ZOOM_RADAR_THRESHOLD) 
    { 
     _area.scale = ZOOM_RADAR_THRESHOLD; 
    } 
    else if (_area.scale*deltaScale >= ZOOM_MAX) 
    { 
     _area.scale = ZOOM_MAX; 
    } 
    else 
    { 
     // First set the anchor point. 
     _area.scale *= deltaScale; 
    } 

    _prevScale = recognizer.scale; 
} 
} 

: 여기처럼 그 모습입니다 이 값을 가리키면 레이어가 제어 범위를 벗어나서 플레이어의 뷰를 완전히 벗어납니다. 앵커 포인트는 (-80, 10)과 같은 미친 값을 취합니다. 일반적으로 어느 좌표에 대해서도 0에서 1의 범위에 비교적 가깝습니다.

이런 종류의 동작을 일으키는 원인은 무엇입니까?

+0

예상되는 콘텐츠 크기는 실제로 있습니까? 장면을 설정 한 후에 노드의 내용 크기를 설정해야하는 경우가 많습니다. –

+0

앱을 디버깅 할 때 정확한 콘텐츠 크기가있는 것 같습니다. _area objects init 메소드에서 컨텐츠 크기를 설정했습니다. – user3693376

+0

나는 그것을 취한다 _area는 자기 자신의 아이인가? 자기 자신을 위해 세계 공간으로 변환하는 대신 노드 공간으로 변환 해보십시오. 또는이를 건너 뛰고 센터를 데리고 노드 영역으로 변환하기 위해 _area로 직접 보낼 수도 있습니다. –

답변

0

좋아, 내가 해결 한 것처럼 보입니다. 나는 처음부터 앵커 포인트를 설정하는 것보다는 스케일링 중 앵커 포인트를 계속 움직이고있었습니다. 그 결과는 매끄럽고 예상되는 것보다는 오히려 엉뚱한 스케일링이었습니다. 해결 된 코드는 다음과 같습니다.

- (void) handlePinchFrom:(UIPinchGestureRecognizer*) recognizer 
{ 
if(recognizer.state == UIGestureRecognizerStateEnded) 
{ 
    _isScaling = false; 
    _prevScale = 1.0; 
} 
else 
{ 
    if (!_isScaling) 
    { 

     // Obtain the center of the camera. 
     CGPoint center = CGPointMake(self.contentSize.width/2, self.contentSize.height/2); 
     CGPoint areaPoint = [_area convertToNodeSpace:center]; 

     // Now set anchor point to where areaPoint is relative to the whole _area contentsize 
     float anchorLocationX = areaPoint.x/_area.contentSize.width; 
     float anchorLocationY = areaPoint.y/_area.contentSize.height; 

     [_area moveDebugParticle:areaPoint]; 
     [_area setAnchorPoint:CGPointMake(anchorLocationX, anchorLocationY)]; 
     CCLOG(@"Anchor Point: (%f, %f)", anchorLocationX, anchorLocationY); 
    } 

    _isScaling = true; 
    float deltaScale = 1.0 - _prevScale + recognizer.scale; 

    if (_area.scale*deltaScale <= ZOOM_RADAR_THRESHOLD) 
    { 
     _area.scale = ZOOM_RADAR_THRESHOLD; 
    } 
    else if (_area.scale*deltaScale >= ZOOM_MAX) 
    { 
     _area.scale = ZOOM_MAX; 
    } 
    else 
    { 
     _area.scale *= deltaScale; 
    } 

    _prevScale = recognizer.scale; 
} 
}