iPhone:使用touchesBegan、touchesMoved和touchesEnded平移移动图像
我的视图中有一个图像,其中包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为在
if (canMove) {
下面,您应该累积移动量。让我解释一下,您应该以这种方式计算运动的绝对 deltaX:其中 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: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.