UIImageView旋转animation在触摸的方向

我有一个UIImageView有一个箭头的图像。 当用户点击UIView时,这个箭头应指向保持其位置的水龙头的方向,它应该改变变换。 我已经实现了以下代码。 但它不能按预期工作。 我添加了一个截图。 在这个截图中,当我触摸点左上方的箭头方向应该如图所示。但是这并不是如此。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch=[[event allTouches]anyObject]; touchedPoint= [touch locationInView:touch.view]; imageViews.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(rangle11)); previousTouchedPoint = touchedPoint ; } - (CGFloat) pointPairToBearingDegrees:(CGPoint)startingPoint secondPoint:(CGPoint) endingPoint { CGPoint originPoint = CGPointMake(endingPoint.x - startingPoint.x, endingPoint.y - startingPoint.y); // get origin point to origin by subtracting end from start float bearingRadians = atan2f(originPoint.y, originPoint.x); // get bearing in radians float bearingDegrees = bearingRadians * (180.0 / M_PI); // convert to degrees bearingDegrees = (bearingDegrees > 0.0 ? bearingDegrees : (360.0 + bearingDegrees)); // correct discontinuity return bearingDegrees; } 

在这里输入图像说明

我假设你想要一个箭头图像指向你触摸的地方,我试过了,这是我能想出来的。 我把一个箭头向上的图像视图(没有尝试从任何其他位置开始,日志给出正确的angular度),并触摸不同的位置,它旋转并指向触摸的位置。 希望它可以帮助(尝试一些旧的math:-))

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch=[[event allTouches]anyObject]; touchedPoint= [touch locationInView:touch.view]; CGFloat angle = [self getAngle:touchedPoint]; imageView.transform = CGAffineTransformMakeRotation(angle); } -(CGFloat) getAngle: (CGPoint) touchedPoints { CGFloat x1 = imageView.center.x; CGFloat y1 = imageView.center.y; CGFloat x2 = touchedPoints.x; CGFloat y2 = touchedPoints.y; CGFloat x3 = x1; CGFloat y3 = y2; CGFloat oppSide = sqrtf(((x2-x3)*(x2-x3)) + ((y2-y3)*(y2-y3))); CGFloat adjSide = sqrtf(((x1-x3)*(x1-x3)) + ((y1-y3)*(y1-y3))); CGFloat angle = atanf(oppSide/adjSide); // Quadrant Identifiaction if(x2 < imageView.center.x) { angle = 0-angle; } if(y2 > imageView.center.y) { angle = M_PI/2 + (M_PI/2 -angle); } NSLog(@"Angle is %2f",angle*180/M_PI); return angle; } 

-anoop4real

鉴于你告诉我,我认为问题是,你没有重置touchesBegan你的变换。 尝试改变它是这样的,看看它是否工作得更好:

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch=[[event allTouches]anyObject]; touchedPoint= [touch locationInView:touch.view]; imageViews.transform = CGAffineTransformIdentity; imageViews.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(rangle11)); previousTouchedPoint = touchedPoint ; } 

你是否需要这条线来“消除不连续性”? 似乎atan2f()返回+π到-π之间的值。 那些不会直接使用CATransform3DMakeRotation()

你需要的是箭头指向最后一个点击点。 为了简化和testing,我使用了一个轻击手势(但它与touchBegan:withEvent :)类似。

在viewDidLoad方法中,我注册了这个手势:

 UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped :)];
 [self.view addGestureRecognizer:tapGesture];
 [tapGesture发布];

在每个水龙头上调用的方法:

 - (void)轻敲:(UITapGestureRecognizer *)手势
 {
     CGPoint imageCenter = mFlecheImageView.center;
     CGPoint tapPoint = [手势locationInView:self.view];

    双deltaY = tapPoint.y  -  imageCenter.y;
     double deltaX = tapPoint.x  -  imageCenter.x;
     double angleInRadians = atan2(deltaY,deltaX)+ M_PI_2;

     mFlecheImageView.transform = CGAffineTransformMakeRotation(angleInRadians);
 }

一个关键是+ M_PI_2因为UIKit坐标的原点位于左上angular(而在三angular形中,我们使用左下angular)。