首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用捏手势限制潘手势

用捏手势限制潘手势
EN

Stack Overflow用户
提问于 2014-05-27 21:53:45
回答 1查看 572关注 0票数 1

我正在创建一个游戏,在Spritekit,并使用潘手势与捏手势,以查看地图。目前,我限制捏手势的缩放到2.0的比例。我试图把潘的手势限制在屏幕的边界上,即使在缩放时也是如此。地图比屏幕大,我只希望用户能够在地图的外部边缘到达屏幕的外部边缘之前,甚至在缩放时也是如此。这里是我的业余方法,试图处理这种情况:

代码语言:javascript
复制
-(void) handlePanFrom:(UIPanGestureRecognizer*)recognizer {
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        [recognizer setTranslation:CGPointZero inView:recognizer.view];
    } else if (recognizer.state == UIGestureRecognizerStateChanged) {
        CGPoint translation = [recognizer translationInView:recognizer.view];
        translation = CGPointMake(-translation.x, translation.y);
        CGPoint desiredPos = CGPointSubtract(_mapNode.position, translation);
        NSLog(@"Moving map to x: %f y: %f", desiredPos.x, desiredPos.y);
        NSLog(@"Map node position x: %f y: %f", _mapNode.position.x, _mapNode.position.y);
        NSLog(@"Map scale is %f", mapScale);
        NSLog(@"Map size is x: %f y: %f", _mapNode.map.frame.size.width, _mapNode.map.frame.size.height);

        if (desiredPos.y <= (mapScale * 300) && desiredPos.y >= ((1/mapScale) * 200)) {
            _mapNode.position = CGPointMake(_mapNode.position.x, desiredPos.y);
            [recognizer setTranslation:CGPointZero inView:recognizer.view];
        }

        if (desiredPos.x <= (mapScale * 250) && desiredPos.x >= ((1/mapScale) * 77)) {
            _mapNode.position = CGPointMake(desiredPos.x, _mapNode.position.y);
            [recognizer setTranslation:CGPointZero inView:recognizer.view];
        }
    } else if (recognizer.state == UIGestureRecognizerStateEnded) {

    }
}

当缩放以获得节点的缩放时,我设置了一个标度iVar。我应该能够使用节点的大小(_mapNode.map是一个SKSpriteNode)和屏幕大小来获得我想要的摇摄。

我以为我可以做scale*xMax(1/scale)*xMin (也有y位置),但这似乎行不通。我希望那里没有硬数字(300、200等),并使用节点/屏幕/etc的大小来限制摇摄。任何帮助都是非常感谢的!谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-29 21:54:48

好吧,这就是我为将来的人做的参考。

我首先创建了两个iVars:一个用于节点的初始x位置,另一个用于初始y位置。然后,通过取:sizeOfNode * 0.5 * scale和减去screenSize * 0.5,计算出“可移动距离”。只要是desiredLocation <= (initialPos + movableDistance)desiredLocation >= (initialPos - moveableDistance),就可以更改节点的位置。你可以对x和y都这样做。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23899602

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档