-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewController.swift
101 lines (85 loc) · 3.05 KB
/
ViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//
// ViewController.swift
// app1
//
// Created by Sara Anwar on 7/4/19.
// Copyright © 2019 Sara Anwar. All rights reserved.
//
import UIKit
class ViewController:
UIViewController {
var currentValue = 0
var targetValue = 0
var score = 0
var round = 0
@IBOutlet weak var slider : UISlider!
@IBOutlet weak var targetLabel : UILabel!
@IBOutlet weak var scoreLabel : UILabel!
@IBOutlet weak var roundLabel : UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let roundedValue = slider.value.rounded()
currentValue = Int(roundedValue)
startNewGame()
// Do any additional setup after loading the view.
let thumbImageNormal = #imageLiteral(resourceName: "SliderThumb-Normal")
slider.setThumbImage(thumbImageNormal, for: .normal)
let thumbImageHighlighted = #imageLiteral(resourceName: "SliderThumb-Highlighted")
slider.setThumbImage(thumbImageHighlighted, for: .highlighted)
let insets = UIEdgeInsets(top: 0, left: 14, bottom: 0, right: 14)
let trackLeftImage = #imageLiteral(resourceName: "Button-Highlighted")
let trackLeftResizable = trackLeftImage.resizableImage(withCapInsets: insets)
slider.setMinimumTrackImage(trackLeftResizable, for: .normal)
let trackRightImage = #imageLiteral(resourceName: "SmallButton")
let trackRightResizable = trackRightImage.resizableImage(withCapInsets: insets)
slider.setMaximumTrackImage(trackRightResizable, for: .normal)
}
@IBAction func showAlert(){
let difference = abs(targetValue - currentValue)
var points = 100 - difference
score += points
let title : String
if difference == 0 {
title = "Perfect!"
points += 100
} else if difference < 5 {
title = " You almost had it!"
if difference == 1 {
points += 50
}
}else if difference < 10{
title = "Pretty good!"
} else {
title = "Not even close..."
}
let message = "You scored \(points) points"
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default, handler: {
action in
self.startNewRound()
})
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
@IBAction func sliderMoved(_ slider:UISlider){
let roundedValue = slider.value.rounded()
currentValue = Int(roundedValue)
}
func startNewRound() {
round+=1
targetValue = Int.random(in: 1...100)
currentValue = 50
slider.value = Float(currentValue)
updateLabels()
}
func updateLabels(){
targetLabel.text = String(targetValue)
scoreLabel.text = String(score)
roundLabel.text = String(round)
}
@IBAction func startNewGame(){
score = 0
round = 0
startNewRound()
}
}