-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhaptic-input urgency var
More file actions
64 lines (57 loc) · 2.36 KB
/
haptic-input urgency var
File metadata and controls
64 lines (57 loc) · 2.36 KB
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
import UIKit
class SortingGameViewController: UIViewController {
// MARK: - Properties
private let images = [
"plastic-bottle",
"metal-can",
"cardboard-box",
"other-trash"
]
private let hapticFeedback = UIImpactFeedbackGenerator(style: .heavy)
private var currentImageIndex = 0
private var score = 0
private var urgency = 0
// MARK: - Outlets
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var scoreLabel: UILabel!
// MARK: - View Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
// Set the initial image and score
imageView.image = UIImage(named: images[currentImageIndex])
scoreLabel.text = "Score: 0"
// Add a long press gesture recognizer to the image view
let gestureRecognizer = UILongPressGestureRecognizer(target: self,
action: #selector(handleLongPress(gestureRecognizer:)))
gestureRecognizer.minimumPressDuration = 0.1
imageView.addGestureRecognizer(gestureRecognizer)
}
// MARK: - Private
@objc private func handleLongPress(gestureRecognizer: UILongPressGestureRecognizer) {
// Update the urgency value based on the pressure of the user's touch
if gestureRecognizer.state == .began {
urgency = Int(gestureRecognizer.pressure * 10)
hapticFeedback.impactOccurred()
} else if gestureRecognizer.state == .ended {
// Check if the urgency value matches the correct answer for the current image
let correctAnswer = urgency % images.count
if correctAnswer == currentImageIndex {
// Update the score and move on to the next image
score += 1
scoreLabel.text = "Score: \(score)"
currentImageIndex = (currentImageIndex + 1) % images.count
imageView.image = UIImage(named: images[currentImageIndex])
} else {
// Show an alert to inform the user that their answer was incorrect
let alertController = UIAlertController(title: "Incorrect Answer",
message: "Sorry, that was the wrong answer. Try again.",
preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default)
alertController.addAction(okAction)
present(alertController, animated: true)
}
// Reset the urgency value
urgency = 0
}
}
}