[英]How do I change a UIImage for another UIImage by clicking a button in Swift 3?
[英]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.