Reputation: 119
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];
}
Upvotes: 0
Views: 783
Reputation: 27147
I believe your problem here is that, as WWDC 2011 Session 421 - Core Animation Essentials @~ 26:00 says, "Explicit animations do not effect the model values of your layer hierarchy".
Basically, in addition to using Core Animation to animate a rotation you need to change the property on the model object (UIView or CALayer) as well. So that when the animation finishes and the view renders normally again it is at the correct position and at the correct rotation.
Likely something like:
_coasterImageView.view.transform = CGAffineTransform... //rotation with toValue
If you are going to be doing much work with Core Animation I highly suggest you watch the video.
Upvotes: 1