2011-05-11 49 views
0

親愛的,如何根據觸摸移動方法中的點旋轉UIImageView?

我想相對於touchesMoved方法的一個點一個UIImageView旋轉。

我試過2 -3種方法,但我沒有得到我期望的確切結果。

首先方法我用

CGAffineTransform transforms = CGAffineTransformMakeRotation(M_PI/2); 

imgView.transform = transforms; 

這是寫在touchesMoved。所以我期望在每個touchesMoved中的圖像視圖旋轉。 但是旋轉只發生一次。

我用第二種方法

CGAffineTransform transforms = CGAffineTransformRotate(imgView.transform, M_PI/2); 

imgView.transform = transforms; 

現在的結果是我得到的是在圖像視圖圖像中的每個舉動continusely旋轉。但imageview不旋轉。我需要的是旋轉im​​ageview而不是圖像。

任何幫助將不勝感激。

感謝&最好的問候, Rupesh [R梅農

+0

難道ü嘗試CABasicAnimation? – Krishnan 2011-05-11 10:35:57

+0

是的。它也沒有給出所需的輸出。 – 2011-05-11 11:17:28

回答

0

那是因爲你已經通過M_PI/2旋轉它,當你再次旋轉它,它從初始位置轉動它。如果我理解正確,您想達到的圖像上單指旋轉利用這一

CGAffineTransform transforms = CGAffineTransformConcat(imgView.transform,CGAffineTransformMakeRotation(M_PI/2)); 
imgView.transform = transforms; 
+0

親愛的Inder,我嘗試了你的建議。但它給了我與第二種方法一樣的結果。即;圖像正在旋轉而不是圖像視圖。我需要圖像視圖旋轉。任何如何感謝所顯示的興趣。 – 2011-05-11 11:08:19

+0

你會怎麼說imageView沒有被旋轉,你正在旋轉imageView沒有圖像,爲什麼內容旋轉... – 2011-05-11 11:40:26

+0

我親愛的朋友,你必須有一個方形imageView旋轉它90度,使它感覺一樣。要麼改變其高度或寬度(一個矩形不是一個正方形),那麼你可以看到不同之處,或者你可以將它旋轉45度,僅用於測試目的。 'M_PI/4' – 2011-05-11 11:59:06

1

。如果是這樣,您可以使用我的一個現場工作項目中的以下功能。您可以將其用於單張圖像以及多張圖像。您需要在某種程度上修改多個圖像。最好的方法是擴展UIImageView類並創建自己的類。

從觸摸移動調用此函數

  • [自transformImagewithTouches:觸摸];

聲明1屬性併合成如下。 (也請如果需要在下面的代碼聲明其他變量

  • @屬性(非原子)CGFloat的fltRotatedAngle;
-(void)transformImagewithTouches:(UITouch *)touchLocation 
{ 
    //NSLog(@"Before %f",self.fltRotatedAngle); 
    CGPoint touchLocationpoint = [touchLocation locationInView:[self superview]]; 
    CGPoint PrevioustouchLocationpoint = [touchLocation previousLocationInView:[self superview]]; 
    CGPoint origin; 
    origin.x=self.center.x; 
    origin.y=self.center.y; 
    CGPoint previousDifference = [self vectorFromPoint:origin toPoint:PrevioustouchLocationpoint]; 
    CGAffineTransform newTransform =CGAffineTransformScale(self.transform, 1, 1); 
    CGFloat previousRotation = atan2(previousDifference.y, previousDifference.x); 
    CGPoint currentDifference = [self vectorFromPoint:origin toPoint:touchLocationpoint]; 
    CGFloat currentRotation = atan2(currentDifference.y, currentDifference.x); 
    CGFloat newAngle = currentRotation- previousRotation; 

    //Calculate Angle to Store 
    fltTmpAngle = fltTmpAngle+newAngle; 
    self.fltRotatedAngle = (fltTmpAngle*180)/M_PI; 

    newTransform = CGAffineTransformRotate(newTransform, newAngle); 
    [self animateImageView:self toPosition:newTransform]; 
} 

-(void)animateImageView:(UIImageView *)theView toPosition:(CGAffineTransform) newTransform 
{ 
    [UIView setAnimationsEnabled:YES]; 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationCurve:UIViewAnimationCurveLinear]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:0.0750]; 
    self.transform = newTransform; 
    [UIView commitAnimations]; 
} 

-(CGPoint)vectorFromPoint:(CGPoint)firstPoint toPoint:(CGPoint)secondPoint 
{ 
    CGFloat x = secondPoint.x - firstPoint.x; 
    CGFloat y = secondPoint.y - firstPoint.y; 
    CGPoint result = CGPointMake(x, y); 
    return result; 
} 

希望它可以幫助如果你卡住了,請讓我。知道我一定會幫你的

+0

謝謝Jennnis,這真的很有幫助。我能夠使用代碼中的某個部分。 – 2011-05-12 03:48:34

+0

@ Jennis,我想要旋轉以及翻譯,如果我想旋轉,然後將我的圖像拖到屏幕的其他部分,我該怎麼做? – user930195 2011-11-12 08:10:04

+0

您必須首先定義動作,即旋轉或翻譯的內容。並根據選定的行動相應採取行動。您可以從教程中輕鬆獲得翻譯代碼。和我已經給出的輪換。 – 2011-11-12 12:55:35

1

感謝您對我的幫助所表示的興趣

當我使用下面的代碼時,我能夠以所需的角度旋轉圖像視圖。

imgView.layer。transform = CATransform3DMakeRotation(pCalculator.tangentToCornerAngle,0,0,1);

其中img查看是一個UIImageView。

pCalculator.tangentToCornerAngle is the desired angle in which rotation has to be made. 

Function is CATransform3DMakeRotation(CGFloat angle, CGFloat x, <CGFloat y, CGFloat z); 

三江源所有, Rupesh [R梅農