## Get rotated rectangle UIView corners' coordinates iOS

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

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!

EDIT:

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

**Get rotated rectangle UIView corners' coordinates iOS,** I'm trying to find a rotated rectangle UIView's four corners' coordinates.I think one way I can do is to use recognizer.rotation, find the rotated� (2) Translate the rectangle so that center c is at the origin, by subtracting c from all four corners. (3) Rotate the rectangle by 40 deg via the trig formulas cited. (4) Translate back by adding c to each coordinate.

Checked answer in Swift

struct ViewCorners { private(set) var topLeft: CGPoint! private(set) var topRight: CGPoint! private(set) var bottomLeft: CGPoint! private(set) var bottomRight: CGPoint! 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) } }

Then create struct instance and take transformed rect corners new points.

let view = UIView(frame: CGRect(x: 40, y: 20, width: 100, height: 50)) view.transform = .init(rotationAngle: .pi / 4) let corners = ViewCorners(view: view) print(corners.topLeft, corners.topRight, corners.bottomLeft, corners.bottomRight, separator: "\n")

**How to apply scale animation on a rectangular UIView which has ,** My Approach 1.i made a UIView subclass,say DrwArrow and in its draw rect the rotation at the same position, but I can't find anywhere how to do the scaling. When I add this constraint I simply get very rounded corners instead of a circle. In the above code, we first find the rectangle enclosing the text area based on the four points we provide using the cv2.minAreaRect() method. Then in function crop_rect(), we calculate a rotation matrix and rotate the original image around the rectangle center to straighten the rotated rectangle.

Using Swift 4, We can get the bounds of rotated view by simple way.

let transformedBounds = view.bounds.applying(view.transform)

**How to round the corners of a UIView – Swift 5,** All UIView subclasses have the ability to round their corners thanks to their underlying CALayer Duration: 0:54
Posted: May 31, 2019 It has 4 corners. You can re-size that rectangle by dragging one of corner. But now I want to enhance that logic and want to put rotation in that code. I successfully find angle when user touch center of one of the edge of rectangle. But now problem is I can't get new position of corners so that I can redraw that rectangle and rotation is possible.

**iOS,** import UIKit class ViewController: UIViewController { var myLayer = CATextLayer () @IBOutlet weak var myView: UIView! override func viewDidLoad() { super. The CanvasRenderingContext2D.rotate() method of the Canvas 2D API adds a rotation to the transformation matrix. Syntax void ctx.rotate(angle); Parameters angle The rotation angle, clockwise in radians. You can use degree * Math.PI / 180 to calculate a radian from a degree. The rotation center point is always the canvas origin.

**CoreGraphics Geometry Primitives,** A CGRect represents a rectangle and is defined by an origin point ( CGPoint ) On iOS, the origin is located at the top-left corner of a window, so x and y For the other 3 sides, use the built-in convenience properties to get the allows you to translate, scale, and rotate geometries — all at the same time! Dimensions Creates a rectangle using length and width values. ImageJ uses sub-pixel accuracy for the corners of the rotated rectangle, but the image pixels have integer coordinates, so they are considered either inside or outside the rectangle, even if the border would actually cut a pixel into one portion inside and the rest outside.

**Frame vs Bounds in iOS. It all depends on your perspective,** The bounds' origin (point (0, 0) by default) is the top left corner of the view. The yellow rectangle represents the view's frame. Ok, let's get rid of our rotation and focus on the bounds. @IBOutlet weak var myView: UIView! Setting this property updates the origin of the rectangle in the frame property appropriately. Use this property, instead of the frame property, when you want to change the position of a view. The center point is always valid, even when scaling or rotation factors are applied to the view's transform. Changes to this property can be animated.

##### Comments

- How you took value of r = 100?
- I added topLeft.x += self.mainImageView.transform.tx; and topLeft.y += self.mainImageView.transform.ty; after topLeft = CGPointApplyAffineTransform(topLeft, theView.transform); for views that translate as well as scale and rotate.