coder
coder

Reputation: 411

Creating a grid system with UICollectionView

I have a UICollectionView that I want to use as a grid. When I receive an ordered pair I want to move a CAShapeLayer circle to the appropriate spot. I have 400 UICollectionView cells to form a 20x20 grid (+, +). Is there a way to do this with UICollectionView?

This what my grid looks like…

UICollectionView Grid

The green dot represents ordered pair (12, 2). I got the dot in the right spot by guessing and checking what CGPoint would correspond with (12, 2).

Any help or suggestions are appreciated! Thanks in advance!

Upvotes: 0

Views: 102

Answers (1)

vacawama
vacawama

Reputation: 154603

Something like this should work assuming your grid goes to the edges of your UICollectionView. You might need to adjust the CGPoint by half the width of your circle.

let width = collectionView.bounds.width

// dimensions of the grid
let xdim: CGFloat = 20
let ydim: CGFloat = 20

// the grid coordinates of your point
let x: CGFloat = 12
let y: CGFloat = 2

let point = CGPoint(x: x * width / xdim, y: (ydim - y) * width / xdim)

Upvotes: 1

Related Questions