Hi there. I've spent a few hours reading up about this on various forums and am just a little unclear on a couple of things. I'm basically trying to implement a golf-game style swingometer, where the user holds a button to start a swing, releases at the apex to determine power, at which point the 'swing' reverses direction, and the user taps again on the way down to set the accuracy.
Rather than having a bar that fills up, I would like to use a pendulum that swings from left to right. The pendulum leaves a mark where it is first tapped (power) and another mark where it is tapped for the second time (accuracy).
Just to make the pendulum move, i.e. rotate the view, I tried the following:
Code:
- (void)viewDidLoad {
pendulum.image = [UIImage imageNamed:@"pendulum.png"];
[self swingPendulum];
}
- (void) swingPendulum;
{
CABasicAnimation *rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0]; ///* full rotation*/ * roation * duration ];
rotationAnimation.duration = 2.0;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = 1.0;
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[pendulum.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}
This works fine so far, but I've also seen a different approach, as I've done below:
Code:
- (void)swingPendulum {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2];
CGAffineTransform transform = CGAffineTransformMakeRotation(degreesToRadian(180));
pendulum.transform = CGAffineTransformScale(transform, 1, 1);
[UIView commitAnimations];
}
Could anyone tell me which approach is better and why?
My question really, is how best to approach the solution to this problem as a whole. Is a UIView rotation animation the best bet? I don't need to calculate accurately how far from the correct point the pendulum is stopped, simply if it's too strong, too weak or just right. I can presumably do this by just taking the angle it's at when the user hits the button. I.e. < 170 degrees = too weak, > 170 and < 190 = just right and > 190 = too strong. But then I need to extract the current image's transformation angle from somewhere... Is there an easy way to do this?
I hope this question isn't too vague. I've been messing around for so long on this my brain is a bit of a muddle, so I guess I'm wondering what the pros would do, before I commit several more hours wandering around in the dark.
Thanks for any help - very much appreciated
Michael