2016-07-03 4 views
-1

ボタンを押すたびに、または誰かがアプリケーションを開くと自動的に画像を表示する配列を作成する方法。この問題を解決するのに役立つ任意のコード。シーケンス内の画像を表示する画像の配列を作成する方法(swift2、xcode7)

@IBOutlet var imageview: UIImageView! 

    var picture:[UIImage] = [ 
    UIImage(named: "page3.JPG")!, 
    UIImage(named: "page4.JPG")!, 
     ] 

    @IBAction func buttton(sender: AnyObject) { 
    } 

答えて

1

以下 コード、コードは、選択された画像を追跡するために他の変数を必要とします。現在のイメージは、現在のインデックスに従って表示されます。ボタンが押されるとインデックスがインクリメントされ、ピクチャは新しいインデックスを使用するように更新されます。コードでは、インデックスが配列内の項目数を超えないようにする必要があります。

アプリの起動時に画像を表示するには、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] 
} 
関連する問題