How to animate the pendulum of a clock where the pendulum starts in the 6 o'clock position?
I try to animate the motion of a pendulum which is starting at the 6 o'clock position, then swings a few times and then stops again in the 6 o'clock position.
I already know how to change the anchor point of a layer and how to rotate it.
I have a method to rotate a layer.
- (void)spinLayer:(CALayer *)inLayer duration:(CFTimeInterval)inDuration direction:(int)direction forPartsOfPi:(float) degrees withReverse:(bool) reverse andRepeatCount:(float) repeatCount;
I thought it might be best to split the movement of the pendulum in 3 "swings" The first swing is from the starting position to lets say 9 o'clock position. So this is the first time i call the Method.
[self spinLayer:layer duration:0.4 direction:SPIN_CLOCK_WISE forPartsOfPi:M_PI/2 withReverse:NO andRepeatCount:0];
Then, in the animationDidFinish Delegate i call the method once again to let it do the several swings. For the rotation of the Pendulum is now double the length of the first swing i call:
[self spinLayer:layer duration:0.8 direction:SPIN_COUNTERCLOCK_WISE forPartsOfPi:M_PI withReverse:YES andRepeatCount:4];
after that i want the pendulum to return to 6 o'clock position so i call:
[self spinLayer:layer duration:0.4 direction:SPIN_COUNTERCLOCK_WISE forPartsOfPi:M_PI/2 w开发者_如何学运维ithReverse:NO andRepeatCount:0];
Here is the implementation of my Method:
- (void)spinLayer:(CALayer *)inLayer duration:(CFTimeInterval)inDuration direction:(int)direction forPartsOfPi:(float) degrees withReverse:(bool) reverse andRepeatCount:(float) repeatCount
{
CABasicAnimation* rotationAnimation;
// Rotate about the z axis
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
// Rotate 180 degress, in direction specified
rotationAnimation.toValue = [NSNumber numberWithFloat: degrees * direction];
// Perform the rotation over this many seconds
rotationAnimation.duration = inDuration;
rotationAnimation.autoreverses = reverse;
rotationAnimation.fillMode = kCAFillModeForwards;
rotationAnimation.removedOnCompletion = FALSE;
rotationAnimation.repeatCount = repeatCount;
rotationAnimation.delegate = self;
// Set the pacing of the animation
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
// Add animation to the layer and make it so
[inLayer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}
So the problem is, that the second swing doesn't start at the end of the first swing but on the 6 o'clock position.
Can anybody set me straight how to do this?
Thx in advance
Mav
The animation starts from the value of the model layer's property. The animation only affects the presentation layer. So after the first animation is done, the model layer still points in the 6 o'clock orientation.
What you should do is in your spinLayer
method, set the layer's transform to the final orientation transform:
layer.transform = CATransform3DMakeRotation(radians, 0, 0, 1);
精彩评论