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.
macOS Catalina10.15.4 Xcode11.7
@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
}
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
answerBtn1.backgroundColor = .blue
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
}
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
}
}
https://qiita.com/hachinobu/items/57d4c305c907805b4a53
Recommended Posts