2010-10-24 41 views
4

我想旋轉圖像。這我成功了。問題是,當我點擊並輕微拖動時,圖像完全旋轉到達點擊位置,然後順時針拖動圖像,然後緩慢旋轉。我擔心它爲什麼會完全旋轉到我拖着的地方。我想拖動從找到的位置開始,而不是旋轉到我的手指停下的位置,然後從那裏開始。使用目標旋轉圖像C

這是我的代碼:

-

(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    } 

    -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
     NSSet *allTouches = [event allTouches]; 

     int len = [allTouches count]-1; 

     UITouch *touch =[[allTouches allObjects] objectAtIndex:len]; 
     CGPoint location = [touch locationInView:[self superview]]; 
     float theAngle = atan2(location.y-self.center.y, location.x-self.center.x); 

     totalRadians = theAngle; 

     [self rotateImage:theAngle]; 

    } 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
} 

-(void) rotateImage:(float)angleRadians{ 

    self.transform = CGAffineTransformMakeRotation(angleRadians); 
    CATransform3D rotatedTransform = self.layer.transform; 
    self.layer.transform = rotatedTransform;  
} 

難道我做錯了什麼?

謝謝!

+0

我懷疑問題出在你的rotateImage:方法你介意發佈代碼嗎? – Mattia 2010-11-01 00:38:23

+0

它有 - 在代碼片段底部 – Lilz 2010-11-02 15:38:03

回答

3

不是從您從觸摸中獲得的角度創建轉換,而是嘗試通過與新角度的差異來增加totalRadians,然後從中創建轉換。

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
     NSSet *allTouches = [event allTouches]; 

     int len = [allTouches count]-1; 

     UITouch *touch =[[allTouches allObjects] objectAtIndex:len]; 
     CGPoint location = [touch locationInView:[self superview]]; 
     float theAngle = atan2(location.y-self.center.y, location.x-self.center.x); 

     totalRadians += fabs(theAngle - totalRadians); 
     totalRadians = fmod(totalRadians, 2*M_PI); 

     [self rotateImage:totalRadians]; 

    } 
1

我的第一個建議是,如果您的應用程序的版本是4.0或更高版本,則切換到使用UIRotateGestureRecognizer。它做正確的事情,併爲您提供一個rotation屬性。

+0

我希望也支持舊版本 – Lilz 2010-10-25 08:37:51