从头到尾循环 uiimages 的 Nsmutable 数组

发布于 2024-12-27 05:09:08 字数 162 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

又爬满兰若 2025-01-03 05:09:08

你想要的是一个循环数组,使用标准的 NSMutableArray 很容易实现。例如,假设您将图像存储在名为 imageArray 的数组中,并使用一个简单的变量来跟踪当前图像的索引,例如:

int currentImageIndex = 0;

...那么您可以实现 nextImage< /code> 和 previousImage 类似:

- (UIImage*) nextImage {
    currentImageIndex = (currentImageIndex + 1) % [imageArray count];
    return [imageArray objectAtIndex:currentImageIndex];
}

- (UIImage*) previousImage {
    currentImageIndex--;
    if (currentImageIndex < 0) {
        currentImageIndex = [imageArray count] - 1;
    }

    return [imageArray objectAtIndex:currentImageIndex];
}

然后只要你想单步遍历数组,就使用 nextImagepreviousImage ,问题就解决了。

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 called imageArray and use a simple variable to keep track of the index of your current image, like:

int currentImageIndex = 0;

...then you might implement nextImage and previousImage like:

- (UIImage*) nextImage {
    currentImageIndex = (currentImageIndex + 1) % [imageArray count];
    return [imageArray objectAtIndex:currentImageIndex];
}

- (UIImage*) previousImage {
    currentImageIndex--;
    if (currentImageIndex < 0) {
        currentImageIndex = [imageArray count] - 1;
    }

    return [imageArray objectAtIndex:currentImageIndex];
}

Then just use nextImage and previousImage whenever you want to step through the array, and problem solved.

2025-01-03 05:09:08

很简单就能做到。您需要做的就是创建一个检查来查看是否位于最后一个元素,如果是,则再次将跟踪器(如 count 或 i 等)设置为 0,

这是伪代码
//设置索引

if ( array [ index ] == len(array) - 1) //at end
{
  index = 0
}

if(array [index] == -1)//at beginning
{
  index = len(array) -1
}
// do something with array[index]

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

if ( array [ index ] == len(array) - 1) //at end
{
  index = 0
}

if(array [index] == -1)//at beginning
{
  index = len(array) -1
}
// do something with array[index]
幽梦紫曦~ 2025-01-03 05:09:08

我想提出这个解决方案:

所选 UIImage 的属性,保存当前索引的 NSInteger 属性。

-(IBAction) nextButtonTapped:(id)sender
{
    self.currentIndex = self.currentIndex++ % [self.images count];
    self.selectedImage= [self.images objectAtIndex:self.currentIndex];
    [self reloadImageView];
}

-(IBAction) previousButtonTapped:(id)sender
{
    self.currentIndex--;
    if (self.currentIndex < 0)
        self.currentIndex += [self.images count];
    self.selectedImage= [self.images objectAtIndex:self.currentIndex];
    [self reloadImageView];
}

-(void)reloadImageView
{
    //do, what is necessary to display new image. Animation?

}

I want to propose this solution:

A property for the selected UIImage, a NSInteger property to hold the current index.

-(IBAction) nextButtonTapped:(id)sender
{
    self.currentIndex = self.currentIndex++ % [self.images count];
    self.selectedImage= [self.images objectAtIndex:self.currentIndex];
    [self reloadImageView];
}

-(IBAction) previousButtonTapped:(id)sender
{
    self.currentIndex--;
    if (self.currentIndex < 0)
        self.currentIndex += [self.images count];
    self.selectedImage= [self.images objectAtIndex:self.currentIndex];
    [self reloadImageView];
}

-(void)reloadImageView
{
    //do, what is necessary to display new image. Animation?

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文