获取旋转的矩形 UIView 角坐标 iOS [英] Get rotated rectangle UIView corners' coordinates iOS

查看:37
本文介绍了获取旋转的矩形 UIView 角坐标 iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一个旋转矩形 UIView 的四个角的坐标.

I'm trying to find a rotated rectangle UIView's four corners' coordinates.

我认为我能做的一种方法是使用receiver.rotation,找到旋转的角度然后计算原点.但这需要一些几何计算.

I think one way I can do is to use recognizer.rotation, find the rotated angle then calculate the origins. But that requires some geometry calculation.

- (IBAction)handlePan:(UIRotationGestureRecognizer*)recognizer {
    NSLog(@"Rotation in degrees since last change: %f", [recognizer rotation] * (180 / M_PI));
    recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
    NSLog(@"%@",recognizer);

    recognizer.rotation = 0;
    NSLog(@"bound is %f and %f, frame is %f and %f, %f and %f.",recognizer.view.bounds.size.width,recognizer.view.bounds.size.height, recognizer.view.frame.size.width,recognizer.view.frame.size.height, recognizer.view.frame.origin.x, recognizer.view.frame.origin.y);
}

我只是想知道是否还有其他更简单的方法来获取坐标?谢谢!

I'm just wondering if there are any other easier ways to get the coordinates? Thanks!

看起来我们在这里有一个很好的答案(见下面的答案).我设法通过一种愚蠢的方式来计算角落——使用旋转角度和几何.它有效但并不容易和轻便.我在这里分享我的代码以防万一有人可能想要使用它(即使我怀疑它.)

Looks like we have a great answer here(see answer below). I have managed to calculate the corners through a stupid way -- using rotation angle and geometry. It works but not easy and light. I'm sharing my code here just in case some one may want to use it(Even though I doubt it.)

    float r = 100;
    NSLog(@"radius is %f.",r);
    float AAngle = M_PI/3+self.rotatedAngle;
    float AY = recognizer.view.center.y - sin(AAngle)*r;
    float AX = recognizer.view.center.x - cos(AAngle)*r;
    self.pointPADA = CGPointMake(AX, AY);
    NSLog(@"View Center is (%f,%f)",recognizer.view.center.x,recognizer.view.center.y);
    NSLog(@"Point A has coordinate (%f,%f)",self.pointPADA.x,self.pointPADA.y);

    float BAngle = M_PI/3-self.rotatedAngle;
    float BY = recognizer.view.center.y - sin(BAngle)*r;
    float BX = recognizer.view.center.x + cos(BAngle)*r;
    self.pointPADB = CGPointMake(BX, BY);
    NSLog(@"Point B has coordinate (%f,%f)",BX,BY);

    float CY = recognizer.view.center.y + sin(AAngle)*r;
    float CX = recognizer.view.center.x + cos(AAngle)*r;
    self.pointPADC = CGPointMake(CX, CY);
    NSLog(@"Point C has coordinate (%f,%f)",CX,CY);

    float DY = recognizer.view.center.y + sin(BAngle)*r;
    float DX = recognizer.view.center.x - cos(BAngle)*r;
    self.pointPADD = CGPointMake(DX, DY);
    NSLog(@"Point D has coordinate (%f,%f)",DX,DY);

推荐答案

这是我的解决方案,但我想知道是否有更简洁的方法:

Here's my solution though I wonder if there's a more succinct way:

CGPoint originalCenter = CGPointApplyAffineTransform(theView.center,
    CGAffineTransformInvert(theView.transform));

CGPoint topLeft = originalCenter;
topLeft.x -= theView.bounds.size.width / 2;
topLeft.y -= theView.bounds.size.height / 2;
topLeft = CGPointApplyAffineTransform(topLeft, theView.transform);

CGPoint topRight = originalCenter;
topRight.x += theView.bounds.size.width / 2;
topRight.y -= theView.bounds.size.height / 2;
topRight = CGPointApplyAffineTransform(topRight, theView.transform);

CGPoint bottomLeft = originalCenter;
bottomLeft.x -= theView.bounds.size.width / 2;
bottomLeft.y += theView.bounds.size.height / 2;
bottomLeft = CGPointApplyAffineTransform(bottomLeft, theView.transform);

CGPoint bottomRight = originalCenter;
bottomRight.x += theView.bounds.size.width / 2;
bottomRight.y += theView.bounds.size.height / 2;
bottomRight = CGPointApplyAffineTransform(bottomRight, theView.transform);

这篇关于获取旋转的矩形 UIView 角坐标 iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆