user930195
user930195

Reputation: 432

How to find the exact rotation value of an image?

Currently i am using following code to rotate my image

- (void)myImageRotate:(UIRotationGestureRecognizer *)gesture
{
     if(arr==nil)
        {
            arr=[[NSMutableArray alloc] init ];
        }

     if ([gesture state] == UIGestureRecognizerStateBegan || [gesture state] == UIGestureRecognizerStateChanged) 
        {
            currentRotation =(float) [gesture rotation];

        self.transform=CGAffineTransformRotate(self.transform,currentRotation);
        [gesture setRotation:0];
        [arr addObject:[NSNumber numberWithFloat:currentRotation]]; 
    }
    NSLog(@"Rotation Value: %f", currentRotation);

//Now i am saving all rotation value to an array & perform reverse array and fetching the //last rotation value

NSArray *reversedArray = [[arr reverseObjectEnumerator] allObjects];
NSLog(@"Array: %@", arr);
NSLog(@"Reversed Array: %@", reversedArray);

//Now the Reversed array is displaying always start with 0.0.....like value whatever may be the rotation Reversed Array: ( 0, 0, "0.001174212", "0.006030798", "0.01210225", "0.01215386", "0.01220191", "0.01224673", "0.006139159", "0.006149054", "0.01850212", "0.01237607", )

lastRotationValue = 0.0;
for (NSString* stringValue in [arr reverseObjectEnumerator]) {
    double value = [stringValue doubleValue];
    if (value != 0.0) {

        //Note: 1 degree=0.0174532925 radian

        lastRotationValue = value;
        break;
    }
}

if (lastRotationValue != 0.0) {
    NSLog(@"My firstNonZeroValue of Rotation Degree:%f",lastRotationValue);
}
}

Now i am writing the last rotation value to a xml file ,Close & restart app i am able to read the last exact value from XML file .

But as the last rotation value is not actual rotation value the image is not rotating perfectly to last state.

So i have also tried by putting hard coded value and the image rotating perfectly.

self.transform = CGAffineTransformMakeRotation(1.57);//By 90 Degree

So what should be the solution to get exact rotation value of image.

Upvotes: 0

Views: 1070

Answers (1)

raaz
raaz

Reputation: 12500

Try this one

In .h put this thing float angle;

-(void)viewDidLoad
{
[super viewDidLoad];

UIRotationGestureRecognizer *recognizer = [[[UIRotationGestureRecognizer alloc]                                          initWithTarget:self                                        action:@selector(handleRotate:)] autorelease];

[self.rotatedView addGestureRecognizer:recognizer];

}
- (void)handleRotate:(UIRotationGestureRecognizer *)recognizer {
    // current value is past rotations + current rotation
    float rotation = angle + -recognizer.rotation;  
    self.rotatedView.transform = CGAffineTransformMakeRotation(-rotation);

    // once the user has finsihed the rotate, save the new angle
    if (recognizer.state == UIGestureRecognizerStateEnded) {
        angle = rotation;

        NSLog(@"Last Rotation: %f",angle);
    }
}

Upvotes: 1

Related Questions