I want to move an image (cBlock) continuously with a gesture. The direction of the swipe gesture determines the direction of the movement. I am able to move, but not continuously. Although the finger is held down, the movement starts/stops. Can anyone see why?
I believe touchesMoved is called continuously, as the log statements appear to be firing off continuously. Also, this logic does not seem to account for the finger being held down while the direction becomes positive, then negative (ex. users pans right, then pans left without lifting finger.) Is there a proven solution for that case?
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
gestureStartPoint = [touch locationInView:gridView];
prevGesturePoint = gestureStartPoint;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:gridView];
CGFloat deltaX = currentPosition.x - prevGesturePoint.x;
CGFloat deltaY = currentPosition.y - prevGesturePoint.y;
if(running == YES){
if(fromTop || fromBottom){
if((currentPosition.x - prevGesturePoint.x) > 0) {
NSLog(@"swipe right.");
[cBlock movXSinglePixel:10]; // move 10px
} else if((currentPosition.x - prevGesturePoint.x) < 0) {
NSLog(@"swipe left.");
[cBlock movXSinglePixel:-10]; //move -10px
}
}
}
[container setNeedsDisplay];
prevGesturePoint = currentPosition;
}
- (void)movXSinglePixel:(int)dir{
[UIView beginAnimations:@"center" context:nil];
[UIView setAnimationDuration:0.01];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
if(tX + dir > 8 || tX + dir < 0){
// Attempt to move out of grid bounds.
return;
}
CGPoint center = [self center];
center.x += (dir * 1.0f);
[self setCenter:center];
tX += dir;
[UIView commitAnimations];
}