2011-11-09 48 views
0

目前我使用下面的代碼我旋轉圖像如何找到圖像的確切旋轉值?

- (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); 

//現在我所有的旋轉值保存到一個數組&執行反向排列,並獲取了//最後旋轉值

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

//現在反轉陣列始終顯示始於0.0 .....類似值無論可能是旋轉反轉陣列:( 0, 0, 「0.001174212」, 「0.006030798」, 「0.01210225」, 「0.01215386」, 「0.01220191」, 「0.01224673」, 「0.006139159」, 「0.006149054」, 「0.01850212」, 「0.」, )

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); 
} 
} 

現在我正在將最後一個旋轉值寫入一個xml文件,關閉&重新啓動應用程序我能夠從XML文件讀取最後一個確切的值。

但是由於最後的旋轉值不是實際的旋轉值,圖像不能完美地旋轉到最後狀態。

所以我也試圖通過硬編碼值和完美的圖像旋轉。

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

那麼應該怎樣才能獲得準確的圖像旋轉值。

回答

1

試試這個

在.H把這個東西 浮動角;

-(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); 
    } 
} 
+0

完美和短代碼,這正是我所期待的。 – Michael