'How can I get CGRect of tapped rectangle in ScrollView? [duplicate]

enter image description here

Hello, I want to know the CGRect(coordinates and width/height) of dynamic size rectangle which user tapped in ScrollView.

Is there any way to get this?



Solution 1:[1]

If you have added UITapGestureRecognizer in every view then you can get the tapped view frame as -

@objc func tapFunction(gesture: UITapGestureRecognizer) {
    let viewFrame = gesture.view.frame
    let height = viewFrame.height  //for height
}

You need to add gesture in every view with a parameter.

let tap = UITapGestureRecognizer(target: self, action: #selector(tapFunction(gesture:)))
yourView.addGestureRecognizer(tap)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Imran0001