swift - 如何通过单击下一步按钮添加和查看数组中下一张图像的图像

标签 swift xcode

我已经创建了一个图像数组 (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/

相关文章:

ios - Swift:调用函数时创建 UI

objective-c - 导入 modulename-Swift.h 文件到 ObjC .h 文件

ios - Firebase 安全规则 : . indexOn 唯一 ID

c++ - 在 cocos2d-x 中暂停特定场景

swift - 如何在Xcode 11.1中创建使 View Controller 全屏显示的Segue

ios - iOS (9+) 中使用 nibs (.xib) 和 Swift 5/Xcode 11.3 的 View 半径不一致

ios - 如果将代码共享给其他系统,我是否需要在代码中安装 alamofire

ios - 在 iOS 8 中的 Apple 状态栏上方显示 UIView

swift - 无法将 3D 模型作为 SCNGeometry 提供给 MDLMesh 容器

ios - UIImageView 加倍并改变大小