I have a CGRect which I have rotated using translation and rotation function as below:-
let ctx: CGContext = UIGraphicsGetCurrentContext()!
ctx.saveGState()
let halfWidth = (selectedCropRect?.size.width)! / 2.0
let halfHeight = (selectedCropRect?.size.height)! / 2.0
let center = CGPoint(x: (selectedCropRect?.origin.x)! + halfWidth, y: (selectedCropRect?.origin.y)! + halfHeight)
// Move to the center of the rectangle:
ctx.translateBy(x: center.x, y: center.y)
// Rotate:
ctx.rotate(by: rotationAngle!);
// Draw the rectangle centered about the center:
let rect = CGRect(x: -halfWidth, y: -halfHeight, width: (selectedCropRect?.size.width)!, height: (selectedCropRect?.size.height)!)
let path = UIBezierPath(rect: rect).cgPath
ctx.addPath(path)
ctx.restoreGState()
Now the problem is i need to get all the corner points of the rotated rect and place circular views on the corner points so that the user can drag the corner points and increase/decrease size of rect. Any idea how i can place the circular views on the 4 corner points of rotated rect?
Untested but this should help point you in the right direction
guard let rect = selectedCropRect else {
return
}
let rectCenter = CGPoint(x: rect.width/2, y: rect.height/2)
// Or whatever angle you supply
let rotAngle = CGFloat.pi/4.0
// Rotate around center of rect
/*
Instead of creating a new transform you should store a transform
inside of the object you are transforming like UIView does
*/
var transform = CGAffineTransform(translationX: rectCenter.x, y: rectCenter.y)
transform = transform.rotated(by: rotAngle)
transform = transform.translatedBy(x: -rectCenter.x, y: -rectCenter.y)
// Get transformed center
let originalCenter = rectCenter.applying(transform)
var topLeft = originalCenter
topLeft.x -= rectCenter.x
topLeft.y -= rectCenter.y
topLeft = topLeft.applying(transform)
var topRight = originalCenter
topRight.x += rectCenter.x
topRight.y -= rectCenter.y
topRight = topRight.applying(transform)
var botLeft = originalCenter
botLeft.x -= rectCenter.x
botLeft.y += rectCenter.y
botLeft = botLeft.applying(transform)
var botRight = originalCenter
botRight.x += rectCenter.x
botRight.y += rectCenter.y
botRight = botRight.applying(transform)
// Create new path using the orignal supplied rect and transform
let path = CGPath(rect: rect, transform: &transform)
let bez = UIBezierPath(cgPath: path)
// Do whatever you need with the context
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.