我已经创建了一个图像数组 (imagelateral),我想在 Xcode 中实现下一个按钮,但是我无法查看数组中的图像以获取数组中的下一个图像
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
let images: [UIImage] = [#imageLiteral(resourceName: "tub"),#imageLiteral(resourceName: "ball"),#imageLiteral(resourceName: "apple"),#imageLiteral(resourceName: "igloo"),#imageLiteral(resourceName: "frog")]
var i : Int = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func nextButton(_ sender: UIButton) {
if(i+1 > images.count){
i = 0
}
//self.imageView.image = [images images[i]:i]
imageView.image = UIImage [images images[i]:i]
}
}
它应该显示数组中的下一张图片 示例 1:单击下一步按钮时显示“tub”,然后显示“ball”
最佳答案
nextButton Action 有错误。你的数组给你 UIImage,你不需要创建新的 UIImage 来设置你的 imageView。如果您使用以下代码更改 nextButton 操作代码,也许可以工作。
@IBAction func nextButton(_ sender: UIButton) {
i = (i+1)%images.count
imageView.image = images[i]
}
关于swift - 如何通过单击下一步按钮添加和查看数组中下一张图像的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56804651/