【问题标题】:Issue scaling layer at the center of a pinch gesture在捏合手势的中心发出缩放层
【发布时间】:2013-09-27 11:25:21
【问题描述】:

我目前在图层中有一个地图(tilemap),我想使用以下代码进行平移/缩放:

- (void) pinchGestureUpdated: (UIPinchGestureRecognizer *) recognizer {

    if([recognizer state] == UIGestureRecognizerStateBegan) {

            _lastScale = [recognizer scale];

            CGPoint touchLocation1 = [recognizer locationOfTouch:0 inView:recognizer.view];
            CGPoint touchLocation2 = [recognizer locationOfTouch:1 inView:recognizer.view];

            CGPoint centerGL = [[CCDirector sharedDirector] convertToGL: ccpMidpoint(touchLocation1, touchLocation2)];
            _pinchCenter = [self convertToNodeSpace:centerGL];
    }

    else if ([recognizer state] == UIGestureRecognizerStateChanged) {

//        NSLog(@"%d", recognizer.scale);

        CGFloat newDeltaScale = 1 -  (_lastScale - [recognizer scale]);
        newDeltaScale = MIN(newDeltaScale, kMaxScale / self.scale);
        newDeltaScale = MAX(newDeltaScale, kMinScale / self.scale);

        CGFloat newScale = self.scale * newDeltaScale;

        //self.scale = newScale;
        [self scale: newScale atCenter:_pinchCenter];

        _lastScale = [recognizer scale];
    }
}

- (void) scale: (CGFloat) newScale atCenter: (CGPoint) center {

    CGPoint oldCenterPoint = ccp(center.x * self.scale, center.y * self.scale);

    // Set the scale.
    self.scale = newScale;

    // Get the new center point.
    CGPoint newCenterPoint = ccp(center.x * self.scale, center.y * self.scale);

    // Then calculate the delta.
    CGPoint centerPointDelta  = ccpSub(oldCenterPoint, newCenterPoint);

    // Now adjust your layer by the delta.
    self.position = ccpAdd(self.position, centerPointDelta);
}

我的问题是缩放没有在夹点的中心生效,即使我试图在通过这种方法放大的同时改变它:(void) scale: (CGFloat) newScale atCenter: (CGPoint) center。有什么原因可能无法正常发生吗?另外,如何将夹点的中心位置转换为我的场景/图层的坐标系?

【问题讨论】:

    标签: ios cocos2d-iphone scaling uipinchgesturerecognizer


    【解决方案1】:

    我的方法实际上一切都很好。我遇到的问题是anchor point 层与我定义的地图不同,后者在缩放期间引入了偏移。我必须将两个锚都设置为ccp(0,0)

    捏合手势中心的屏幕坐标到图层的转换是正确的,使用UIKItgesture recognizers时可以通过以下说明实现:

    CGPoint centerGL = [[CCDirector sharedDirector] convertToGL: ccpMidpoint(touchLocation1, touchLocation2)];
    _pinchCenter = [self convertToNodeSpace:centerGL];
    

    【讨论】:

      【解决方案2】:

      首先,你不能这样做 ([recognizer state] == UIGestureRecognizerStateBegan) 因为 state 是一个位域!所以你必须这样做:

      ([识别器状态] & UIGestureRecognizerStateBegan)

      捏合的中心位置基本上在屏幕上的坐标中。至于如何将其转换为您自己的坐标系,您需要弄清楚设备屏幕上的边界是在手势开始时显示的场景/图层的一部分。这将是 10,10 x 200,200 之类的坐标,代表屏幕的像素网格。然后你必须弄清楚,在你自己的应用场景/图层的坐标系中,10,10 映射到什么,以及 200,200 映射到什么。从那里,您可以导出一个应用于捏合手势中心的屏幕坐标的因子,这会将捏合手势的屏幕坐标转换为场景/图层坐标。

      您尝试做的事情很棘手,因为当您缩放场景/图层时,您将围绕不在视图中心的点进行缩放。我敢肯定,如果您在其中一个与地图相关的应用程序中查看 Apple 的一些示例代码,您可能会找到一些具有这种捏缩放的方法示例。

      我希望这会有所帮助。

      【讨论】:

      • 实际上不需要你对位域的要求,因为每个状态都是独占的
      • 我将屏幕坐标转换为图层坐标的逻辑也包含在我的代码 sn-p 中。
      猜你喜欢
      • 2018-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-12
      • 2011-10-17
      • 1970-01-01
      • 2018-04-22
      相关资源
      最近更新 更多