Reputation:
I have implemented speedometer i have one arrow image.i want to rotate 0 to 180 very smoothly.how it possible?
Upvotes: 1
Views: 721
Reputation: 19343
I just wrote an iPhone app that displays a spedometer. Here's the code that animates movement of the needle image:
CABasicAnimation *rotateAnimation = [CABasicAnimation animation];
rotateAnimation.keyPath = @"transform.rotation.z";
rotateAnimation.fromValue = [NSNumber numberWithFloat:DegreesToRadians( self.needleAngle )];
rotateAnimation.toValue = [NSNumber numberWithFloat:DegreesToRadians( newNeedleAngle )];
rotateAnimation.duration = 1.5;
rotateAnimation.removedOnCompletion = NO;
// leaves presentation layer in final state; preventing snap-back to original state
//
rotateAnimation.fillMode = kCAFillModeBoth;
rotateAnimation.repeatCount = 0;
rotateAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
// Add the animation to the selection layer. This causes it to begin animating.
//
[self.needleIV.layer addAnimation:rotateAnimation forKey:@"rotateAnimation"];
The "removedOnCompletion = NO" is important for the needle not to snap back to its starting position once the animation completes.
Upvotes: 0
Reputation: 1123
Does 'speedometer' means accelerometer?
Use UIAccelerometer to get accelerometer datas. Then get the actual movement data with filter. Then transform them to angles. These three steps can be found in UIAccelerometer's official examples.
Use CGAffineTransform to rotate the image view. This won't be hard I think. But I have no idea about this.
Upvotes: 1