簡體   English   中英

Swift:如何使用上一個和下一個按鈕更改 UIImage?

[英]Swift: How to change a UIImage with a previous and next button?

我是一名學生,是 swift 的新手。 這項作業要求我為 iOS 創建一個應用程序,該應用程序顯示圖像以及相關的描述性文本。 我有 10 張圖像,它們自己的描述存儲在字典中。 我有一個 ImageView 和 textLabel 。 我有一個顯示當前圖像的功能和兩個按鈕(上一個和下一個)來顯示上一個圖像或下一個圖像。 但是我的代碼有一些錯誤。 請幫忙。 泰。

import UIKit

class ViewController: UIViewController {

    var count = 0

    var photoCollection : [UIImage: String] = [ // store into dictionary
        UIImage(named: "P1")!: "City Tavern Bathroom",
        UIImage(named: "P2")!: "Shafer Trail, Island in the Sky District",
        UIImage(named: "P3")!: "Rivers Bend Group Campground",
        UIImage(named: "P4")!: "Delta at Lake Mead",
        UIImage(named: "P5")!: "Deer between Sequoias",
        UIImage(named: "P6")!: "Arlington House, The Robert E. Lee Memorial",
        UIImage(named: "P7")!: "Brink of the Lower Falls of the Yellowstone River",
        UIImage(named: "P8")!: "Garage Exterior",
        UIImage(named: "P9")!: "DSCF1199",
        UIImage(named: "P10")!: "The Bi-national Formation"
    ]

    @IBOutlet weak var photo: UIImageView!
    @IBOutlet weak var Text: UILabel!

    func showImage() {
        if count < photoCollection.count {
            if let images = photoCollection[count] as? Dictionary<UIImage, String> {
                photo.image = images.keys.first
                Text.text = images.values.first
            }
        } else {
            debugPrint("Failed!")
        }
    }

    @IBAction func Previous(_ sender: UIButton) {
        count = count - 1
        showImage()
    }


    @IBAction func Next(_ sender: UIButton) {
        count = count + 1
        showImage()
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
}

字典不是適合這項工作的工具,但如果您必須使用字典,您至少應該確保鍵比 UIImage 更用戶友好。 如果你定義了一個類型來保存圖像和文本,你可以使用一個整數來鍵入字典來訪問你需要的值。

class ViewController1: UIViewController {

    struct Photo {
        let image: UIImage?
        let text: String
    }

    var count = 0

    var photoCollection: [Int: Photo] = [
        0: Photo(image: UIImage(named: "P1"), text: "City Tavern Bathroom"),
        1: Photo(image: UIImage(named: "P2"), text: "Shafer Trail, Island in the Sky District"),
        2: Photo(image: UIImage(named: "P3"), text: "Rivers Bend Group Campground"),
        3: Photo(image: UIImage(named: "P4"), text: "Delta at Lake Mead"),
        4: Photo(image: UIImage(named: "P5"), text: "Deer between Sequoias"),
        5: Photo(image: UIImage(named: "P6"), text: "Arlington House, The Robert E. Lee Memorial"),
        6: Photo(image: UIImage(named: "P7"), text: "Brink of the Lower Falls of the Yellowstone River"),
        7: Photo(image: UIImage(named: "P8"), text: "Garage Exterior"),
        8: Photo(image: UIImage(named: "P9"), text: "DSCF1199"),
        9: Photo(image: UIImage(named: "P10"), text: "The Bi-national Formation"),
    ]

    func showImage() {
        guard let item = photoCollection[count] else { return }
        photo.image = item.image
        Text.text = item.text
    }
}

我會使用一系列字典:

import UIKit

class ViewController: UIViewController {
    var count = 0

    var photoCollection: [[String:Any]] = [
        ["image": UIImage(named: "P1")!, "text": "City Tavern Bathroom"],
        // Other photos
    ]

    @IBOutlet weak var photo: UIImageView!
    @IBOutlet weak var Text: UILabel!

    func showImage() {
        photo.image = photoCollection[count]["image"] as! UIImage
        Text.text = photoCollection[count]["text"] as! String
    }

    @IBAction func Previous(_ sender: UIButton) {
        guard count > 0 else {return}
        count -= 1
        showImage()
    }

    @IBAction func Next(_ sender: UIButton) {
        guard count < photoCollection.count - 1 else {return}
        count += 1
        showImage()
    }
}

當無法后退或前進時,您也可以禁用按鈕。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM