应用旋转变换后如何获取UIImageView的实际角点

时间:2018-09-27 08:30:58

标签: ios image-rotation

我已将旋转变换应用于UIImageImage

假设UIImageView的实际框架是

Frame : (100.0, 360.0, 200.0, 130)

白色边框是框架

enter image description here

在进行一定程度的旋转后,UIImageView返回帧为

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

enter image description here

我需要得到的是旋转图像的实际角点。

enter image description here

请帮助。我不确定如何获得实际的角点数

根据@sandeep的回答,结果如下

enter image description here

点应在图像的角部移动,并应固定在角部。我知道是否将圆点作为UIImageView的子视图放置,但是圆点是自由的,并且不能放置在UIImageView内以在旋转应用时移动。

1 个答案:

答案 0 :(得分:0)

您将从原始框架的每个角获取点,并将它们传递给相同的变换。

因此,旋转之前左上角为let ul = view.frame.origin

然后旋转后的点是let ulT = ul.applying(view.transform)

示例:

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