#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize recordedRotation;
- (void)viewDidLoad {
image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
image.image = [UIImage imageNamed:@"2.jpg"];
image.userInteractionEnabled = YES;
image.contentMode = UIViewContentModeScaleAspectFit;
image.autoresizingMask =
UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self.view addSubview:image];
[image release];
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(onRotation:)];
[image addGestureRecognizer:rotation];
[rotation release];
deltaAngle = atan2(image.frame.origin.y+image.frame.size.height - image.center.y,
image.frame.origin.x+image.frame.size.width - image.center.x);
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)onRotation:(UIGestureRecognizer *)_rotationGes {
float rotation1 = self.recordedRotation - ((UIRotationGestureRecognizer*)_rotationGes).rotation;//recordedRotation自己定义的double类型变量
NSLog(@"rotation1 is %f rotation is %f",rotation1,((UIRotationGestureRecognizer*)_rotationGes).rotation);
image.transform = CGAffineTransformMakeRotation(- rotation1 ); //self 是view
if ([(UIRotationGestureRecognizer*)_rotationGes state] == UIGestureRecognizerStateEnded) {
_lastRotation = 0.0f;
self.recordedRotation = rotation1; //获取了 当前旋转的弧度值
NSLog(@"recordedRotation is %f",recordedRotation);
[self resetImageView:(UIRotationGestureRecognizer*)_rotationGes];
}
image.contentMode = UIViewContentModeScaleAspectFit;
}
//该函数算法提供当旋转角度不超过1/8度角将复位,超过将直接去旋转的位置
- (void)resetImageView:(UIRotationGestureRecognizer *)_rotaionGes {
//rotaion顺时针为正逆时针为负
CGFloat rotaion = _rotaionGes.rotation;
CGFloat engle = rotaion / (M_PI * 2);
CGFloat eng = (engle - (int)engle) * (M_PI * 2);
eng = fabs(eng);
CGFloat fabsRotain = fabs(rotaion);
NSLog(@"recordedRotation1 is %f eng is %f",recordedRotation,eng);
if (fabsRotain > 0 && fabsRotain <= M_PI / 8) {
if (rotaion > 0)
self.recordedRotation = self.recordedRotation + eng;
else if (rotaion < 0) {
self.recordedRotation = self.recordedRotation - eng;
}
} else if (fabsRotain > M_PI / 8 && fabsRotain <= M_PI / 2) {
if (rotaion < 0)
self.recordedRotation = self.recordedRotation + (M_PI / 2 - eng);
else
self.recordedRotation = self.recordedRotation - (M_PI / 2 - eng);
} else if (fabsRotain > M_PI / 2 && fabsRotain <= M_PI * 5 / 8) {
if (rotaion < 0)
self.recordedRotation = self.recordedRotation - (- M_PI / 2 + eng);
else
self.recordedRotation = self.recordedRotation + (- M_PI / 2 + eng);
} else if (fabsRotain > M_PI * 5 / 8 && fabsRotain <= M_PI) {
if (rotaion < 0) {
self.recordedRotation = self.recordedRotation + (M_PI - eng);
} else
self.recordedRotation = self.recordedRotation - (M_PI - eng);
} else if (fabsRotain > M_PI && fabsRotain <= M_PI * 9 / 8) {
if (rotaion < 0) {
self.recordedRotation = self.recordedRotation - (- M_PI + eng);
} else {
self.recordedRotation = self.recordedRotation + (- M_PI + eng);
}
} else if (fabsRotain > M_PI * 9 / 8 && fabsRotain <= M_PI * 3 / 2) {
if (rotaion < 0) {
self.recordedRotation = self.recordedRotation + (M_PI * 3 / 2 - eng);
} else
self.recordedRotation = self.recordedRotation - (M_PI * 3 / 2 - eng);
} else if (fabsRotain > M_PI * 3 / 2 && fabsRotain <= M_PI * 13 / 8) {
if (rotaion < 0) {
self.recordedRotation = self.recordedRotation - (- M_PI * 3 / 2 + eng);
} else
self.recordedRotation = self.recordedRotation + (- M_PI * 3 / 2 + eng);
} else if (fabsRotain > M_PI * 13 / 8 && fabsRotain <= M_PI *2) {
if (rotaion < 0) {
self.recordedRotation = self.recordedRotation + (M_PI * 2 - eng);
} else
self.recordedRotation = self.recordedRotation - (M_PI * 2 - eng);
}
NSLog(@"recordedRotation2 is %f eng is %f",recordedRotation,eng);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationRepeatCount:1];
[UIView setAnimationDuration:0.3f];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(stopAnimated)];
image.transform = CGAffineTransformMakeRotation(-self.recordedRotation);
[self repositionImageView];
[UIView commitAnimations];
}
- (void)stopAnimated {
}
- (void)repositionImageView {
[image setFrame:CGRectMake(
0,
0,
self.view.frame.size.width - (0 * 2),
self.view.frame.size.height - (0 * 2))];
}