iPhone:使用touchesBegan、touchesMoved和touchesEnded平移移动图像

发布于 2024-09-25 22:11:35 字数 1664 浏览 2 评论 0原文

我的视图中有一个图像,其中包含 9x9 网格。我想使用平移移动沿着网格移动对象,该网格由另一个数组(9)内的列数组(9)组成。图像应在网格中从一个正方形移动到另一个正方形。下面的代码是我到目前为止所拥有的。问题是图像一次跳动 3 - 4 个方格。它太敏感了。任何人都可以阐明原因并就如何解决此敏感性问题提出一些建议吗?

有效的代码:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    gestureStartPoint = [touch locationInView:self.view];

    // Do not move if touch is off the grid
    if (gestureStartPoint.x < 0 || 
        gestureStartPoint.x > 450 || 
        gestureStartPoint.y < 0 || 
        gestureStartPoint.y > 450) {
        canMove = NO;
    }else {
        canMove = YES;
    }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint currentPosition = [touch locationInView:self.view];   

    double thresholdX = 50;

    if (canMove) { 
        deltaX = (int)(currentPosition.x - gestureStartPoint.x); 
        if (deltaX > thresholdX) {
            deltaX = 0; 
            if([self blockCanMoveXDir:1]){
                [cBlock movX:1];
            }
            // Resets start point 
            gestureStartPoint = currentPosition; 
            NSLog(@"-------------> Block Moved"); 
        } else if(deltaX < 0 && fabs(deltaX) > thresholdX) {
            deltaX = 0; 
            if([self blockCanMoveXDir:-1]){
                [cBlock movX:-1];
            }
            gestureStartPoint = currentPosition; 
        }
    }
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    canMove = NO;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    canMove = NO;
}

I have an image in a view which contains a 9x9 grid. I want to move the object along the grid, which is made up of a column array(9) inside another array(9), using a pan movement. The image should move square to square in the grid. The code below is what I have so far. The problem is the image jumps 3 - 4 squares at a time. It is much too sensitive. Can anyone shed some light as to why and make some suggestions as to what to do to fix this sensitivity issue issue?

Code which works:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    gestureStartPoint = [touch locationInView:self.view];

    // Do not move if touch is off the grid
    if (gestureStartPoint.x < 0 || 
        gestureStartPoint.x > 450 || 
        gestureStartPoint.y < 0 || 
        gestureStartPoint.y > 450) {
        canMove = NO;
    }else {
        canMove = YES;
    }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint currentPosition = [touch locationInView:self.view];   

    double thresholdX = 50;

    if (canMove) { 
        deltaX = (int)(currentPosition.x - gestureStartPoint.x); 
        if (deltaX > thresholdX) {
            deltaX = 0; 
            if([self blockCanMoveXDir:1]){
                [cBlock movX:1];
            }
            // Resets start point 
            gestureStartPoint = currentPosition; 
            NSLog(@"-------------> Block Moved"); 
        } else if(deltaX < 0 && fabs(deltaX) > thresholdX) {
            deltaX = 0; 
            if([self blockCanMoveXDir:-1]){
                [cBlock movX:-1];
            }
            gestureStartPoint = currentPosition; 
        }
    }
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    canMove = NO;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    canMove = NO;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一紙繁鸢 2024-10-02 22:11:35

我认为在 if (canMove) { 下面,您应该累积移动量。让我解释一下,您应该以这种方式计算运动的绝对 deltaX:

deltaX = currentPosition.x - gestureStartPoint.x;

其中 deltaX 是一个类变量。当该值大于阈值时,您将进行一格移动。调整该阈值可以改变灵敏度。
当然,您还必须考虑 Y 分量。

I think that below the if (canMove) { you should accumulate movements. Let me explaing, you should compute the absolute deltaX of the movement in this way:

deltaX = currentPosition.x - gestureStartPoint.x;

where deltaX is a class variable. When this value is greater than a threshold then you will do a one-block movement. Adjusting that threshold you can change sensitivity.
Of course you have also to take into account the Y component.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文