【问题标题】:Swift: How to change a UIImage with a previous and next button?Swift:如何使用上一个和下一个按钮更改 UIImage?
【发布时间】:2021-12-25 18:39:48
【问题描述】:

我有一个显示当前图像的功能和两个按钮(上一个和下一个)来显示上一个图像或下一个图像。但是我的代码有一些错误。请帮忙。泰。

import UIKit

class ViewController: UIViewController {

    

    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!")
        }
    }

    
}

【问题讨论】:

  • 你不应该使用字典。您应该使用数组来存储数据。
  • 但是需要使用字典。
  • 你确定它必须是一本字典吗?这似乎是个坏主意——字典是无序的。

标签: ios swift dictionary button uiimageview


【解决方案1】:

我会使用一个字典数组:

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()
    }
}

您也可以在无法后退或前进时禁用按钮。

【讨论】:

    【解决方案2】:

    字典不是完成这项工作的正确工具,但如果您必须使用字典,您至少应该确保该键比 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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-09
      • 2016-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多