OStack程序员社区-中国程序员成长平台

标题: ios - 在捏合手势的中心发出缩放层 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 14:58
标题: ios - 在捏合手势的中心发出缩放层

我目前在图层中有一个 map (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) scaleCGFloat ) newScale atCenterCGPoint)中心。有什么原因可能无法正常发生吗?另外,如何将夹点的中心位置转换为我的场景/图层的坐标系?



Best Answer-推荐答案


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

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

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

关于ios - 在捏合手势的中心发出缩放层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19049545/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4