3

我正在开发具有某些游戏领域的游戏,该游戏是在某些视图中绘制的。我的游戏场总是相对于他的中心绘制,所以我只是简单的捏

- (void)pinch:(UIPinchGestureRecognizer *)gesture
{
if ((gesture.state == UIGestureRecognizerStateChanged) ||
    (gesture.state == UIGestureRecognizerStateEnded)) {
    self.gameBoardGridView.scale *= gesture.scale; // adjust our scale

    gesture.scale = 1;  
}

和泛

-(void)pan:(UIPanGestureRecognizer *)recognizer
{
CGPoint translatedPoint = [recognizer translationInView:self.gameBoardGridView];


self.gameBoardGridView.gridCenter = CGPointMake(self.gameBoardGridView.gridCenter.x + translatedPoint.x, self.gameBoardGridView.gridCenter.y + translatedPoint.y);

[recognizer setTranslation:CGPointMake(0, 0) inView:self.gameBoardGridView];
}

但我需要其他类型的缩放。当您放下两根手指然后移动其中一根时,该区域会按应有的方式扩展(放大),但手指下方的像素不再位于手指下方。图像从图像的中心缩放,而不是两个手指之间的中点。

我尝试了很多示例,但没有像我想要的那样工作。最后我写了这个,它几乎是正确的,但不完全

- (void)pinch:(UIPinchGestureRecognizer *)gesture
{
CGFloat curScale = gesture.scale;

static CGPoint startPoint;

if (gesture.state == UIGestureRecognizerStateBegan) {
    startPoint = [gesture locationInView:self.gameBoardGridView];
}


if ((gesture.state == UIGestureRecognizerStateChanged) ||
    (gesture.state == UIGestureRecognizerStateEnded)) {
    self.gameBoardGridView.scale *= gesture.scale;

    CGFloat widthDelta = self.gameBoardGridView.gridWidth * fabs(gesture.scale - 1) / 3;
    CGFloat heightDelta = self.gameBoardGridView.gridHeight * fabs(gesture.scale - 1) / 3;


    gesture.scale = 1;

    CGPoint lastPoint = self.gameBoardGridView.gridCenter;

    const CGFloat epsilon = 5;

    CGFloat coef = 1.0;
    if(curScale < 1) {
        coef = -1.0;
    }

    if (startPoint.x + epsilon < self.view.frame.size.width / 2) {
            lastPoint.x += widthDelta*coef;
    }
    else if (startPoint.x - epsilon > self.view.frame.size.width / 2) {
            lastPoint.x -= widthDelta*coef;
    }

    if (startPoint.y + epsilon < self.view.frame.size.height / 2) {
            lastPoint.y += heightDelta*coef;
    }
    else if (startPoint.y - epsilon > self.view.frame.size.height / 2) {
            lastPoint.y -= heightDelta*coef;
    }


    self.gameBoardGridView.gridCenter = lastPoint;
}
}

可能还有其他方法可以做到这一点?

4

2 回答 2

0

您是否尝试过使用UIScrollView? 我做了类似的事情。我开始使用本教程,然后为我的应用添加了一些特定代码。

http://www.raywenderlich.com/10518/how-to-use-uiscrollview-to-scroll-and-zoom-content

于 2014-02-19T16:50:14.327 回答
0

缩放的中心是图像的锚点。您要做的就是将锚点更改为手指的中点,位置和位置。自己没有尝试过购买我在这里看到了完整的解决方案,也许它会对您有所帮助-

https://github.com/robertblackwood/CCPanZoomController

于 2014-02-19T14:40:53.427 回答