gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 21:21:55 25 4
gpt4 key购买 nike

我的 View 中有一个图像,其中包含 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;
}

最佳答案

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

deltaX = currentPosition.x - gestureStartPoint.x;

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

关于iPhone:使用touchesBegan、touchesMoved和touchesEnded平移移动图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3841977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com