How to calculate a random CGPoint which does not t

2019-03-29 22:15发布

This is an example view:

enter image description here

I want to calculate a frame with a CGPoint where I can spawn another card(UIView) without touching any existing card. Ofcourse it is optional since the view can be full of cards, therefore there is no free spot.

This is how I can see any card on the screen and my function how it is now:

func freeSpotCalculator() -> CGPoint?{
    var takenSpots = [CGPoint]()
    for card in playableCards{
        takenSpots.append(card.center)
    }

}

I have no idea where to start and how to calculate a random CGPoint on the screen. The random frame has the same width and height as a card in on the screen.

7条回答
啃猪蹄的小仙女
2楼-- · 2019-03-29 22:48

As I can see in the picture all your cards are aligned at the bottom of the View. so if you generate a random y position from 0 to origin of your cards row - one card height you can simply get a CGPoint based on the frame of your view and size of the cards.

查看更多
登录 后发表回答