ios - UIImageView 触摸方向旋转动画

标签 ios cocoa-touch uiimageview transform image-rotation

我有一个带有箭头图像的 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;
}

enter image description here

最佳答案

我假设你想要一个箭头图像指向你触摸的地方,我试过了,这就是我能想到的。我放置了一个带有向上箭头的 ImageView (没有尝试从任何其他位置开始,日志给出了正确的角度)并且在触摸不同位置时它旋转并指向触摸位置。希望它有所帮助(尝试了一些旧数学:-))

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

关于ios - UIImageView 触摸方向旋转动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11431471/

相关文章:

ios - 我什么时候应该调用 [NSUserDefaults standardUserDefaults] registerDefaults :] to ensure that . xib 文件可以立即访问首选项?

ios - iOS中CALayer的UIImage

ios - Xcode 7 - 导出期间发生错误 - IPA 没有主包

ios - 将文本字段中的数字与文本字段中的其他数字相加

ios - wait_fences : failed to receive reply: 10004003?

iPhone 支持多种语言

swift - 如何根据图像调整 UIImaveView 的大小?

ios - SDWebImage 下载后不显示(仅在重新打开 VController 后)

ios - 延迟 RACSignal 直到 UIScrollView 不拖动、跟踪或减速

ios - 如何从 UIWebView(或 WKWebView)访问 asset catalog 中的图片资源