简体   繁体   English

如何通过单击Swift 3中的按钮来更改另一个UIImage的UIImage?

[英]How do I change a UIImage for another UIImage by clicking a button in Swift 3?

I'm trying to make an app for kids so that they can make a relationship between images and words. 我正在尝试为孩子们制作一个应用程序,以便他们可以在图像和单词之间建立联系。 A UIImage (representing an everyday object) appears on a UIImageView and the kids have to type on the text field the name of the object they're seeing (eg there's a yellow car with a luminous top sign on the view and they have to type 'taxi'). 一个UIImage (代表一个日常对象)出现在UIImageView ,孩子们必须在文本字段中键入他们所看到的对象的名称(例如,一辆黄色的汽车在视图上带有发光的顶部标志,他们必须键入'出租车')。 Then, they click on a 'next' button and a new image appears. 然后,他们单击“下一步”按钮,将出现一个新图像。 I've been looking through this site, and this is the code I have right now: 我一直在浏览此站点,这是我现在拥有的代码:

class wordPracticeViewController: UIViewController{
    let currentWord = "taxi"
    var imageName = "taxi.png"
    var answer : String = ""

    var image = UIImage()
    var imageView = UIImageView()

    @IBOutlet weak var inputText: UITextField!
    @IBOutlet weak var ima: UIImageView!

    override func viewDidLoad()
    {
        super.viewDidLoad()

        image = UIImage(named: imageName)!
        imageView = UIImageView(image: image)
        imageView.frame = CGRect(x: 160, y: 80, width: 100, height: 200)
        view.addSubview(imageView)
    }

    @IBAction func answer(_ sender: Any)
    {
        answer = inputText.text!
        print("\(answer)")

        if answer == currentWord
        {
            imageName = "blanket.png"
            imageView.image = UIImage(named: "blanket")
        }
    }
}

Now, my biggest problem is that I want the image to change whether they spell the word correctly or not, because I'm planning to save the results and when they're done playing, the app will show them their scores, the words they spelled correctly and the ones they didn't. 现在,我最大的问题是我希望图像改变他们是否正确拼写单词,因为我正计划保存结果,并且当它们完成播放后,应用程序将向他们显示他们的乐谱,单词拼写正确,而拼写错误。 Of course, I need that a relationship between the word and the image exists (if not, how are they going to get the right word for the right image?), that's why I put the condition answer == currentWord , but in the long term that's not what I need. 当然,我需要单词和图像之间存在某种关系(如果不存在,它们将如何为正确的图像获得正确的单词?),这就是为什么我把条件answer == currentWord ,但是answer == currentWord这个不是我所需要的

With my current code, the image only changes if they have the right word & spelled correctly. 使用我当前的代码,只有在单词和拼写正确的情况下,图片才会更改。 So, how can I change UIImages in a view with a click, whether they've spelled the word correctly or not, keeping the relationship between image and word? 那么,如何通过单击更改视图中的UIImage,无论它们是否正确拼写了单词,又保持了图像和单词之间的关系?

How about creating a class which would include as properties: image file name, correct answer word, and an array of acceptable misspelled words? 如何创建一个包含属性的类:图像文件名,正确的答案词和可接受的拼写错误的单词数组? Then in the answer function, you check if the text input matches any of the acceptable misspellings or the correct answer, or is incorrect. 然后,在answer功能中,检查文本输入是否与任何可接受的拼写错误或正确答案匹配,或不正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM