2
移動圖像我有一個圖像在視圖中包含9x9網格。我想使用平移運動將對象沿着由另一個數組(9)內的列數組(9)組成的網格移動。圖像應該在網格中正方形移動。下面的代碼是我到目前爲止。問題是圖像每次跳躍3-4個方塊。它太敏感了。任何人都可以闡明爲什麼,並提出一些建議,以解決此問題的靈敏度問題該怎麼做?iPhone:泛移動使用touchesBegan,touchesMoved和touchesEnd
其中工程代碼:
-(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;
}
我試過這個,它也移動得太快了。我認爲touchesMoved過於頻繁地被調用。 – 2010-10-01 19:44:51
這不是touchMoved被調用的頻率的問題。這只是一個閾值,試試這個: if(canMove){delta} =如果(deltaX>閾值X){ deltaX = 0; // TODO:移動一個塊 //重置起始點 gestureStartPoint = currentPosition; NSLog(@「-------------> Block Moved」); } ...您的代碼... – MaxFish 2010-10-01 20:25:01
這是非常接近。我想我從這裏得到它。非常感謝。 – 2010-10-01 20:42:58