随机自定义视图选择器 [英] Random custom view selector

查看:169
本文介绍了随机自定义视图选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含滚动视图的视图控制器,在滚动视图中我水平添加了5个不同的自定义UIView,我希望自定义UIView应该随机添加,具体取决于自定义UIViews宽度,因此应该设置scrollView的contentSize。

I have created a view controller which includes a scrollview and on scrollview i am adding 5 different custom UIView horizontally and i want that custom UIView should be added randomly depending on custom UIViews width and accordingly contentSize of scrollView should be set.

请帮我解决这个问题。

推荐答案

最后我来了解决方案

只需将五个视图放在一个可变数组中,然后将x偏移初始化为0.然后获取一个随机数并抓取数组中的视图(随机模数)数组计数,留在数组内)。将此视图添加到滚动视图,并将x偏移量增加选定视图的宽度。例如,当数组计数为零时停止。然后你的x告诉你你的scrollview的宽度,只需设置它的contentSize:

Just put your five view in a mutable array, and initialize your x offset to 0. Then get a random number and grab the view in the array (random modulo array count, to stay inside the array). Add this view to your scrollview and increase your x offset by the width of the choosed view. Stop when array count is zero for example. Then your x tells you the width of your scrollview, simply set its contentSize :

theScrollView.contentSize = CGSizeMake( x, theScrollView.frame.size.height);

这篇关于随机自定义视图选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆