swift 如何确定用户在UIImage而不是UIImageView上点击的(x,y)位置?

niwlg2el  于 2022-11-28  发布在  Swift
关注(0)|答案(2)|浏览(134)

UIImage将在运行时添加到UIImageView,我希望确定用户点击图像的位置。
有没有办法确定点击图像的像素(x,y)是什么?
考试:
如果图像大小为x:1000, y:1000apple.png,点击它的正中心,它应该返回x: 500, y: 500
我需要真实的图像(UIImageView.image)上的点像素,而不是UIImageView

mftmpeh8

mftmpeh81#

是,通过将UITapGestureRecognizer添加到图像视图,从中获取点击位置,并将点击坐标转换为图像坐标:

let img = UIImage(named: "whatever")

// add a tap recognizer to the image view
let tap = UITapGestureRecognizer(target: self, 
             action: #selector(self.tapGesture(_:)))
imgView.addGestureRecognizer(tap)
imgView.isUserInteractionEnabled = true
imgView.image = img
imgView.contentMode = .scaledAspectFit

func convertTapToImg(_ point: CGPoint) -> CGPoint? {
    let xRatio = imgView.frame.width / img.size.width
    let yRatio = imgView.frame.height / img.size.height
    let ratio = min(xRatio, yRatio)

    let imgWidth = img.size.width * ratio
    let imgHeight = img.size.height * ratio

    var tap = point
    var borderWidth: CGFloat = 0
    var borderHeight: CGFloat = 0
    // detect border
    if ratio == yRatio {
        // border is left and right
        borderWidth = (imgView.frame.size.width - imgWidth) / 2
        if point.x < borderWidth || point.x > borderWidth + imgWidth {
            return nil
        }
        tap.x -= borderWidth
    } else {
        // border is top and bottom
        borderHeight = (imgView.frame.size.height - imgHeight) / 2
        if point.y < borderHeight || point.y > borderHeight + imgHeight {
            return nil
        }
        tap.y -= borderHeight
    }

    let xScale = tap.x / (imgView.frame.width - 2 * borderWidth)
    let yScale = tap.y / (imgView.frame.height - 2 * borderHeight)
    let pixelX = img.size.width * xScale
    let pixelY = img.size.height * yScale
    return CGPoint(x: pixelX, y: pixelY)
}

@objc func tapGesture(_ gesture: UITapGestureRecognizer) {
    let point = gesture.location(in: imgView)
    let imgPoint = convertTapToImg(point)
    print("tap: \(point) -> img \(imgPoint)")
}
neekobn8

neekobn82#

import UIKit
import AVFoundation

class ImageViewController: UIViewController {

private lazy var imageView: UIImageView = {
    let imageView = UIImageView()
    imageView.image = UIImage(named: "Screenshot")
    imageView.bounds = CGRect(origin: .zero,
                              size: CGSize(width: view.bounds.width * 0.8,
                                           height: view.bounds.width * 0.8))
    imageView.contentMode = .scaleAspectFit
    imageView.center = view.center
    imageView.isUserInteractionEnabled = true
    imageView.backgroundColor = .blue
    return imageView
}()

override func viewDidLoad() {
    super.viewDidLoad()
    view.addSubview(imageView)
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let imageRect = recognizeImageRect(onImageView: imageView) else { return } 
    let mockView = UIView(frame: imageView.frame)
    mockView.frame.origin.x += imageRect.origin.x
    mockView.frame.origin.y += imageRect.origin.y
    guard  let point = touches.first?.location(in: mockView) else { return }
    print(point)
}

private func recognizeImageRect(onImageView imageView: UIImageView) -> CGRect? {
    guard let imageSize = imageView.image?.size else { return nil }
    let rect = AVMakeRect(aspectRatio: imageSize, insideRect: imageView.bounds)
    return rect
}

}

相关问题