Ios Swift:如何创建/定义用户触摸图像的区域(宽度x高度)?

Ios Swift:如何创建/定义用户触摸图像的区域(宽度x高度)?,ios,swift,uiimageview,touchesbegan,Ios,Swift,Uiimageview,Touchesbegan,我试图定义一个80x80像素的区域,用户在该区域触摸图像(触摸位置位于80x80矩形的中心) 我知道如何获得触摸位置,但现在定义该区域比我所知道的要好 这就是我所做的: //the imageView which contains a user image @IBOutlet weak var imageView: UIImageView! //the touchesBegan function to get the touch location override func touchesBe

我试图定义一个80x80像素的区域,用户在该区域触摸图像(触摸位置位于80x80矩形的中心)

我知道如何获得触摸位置,但现在定义该区域比我所知道的要好

这就是我所做的:

//the imageView which contains a user image
@IBOutlet weak var imageView: UIImageView!

//the touchesBegan function to get the touch location
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first!
        let location = touch.location(in: imageView)
    }

您可以这样做:

let width: CGFloat = 80.0
let height: CGFloat = 80.0
let touchRectangle = CGRect(x: location.x - width / 2,
                            y: location.y - height / 2,
                            width: width ,
                            height: height)

您可以这样做:

let width: CGFloat = 80.0
let height: CGFloat = 80.0
let touchRectangle = CGRect(x: location.x - width / 2,
                            y: location.y - height / 2,
                            width: width ,
                            height: height)