I have an image that i can rotate using a rotation gesture but i want to continue rotating the image based on the velocity of the rotation.
Its rotating after i have released my fingers but when it stops it doesn't stay at the rotated angle. It sets the rotation angle to the last angle after the finger has been released.
- (void)rotateCoaster:(UIRotationGestureRecognizer *)recognizer {
if(recognizer.numberOfTouches >= 2) {
twoTouchesDetected = YES;
}
else {
twoTouchesDetected = NO;
}
if(recognizer.state == UIGestureRecognizerStateBegan ||
recognizer.state == UIGestureRecognizerStateChanged)
{
recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform,
recognizer.rotation);
_player.rotationVelocity = recognizer.velocity;
_player.rotation = recognizer.rotation;
[recognizer setRotation:0];
}
if( !twoTouchesDetected ) {
[self rotationCalculations:nil];
rotating = YES;
}
if( recognizer.state == UIGestureRecognizerStateEnded ||
recognizer.state == UIGestureRecognizerStateCancelled ) {
[self dropDownCoaster];
}
_player.rotationVelocity = recognizer.velocity;
}
- (void)rotationCalculations:(NSTimer *) dt {
// dTime += dt.timeInterval;
[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat:2.0] forKey:kCATransactionAnimationDuration];
CABasicAnimation *animation;
animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
// animation.fromValue = [NSNumber numberWithFloat:0.0];
// animation.toValue = [NSNumber numberWithFloat:_player.rotationVelocity * M_PI];
animation.fromValue = nil;
animation.toValue = nil;
animation.byValue = [NSNumber numberWithFloat:_player.rotationVelocity * M_PI];
animation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseOut];
animation.delegate = self;
[_coasterImageView.layer addAnimation:animation forKey:@"rotationAnimation"];
[CATransaction commit];
}