English 中文(简体)
住宿 从结束到开始,可互换的ui
原标题:loop Nsmutable arrray of uiimages from end to beginning

我有一阵形图象,上面和下图显示不同的形象,但当我到阵列结束时模拟器坠毁时。 我想把阵列的末尾放到一开始,以便当我再次打下台子时,在一阵景中,我回到了第一个形象,如果我打上了上台的ton子,那我就把最后的形象 lo上了,而没有发生故障。

最佳回答

你们想要的是循环阵列,它很容易使用标准NSMutable Array。 例如,你说,你把图像储存在一个叫做imageArray的阵列中,并使用一个简单的变量跟踪你当前形象指数,例如:

int currentImageIndex = 0;

页: 1

- (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];
}

然后,只要你希望穿过阵列,就只使用<条码>。

问题回答

仅够做。 你们都需要做的是检查,看看你是否重新掌握最后的内容,如果是的话,再把你的轨迹(如点数或一等) set到零,

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]

我想提出这一解决办法:

选定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?

}




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签