3

I've applied the rotation transform on the UIImageImage

Suppose the actual frame of UIImageView is

Frame : (100.0, 360.0, 200.0, 130)

The white border is the frame

enter image description here

After applying rotation to some degrees the UIImageView returning frame is

Frame After Rotation : (97.10, 310.81, 219.79, 240.37)

enter image description here

What I need to get is the actual corners points of the rotated image.

enter image description here

Please help. I'm not sure how to get the actual corners points

As per answer by @sandeep, the result is as follows

enter image description here

The dots should move by the corner of the image and should stick to the corners. I Know if I place the dots as subview of UIImageView but Dots are free and can not place inside UIImageView to move when rotation applying.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Amit Singh
  • 2,698
  • 21
  • 49

1 Answers1

2

You would get the points from each corner of the original frame and pass them through the same transform.

So the upper left corner would be let ul = view.frame.origin before the rotation.

Then the point after rotation is let ulT = ul.applying(view.transform)

Example:

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {
    var imageView: UIView!
    var rotationRecognizer: UIRotationGestureRecognizer!
    var cornerView: UIView!

    override func loadView() {
        let view = UIView()
        view.backgroundColor = .white

        let frame = CGRect(x: 100, y: 200, width: 200, height: 200)
        imageView = UIView(frame: frame)
        imageView.backgroundColor = .blue
        view.addSubview(imageView)

        cornerView = UIView(frame: CGRect(x: 95, y: 195, width: 10, height: 10))
        cornerView.layer.cornerRadius = 5
        cornerView.backgroundColor = .red
        view.addSubview(cornerView)

        rotationRecognizer = UIRotationGestureRecognizer(target: self, action: #selector(handleRotation(recognizer:)))
        view.addGestureRecognizer(rotationRecognizer)

        self.view = view
    }

    @objc func handleRotation(recognizer: UIRotationGestureRecognizer) {
         imageView.transform = CGAffineTransform(rotationAngle: recognizer.rotation)
        var cornerPoint = CGPoint(
            x: 100 - imageView.center.x,
            y: 200 - imageView.center.y
        )
        cornerPoint = cornerPoint.applying(imageView.transform)
        cornerPoint.x += imageView.center.x
        cornerPoint.y += imageView.center.y
        cornerView.center = cornerPoint
    }
}

// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
Ken Boreham
  • 904
  • 6
  • 16
  • Thanks to answer @Ken but the transformation only applies to the `CGPoint`. I've updated my question. Please check. I need to rotate the dots with the rotation on `UIImageView` – Amit Singh Sep 27 '18 at 10:28
  • That's correct. So, you would need to offset the point based on the rotation point in the image (looks like you intend to rotate around the center). Then you can use that transformed point as the center point of your corner image. – Ken Boreham Sep 27 '18 at 17:42
  • @AmitSingh I added code example to make it more clear. – Ken Boreham Sep 27 '18 at 17:50