2013-09-30 72 views
3

我想找到一个旋转的矩形UIView的四角的坐标。获取旋转的矩形UIView的角坐标iOS

我想我能做的一个方法是使用recognitionizer.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); 
+0

如何取r = 100的值? – iAviator

回答

6

这里是我的解决方案,虽然我不知道是否有一个更简洁的方式(尽管我对此表示怀疑。):

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); 
+0

谢谢!它效果很好。 – user1491987

+0

我加了topLeft.x + = self.mainImageView.transform.tx;和topLeft.y + = self.mainImageView.transform.ty; topLeft = CGPointApplyAffineTransform(topLeft,theView.transform);用于翻译以及缩放和旋转的视图。 – richy

0

Swift中已检查的答案3.1

struct ViewCorners { 
    private var _topLeft:  CGPoint! 
    private var _topRight: CGPoint! 
    private var _bottomLeft: CGPoint! 
    private var _bottomRight: CGPoint! 

    var topLeft:  CGPoint { return _topLeft } 
    var topRight: CGPoint { return _topRight } 
    var bottomLeft: CGPoint { return _bottomLeft } 
    var bottomRight: CGPoint { return _bottomRight } 

    private let originalCenter: CGPoint 
    private let transformedView: UIView 

    private func pointWith(multipliedWidth: CGFloat, multipliedHeight: CGFloat) -> CGPoint { 
     var x = originalCenter.x 
     x += transformedView.bounds.width/2 * multipliedWidth 

     var y = originalCenter.y 
     y += transformedView.bounds.height/2 * multipliedHeight 

     var result = CGPoint(x: x, y: y).applying(transformedView.transform) 
     result.x += transformedView.transform.tx 
     result.y += transformedView.transform.ty 

     return result 
    } 

    init(view: UIView) { 
     transformedView = view 
     originalCenter = view.center.applying(view.transform.inverted()) 

     _topLeft =  pointWith(multipliedWidth:-1, multipliedHeight:-1) 
     _topRight = pointWith(multipliedWidth: 1, multipliedHeight:-1) 
     _bottomLeft = pointWith(multipliedWidth:-1, multipliedHeight: 1) 
     _bottomRight = pointWith(multipliedWidth: 1, multipliedHeight: 1) 

    } 
}