在 Xcode 中创建带有下一个和上一个按钮的图像数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5430007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating an image array with next and previous buttons in Xcode
提问by Ian
I am very new to iPhone dev and while going through Sams Teach Yourself iPhone App Dev in 24 Hours I have loads of questions.
我对 iPhone 开发非常陌生,在 24 小时内通过 Sams 自学 iPhone 应用开发时,我有很多问题。
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.
基本上,我想设计一个包含 30-100 或更多图像的应用程序,并带有上一个和下一个按钮以在数组中浏览它们。
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
伊恩
回答by Vaibhav Tekam
You can have an array of imageNames;
你可以有一个 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
然后在点击下一个和上一个按钮时,您可以维护一个索引变量,并且可以将图像设置为 imageView
[yourImageView setImage:[UIImage imageNamed:[images objectAtIndex:yourIndex]]];
Hope this helps.
希望这可以帮助。
回答by HenriqueAdriano
//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 <=
回答by Connor Rodriguez
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.
你将不得不制作一个 NSInteger (随便你怎么称呼它)。让我们称之为改变。
So to change the sound with the image do it like this. every time you click the button - change += 1;
因此,要更改图像的声音,请这样做。每次单击按钮时 - 更改 += 1;
and then..
进而..
if (change == 1) {
如果(改变== 1){
--sound code here--
--这里的声音代码--
--change button image here--
--在此处更改按钮图像--
}
}