[Swift] How to display when the quiz app answers correctly [Beginner]

Introduction

I didn't know how to display it when I answered correctly with the quiz app, so I looked it up and summarized it.

I have no practical experience and am studying Swift by myself. I would like to output what I have learned. As a beginner, there may be mistakes. At that time, I would appreciate it if you could tell me.

environment

macOS Catalina10.15.4 Xcode11.7

Completion drawing

sampleMove.gif

Set the screen as shown in the image below on the story board

sample1.png

Connect the code with UIImage and UIButton.

 @IBOutlet weak var answerBtn1: UIButton!
 @IBOutlet weak var answerBtn2: UIButton!
 @IBOutlet weak var answerBtn3: UIButton!
 @IBOutlet weak var answerBtn4: UIButton!
    
 @IBOutlet weak var maruImageView: UIImageView!

UIButton also connects with @IBAction

 @IBAction func answerBtn1Action(_ sender: Any) {
    }
    

    @IBAction func answerBtn2Action(_ sender: Any) {
        //I will not use it this time, but originally write the process here
    }
    
    
    @IBAction func answerBtn3Action(_ sender: Any) {
        //I will not use it this time, but originally write the process here
    }
    
    @IBAction func answerBtn4Action(_ sender: Any) {
        //I will not use it this time, but originally write the process here

    }

○ Store images (maru.png)

sample2.png

Processing when the answer is correct

1, ○ Display image

Write the code inside answerBtn1Action

//○ Display the image
maruImageView.image = UIImage(named: "maru.png ")
//○ Restore the transmittance of the image. When you press the button from the second time onward, if you do not write this, the image will remain transparent.
maruImageView.alpha = 1.0

2. When the answer is correct, make the button color blue

answerBtn1.backgroundColor = .blue

After 3 or 1 seconds, erase the ○ image and the button color.

At this time, use the UIView animation.

//UIView.Color returns after 1 second using animate function
 UIView.animate(withDuration: 1.0) {
      //Change button color to systemOrenge
      self.answerBtn1.backgroundColor = .systemOrange
      //○ Set the transmittance of the image to zero. Zero and transparent.
      self.maruImageView.alpha = .zero
}

Full code


import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var answerBtn1: UIButton!
    @IBOutlet weak var answerBtn2: UIButton!
    @IBOutlet weak var answerBtn3: UIButton!
    @IBOutlet weak var answerBtn4: UIButton!
    
    @IBOutlet weak var maruImageView: UIImageView!
    
    

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    //Processing when the answer is correct
    @IBAction func answerBtn1Action(_ sender: Any) {
        print("Correct answer")
        answerBtn1.backgroundColor = .blue
        //○ Display image
        maruImageView.image = UIImage(named: "maru.png ")
        //○ Restore the transmittance of the image
        maruImageView.alpha = 1.0
        
        //UIView.Color returns after 1 second using animate function
        UIView.animate(withDuration: 1.0) {
            //Change button color to systemOrenge
            self.answerBtn1.backgroundColor = .systemOrange
            //○ Set the transmittance of the image to zero. Zero and transparent.
            self.maruImageView.alpha = .zero
        }
    }
    
    
    @IBAction func answerBtn2Action(_ sender: Any) {
        //I will not use it this time, but originally write the process here
    }
    
    
    @IBAction func answerBtn3Action(_ sender: Any) {
        //I will not use it this time, but originally write the process here
    }
    
    @IBAction func answerBtn4Action(_ sender: Any) {
        //I will not use it this time, but originally write the process here

    }
    
}

Reference site

https://qiita.com/hachinobu/items/57d4c305c907805b4a53

Recommended Posts

[Swift] How to display when the quiz app answers correctly [Beginner]
How to add sound in the app (swift)
[Swift] How to link the app with Firebase
[Swift] How to display the entered characters in Widget via UserDefaults when using WidgetKit
How to transition from the [Swift5] app to the iPhone settings screen
[Rails 5] How to display the password change screen when using devise
[Swift] How to implement the countdown function
[Swift] How to implement the LINE login function
[swift5] How to implement the Twitter share function
How to delete the database when recreating the application
[Swift] How to implement the fade-in / out function
How to display the result of form input
[swift] How to control the behavior when the back button of NavigationBar is pressed
[swift5] How to transition from the app to an external site by specifying the URL
[Swift] How to change the order of Bar Items in Tab Bar Controller [Beginner]
How to run the SpringBoot app as a service
How to add characters to display when using link_to method
[Beginner] Discover the N + 1 problem! How to use Bullet
[Swift] Use UserDefaults to save data in the app
[swift5] How to execute processing when tabBar is tapped
[Swift] How to get the document ID of Firebase
[Rails] How to display an image in the view
[Android] How to check if the Google Play developer service is installed when the app is launched
How to correctly check the local HTML file in the browser
How to set chrony when the time shifts in CentOS7
Android app to select and display images from the gallery
How to specify db when creating an app with rails
How to cure the time dilation when launching Japaneseized Kibana
[Rails] How to display the list of posts by category
How to get the log when install4j does not start
Learn how to customize the Navigation Bar from the sample app
[Rails] How to display information stored in the database in view
How to use JUnit (beginner)
[Swift] How to use UserDefaults
How to use Swift UIScrollView
[Java] How to display Wingdings
When a beginner makes a personal app with rails, the procedure to bring it to the starting point anyway.
[Swift] How to dynamically change the height of the toolbar on the keyboard
I want to create a chat screen for the Swift chat app!
[Swift5] How to get an array and the complement of arrays
[Swift 5] Processing and precautions for transitioning to the iOS settings app
How to display 0 on the left side of the standard input value
[Swift] How to implement the Twitter login function using Firebase UI ①
How to output the value when there is an array in the array
How to display products by category on the same list screen
[Swift UI] How to get the startup status of the application [iOS]
[Rails] How to omit the display of the character string of the link_to method
[jOOQ] How to CASE WHEN in the WHERE / AND / OR clause
[Behavior confirmed in December 2020] How to implement the alert display function
How to solve the unknown error when using slf4j in Java
[Swift] How to implement the Twitter login function using Firebase UI ②
How to write the view when Vue is introduced in Rails?
[Swift] How to reflect the array changed at the transition destination to the transition source and display it on Label etc.
How to use the link_to method
How to use the include? method
How to use the form_with method
[rails] How to display db information
How to find the average angle
How to use the wrapper class
[Swift] How to use SwiftLint (cocoapods)
[Swift] How to use Unwind segue