[英]How To Rotate an UIImageView based on a point in Touches Moved Method?
親愛的大家,
我想相對於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;
現在我得到的結果是圖像視圖中的圖像在每次移動中持續旋轉。 但是imageview並沒有旋轉。 我需要的是旋轉imageview而不是圖像。
任何幫助將不勝感激。
謝謝和最誠摯的問候,Rupesh R Menon
如果我理解正確,你想在圖像上實現單指旋轉。 如果是這樣,您可以使用我的一個實際工作項目中的以下功能。 您可以將此用於單個圖像以及多個圖像。 您需要在某種程度上修改多個圖像。 最好的方法是擴展UIImageView類並創建自己的類。
從觸摸移動調用此功能
- [self transformImagewithTouches:touch];
聲明1屬性並合成如下。 (如果需要,請在下面的代碼中聲明其他變量。
- @property(nonatomic)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;
}
希望能幫助到你。 如果你堅持下來請告訴我,我一定會幫助你。
感謝您對我的興趣。
當我使用下面的代碼時,我能夠以我想要的角度旋轉圖像視圖。
imgView.layer.transform = CATransform3DMakeRotation(pCalculator.tangentToCornerAngle,0,0,1);
其中imgView是一個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 Menon
這是因為你已經將它旋轉了M_PI / 2,當你再次旋轉它時它會從初始位置旋轉它。 使用這個
CGAffineTransform transforms = CGAffineTransformConcat(imgView.transform,CGAffineTransformMakeRotation(M_PI/2));
imgView.transform = transforms;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.