Implement looped movement animation with tap to cancel
- by Nader
Hi All;
My app is based around a grid and an image that moves within a grid that is contained within a scrollview.
I have an imageview that I am animating from one cell to another in time with a slow finger movement and recentering the scrollview. That is rather straight forward. I have also implement the ability to detect a swipe and therefore move the image all the way to the end of the grid and the uiscrollview recentering. I have even implemented the ability to detect a subsequent tap and freeze the swiped movement.
The issue with the swipe movement is that the UIScrollView will scroll all the way to the end before the Image reaches the end and so I have to wait for the image to arrive. Also, when I freeze the movement of the image, I have to re-align the image to a cell (which I can do).
I have come to the realization that I have to animate the image one cell at a time for swipes and recentering the uiscrollview before moving the image to the next cell.
I have attempted to implement this but I cannot come up with a solution that works or works properly. Can anyone suggest how I go about implementing this?
Even if you are able to put up code from a different example or sudo code, it would help a lot as I cannot workout how this should be done, should I be using selectors, a listener in delegates, I just simply lack the experience to solve this design pattern.
Here is some code:
Note that the sprite is an UIImageView
- (void)animateViewToPosition:(SpriteView *)sprite Position:(CGPoint)pos Duration:(CFTimeInterval)duration{
CGMutablePathRef traversePath = CGPathCreateMutable();
CGPathMoveToPoint(traversePath, NULL, sprite.center.x, sprite.center.y);
CGPathAddLineToPoint(traversePath, NULL, pos.x, pos.y);
CAKeyframeAnimation *traverseAnimation = [CAKeyframeAnimation animationWithKeyPath:kAnimatePosition];
traverseAnimation.duration = duration;
traverseAnimation.removedOnCompletion = YES;
traverseAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
traverseAnimation.delegate = sprite;
traverseAnimation.path = traversePath;
CGPathRelease(traversePath);
[sprite.layer addAnimation:traverseAnimation forKey:kAnimatePosition];
sprite.center = pos;
}