Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check a UIView placed inside of UIImage portion or not in iOS?

According to my requirement i need to check UIView are placed inside of UIImage portion or not.Suppose i have UIImage of human with cap, here i need to check my view placed inside of cap portion or not.

Could you please guide me to achieve following requirement.

Example:

      ---------------------------------
     |                                |
     |          UIImage               |
     |   --------------------------   |
     |  |   Image portion          |  |
     |  |              ------      |  |
     |  |             | view |     |  |
     |  |              ------      |  |
     |  |                          |  |
     |   --------------------------   |
     |                                |
     |                                |
     --------------------------------  
like image 617
bAthi Avatar asked Dec 04 '18 09:12

bAthi


1 Answers

You could use the method contains(_:) or intersects(_:) of CGRect. Your code will look something like

let imagePortionFrame = CGRect(....) // some rect based on your image view imagePortionFrame.contains(view.frame)

You just need to make sure that the coordinates of imagePortionFrame and view.frame are expressed using the same coordinate system; if not you could use the method convert(_:to:)

Reference:

  • contains

  • intersect

  • convert

like image 58
Mario Avatar answered Nov 13 '22 09:11

Mario