Animating a wheel in iOS [ Moving it + or – degrees ] in a circle

Consider you a got a wheel, which you need to move plus or minus degrees. You can do it using gesture recognizers and tap recognizer.

The code is as below:
[sourcecode lang=”objc”]
#import "dashViewController.h"
#import <QuartzCore/QuartzCore.h>
#define DegreesToRadians(x) (M_PI * x / 180.0) // read about degrees and radians here
// http://math.rice.edu/~pcmi/sphere/drg_txt.html
@interface dashViewController ()
{
UIImage *wheel;
UIImageView *wheelView;
UITapGestureRecognizer *wheelTapRecognizer;
UIFont *menuItemsFont;
NSArray* sections;
}
@property (nonatomic, strong) UIFont *menuItemsFont;
@end
@implementation dashViewController
@synthesize menuItemsFont;
– (void) imageTap:(UITapGestureRecognizer*) sender
{
if (sender.state == UIGestureRecognizerStateEnded)
{
CGPoint tappedPoint = [sender locationInView:sender.view];
if (sender.view.tag == 1) {
NSLog(@"%d %f %f", sender.view.tag, tappedPoint.x, tappedPoint.y);
wheelView.transform = CGAffineTransformRotate(wheelView.transform, DegreesToRadians(-1));
}
}
}
– (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
wheel = [UIImage imageNamed:@"wheel36.gif"]; // replace this with your image.
wheelView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 50, wheel.size.width, wheel.size.height)];
wheelView.image = wheel;
wheelView.userInteractionEnabled = YES;
[wheelView setTag:1];
[self.view setTag:0];
wheelTapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget: self
action: @selector(imageTap:)];
[wheelView addGestureRecognizer:wheelTapRecognizer];
[self.view addSubview:wheelView];
}
[/sourcecode]

In

Leave a Reply

Your email address will not be published. Required fields are marked *