我试图找到一个旋转的矩形 UIView 的四个角的坐标。
我认为我可以做的一种方法是使用recognizer.rotation,找到旋转的角度然后计算原点。但这需要一些几何计算。
- (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);
}
我只是想知道是否还有其他更简单的方法来获取坐标?谢谢!
编辑:
看起来我们在这里有一个很好的答案(见下面的答案)。我设法通过一种愚蠢的方式计算了角落——使用旋转角度和几何形状。它有效,但并不容易和轻巧。我在这里分享我的代码,以防有人想使用它(尽管我对此表示怀疑。)
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);