s73v3r
s73v3r

Reputation: 1751

Get selected item in UIScrollView?

I'm trying to make an app where the user has a "hand" of cards, and they can select one (it does more than that, as just that would be entertaining for about 3 seconds). Currently, the "hand" is represented by a horizontal, paging UIScrollView. Each card is a UIImageView. Things scroll nicely, but where I'm stuck at is trying to bubble up the selected card to the controller. What's the best way to do this?

Upvotes: 0

Views: 3699

Answers (1)

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

You can directly use the contentOffset to judge the current page.

int cardIndex = (int)(scrollView.contentOffset.x / scrollView.frame.size.width);

You might've to round it though. You should be able to get the card index using the snippet above. However it doesn't hurt to maintain an instance variable which you would update in the delegate method scrollViewDidScroll:.

Or you can look at UIPageControl too which kinda does something similar but a bit more overhead for your case.

I would go for the extra instance variable or property.

Upvotes: 1

Related Questions