以下 コード、コードは、選択された画像を追跡するために他の変数を必要とします。現在のイメージは、現在のインデックスに従って表示されます。ボタンが押されるとインデックスがインクリメントされ、ピクチャは新しいインデックスを使用するように更新されます。コードでは、インデックスが配列内の項目数を超えないようにする必要があります。
アプリの起動時に画像を表示するには、viewWillAppear
が呼び出されたときに画像を更新します。
例:
@IBOutlet var imageView: UIImageView!
// Index to keep track of the current image.
var index = 0
let picture:[UIImage] = [
UIImage(named: "page3.JPG")!,
UIImage(named: "page4.JPG")!,
]
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// Update the image just before the view becomes visible, using the current image.
imageView = picture[index]
}
@IBAction func button(sender: AnyObject) {
// Increment the index to the next image.
index += 1
// If the index goes to the end of the array, then go back to the first image.
if (index == picture.count) {
index = 0
}
// Update the image view to show the current image.
imageView.image = picture[index]
}