开发者

Get selected item in UIScrollView?

开发者 https://www.devze.com 2023-03-15 00:02 出处:网络
I\'m trying to mak开发者_开发问答e 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, t

I'm trying to mak开发者_开发问答e 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号