I’d like to know what the best approach is to placing multiple UIViews on screen with random coordinates without them touching each other or touching the parent view’s edges.
Thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use arc4random() to generate random numbers. You’ll want to constrain these to be between 0 and your parent views frame width for the x-coordinate, and between 0 and your views frame height for the y-coordinate. Subtract your random views frame width/height from the extent assuming you want it fully visible.
Then you’ll need to enumerate all your existing random views and ensure that your new random view frame doesn’t intersect with the frame of your existing views.
Use CGRectIntersectsRect to see if two UIView frames intersect:
How to figure out if two CGRect intersect?