Hello,
I have tried the dispatch queue. I don't know if I did that right.
Game is a public classe that is in another source file in the playground. And checkVictory is a method of that classe.
import Foundation
import UIKit
public class Game {
public var word: String
public var clue: String
public var numberOfTriesLeft = 6
public init(word: String, clue: String) {
self.word = word
self.clue = clue
}
public func checkLetterInWord(letter: String) -> [Int] {
let wordCharacters = Array(word)
var indexesInWord: [Int] = []
var i = 0
for char in wordCharacters {
//print("\(char) and \(letter)")
if char == Character(letter) {
indexesInWord.append(i)
}
i += 1
}
return indexesInWord
}
public func checkVictory(label: String) -> Bool {
for char in label {
if char == "?" {
return false
}
}
return true
}
}
The function updateWordLabelAndCheckVictory calls presentVictoryView method.
This is how I am using queues:
func updateWordLabelAndCheckVictory(letter: String, indexArray: [Int]) {
if let currentLabel = wordLabel.text {
var wordArray = Array(currentLabel)
for i in indexArray {
wordArray[i] = Character(letter)
}
DispatchQueue.main.async { [weak self] in
self?.wordLabel.text = String(wordArray)
}
if (game.checkVictory(label: wordLabel.text!)) {
print("Aqui")
currentGame += 1
DispatchQueue.main.async { [weak self] in
self?.presentVictoryView()
}
}
}
}
func presentVictoryView() {
DispatchQueue.main.async { [weak self ] in
self?.blackView.isHidden = false
for _ in 0 ... 30 {
let objectView = UIView()
objectView.translatesAutoresizingMaskIntoConstraints = false
objectView.frame = CGRect(x: 0, y: 0, width: 20, height: 100)
objectView.backgroundColor = .clear
//objectView.alpha = CGFloat(0.9)
objectView.isHidden = false
let ballon = UILabel()
ballon.translatesAutoresizingMaskIntoConstraints = false
ballon.frame = CGRect(x: 0, y: 0, width: 20, height: 100)
ballon.backgroundColor = .clear
//ballon.alpha = CGFloat(0.9)
ballon.text = ""
ballon.font = UIFont.systemFont(ofSize: 60)
objectView.addSubview(ballon)
NSLayoutConstraint.activate([
ballon.centerXAnchor.constraint(equalTo: objectView.centerXAnchor),
ballon.centerYAnchor.constraint(equalTo: objectView.centerYAnchor)
])
self?.blackView.addSubview(objectView)
let randomXOffset = Int.random(in: -120 ..< 200)
let path = UIBezierPath()
path.move(to: CGPoint(x: 160 + randomXOffset, y: 1000))
path.addCurve(to: CGPoint(x: 100 + randomXOffset, y: -300), controlPoint1: CGPoint(x: 300 - randomXOffset, y: 600), controlPoint2: CGPoint(x: 70 + randomXOffset, y: 300))
let animation = CAKeyframeAnimation(keyPath: "position")
animation.path = path.cgPath
animation.repeatCount = 1
let random = Double.random(in: 3.0 ..< 7.0)
animation.duration = random
//animation.timeOffset = Double(arc4random_uniform(50))
objectView.layer.add(animation, forKey: "animate position along path")
}
self?.newGame()
}
}
Could you tell me how I should use the queues?