Created
August 27, 2020 20:17
-
-
Save EricGustin/38bb1beaff08f48f7e08429f5c71e7c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| class ViewController: UIViewController { | |
| private var healthBar: UIProgressView! | |
| private var enemy: UIImageView! | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| setUpSubviews() | |
| } | |
| private func setUpSubviews() { | |
| enemy = UIImageView(image: UIImage(named: "enemy@4x")) | |
| enemy.isUserInteractionEnabled = true // Allows us to click on the image | |
| enemy.translatesAutoresizingMaskIntoConstraints = false | |
| view.addSubview(enemy) // Always add a subview before creating its constraints | |
| // Makes the enemy centered on the screen and half the width of the screen | |
| enemy.centerYAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerYAnchor).isActive = true | |
| enemy.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor).isActive = true | |
| enemy.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width/2).isActive = true | |
| enemy.heightAnchor.constraint(equalToConstant: UIScreen.main.bounds.width/2).isActive = true | |
| healthBar = UIProgressView() | |
| healthBar.trackTintColor = view.backgroundColor // The color shown for the portion of the progress bar that is not filled | |
| healthBar.progressTintColor = .red // The color shown for the portion of the progress bar that is filled. | |
| healthBar.progress = 1.0 // Initial state of the healthBar will be full | |
| healthBar.translatesAutoresizingMaskIntoConstraints = false | |
| view.addSubview(healthBar) // Always add a subview before creating its constraints | |
| healthBar.centerXAnchor.constraint(equalTo: enemy.centerXAnchor).isActive = true | |
| healthBar.bottomAnchor.constraint(equalTo: enemy.topAnchor).isActive = true | |
| healthBar.widthAnchor.constraint(equalTo: enemy.widthAnchor).isActive = true | |
| healthBar.heightAnchor.constraint(equalTo: enemy.widthAnchor, multiplier: 0.125).isActive = true | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment