从头到尾循环 uiimages 的 Nsmutable 数组
我有一个 NSMUtable 图像数组,其中使用上一个和下一个按钮显示不同的图像,但是当我到达数组末尾时,模拟器崩溃了。我想将数组的末尾循环到开头,这样当我到达图像数组的末尾时,当我再次点击下一个按钮时,它会循环回到第一个图像,当我点击第一个图像时,它也会循环到第一个图像上一个按钮循环到最后一个图像,没有崩溃
I have an NSMUtable array of images where a different image is displayed with a previous and a next button, but when I get to the end of the array the simulator crashes. I want to loop the end of the array to the beginning so that when I get to the end of the array of images when I hit the next button again it loops back to the first image, also when Im at the first image if I hit the previous button it loops to the last image with no crashes
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你想要的是一个循环数组,使用标准的 NSMutableArray 很容易实现。例如,假设您将图像存储在名为
imageArray
的数组中,并使用一个简单的变量来跟踪当前图像的索引,例如:...那么您可以实现
nextImage< /code> 和
previousImage
类似:然后只要你想单步遍历数组,就使用
nextImage
和previousImage
,问题就解决了。What you want is a circular array, which is easy to implement using a standard
NSMutableArray
. For instance, say you store your images in an array calledimageArray
and use a simple variable to keep track of the index of your current image, like:...then you might implement
nextImage
andpreviousImage
like:Then just use
nextImage
andpreviousImage
whenever you want to step through the array, and problem solved.很简单就能做到。您需要做的就是创建一个检查来查看是否位于最后一个元素,如果是,则再次将跟踪器(如 count 或 i 等)设置为 0,
这是伪代码
//设置索引
Simple enough to do. all you need to do is create a check to see if you're on the last element, and if so, set your tracker (like count or i etc) to 0 again,
Heres the psudo code
//set index
我想提出这个解决方案:
所选 UIImage 的属性,保存当前索引的 NSInteger 属性。
I want to propose this solution:
A property for the selected UIImage, a NSInteger property to hold the current index.