开发者

Creating an image array with next and previous buttons in Xcode

开发者 https://www.devze.com 2023-02-19 10:58 出处:网络
I am very new to iPhone dev and while going through Sams Teach Yourself iPhone App Dev in 24 Hours I 开发者_JAVA百科have loads of questions.

I am very new to iPhone dev and while going through Sams Teach Yourself iPhone App Dev in 24 Hours I 开发者_JAVA百科have loads of questions.

Basically, I want to design an app that has anywhere from 30-100 or more images, with a previous and next button to go through them in an array.

I've looked at random bits of example code but I'm having a hard time figuring out how to do it. What is the easiest and most efficient method to go back and forth between these images in one view? The other problem I have is that I want there to be a button on the view which when pressed will allow the user to set the current image as the wallpaper/screensaver of the device.

I know this is an extremely easy answer for most, but I am a designer by trade and code is something I struggle with.

Thanks in advance!

Ian


You can have an array of imageNames;

eg.

NSArray * images = [[NSArray alloc] initWithObjects:@"image1.png",@"image2.png",@"image3.png",@"image4.png",@"image5.png",nil];

and then on next and previous button hits you can maintain an index variable and you can set the image to the imageView

[yourImageView setImage:[UIImage imageNamed:[images objectAtIndex:yourIndex]]];

Hope this helps.


//First you need to global variable.. maybe.. rs

@implementation FirstViewViewController{   
    int indexImage;  // <= important..
}

// This is your array.... <=

NSArray * images = [[NSArray alloc] initWithObjects:@"image1.png",@"image2.png",@"image3.png",@"image4.png",@"image5.png",nil];

//and sample next button do this...

- (IBAction)changeImage:(id)sender {

    if ((indexImage+1) > arrayImages.count) {
        indexImage = 0;
    }
    self.imgLoadImage.image = [images objectAtIndex:indexImage];
    indexImage++;
}
// This guy "imgLoadImage" is an imageView on your screen  <=


The way to play a sound with a selected image is going to be a little different then described above.

Your going to have to make an NSInteger (call it what ever you want). lets call it change.

So to change the sound with the image do it like this. every time you click the button - change += 1;

and then..

if (change == 1) {

--sound code here--

--change button image here--

}

0

精彩评论

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

关注公众号