Similar question to the one I asked last night. But I have spent the day reading and tidying up my code. I am trying to make it so that a random square in a grid has it's alpha setting changed to 0. The code runs but all of the squares appear as they should, none of them become transparent. Please help!
Just to let you know when I print out the random element what is get in the output box is - UIImage:0x600000d0c7e0 named(main: gridsquare3) {120, 170}
Thank you
'''
let cellImages: [UIImage] = [UIImage(named: "gridsquare")!,UIImage(named: "gridsquare2")!,
UIImage(named: "gridsquare3")!,
UIImage(named: "gridsquare4")!,UIImage(named: "gridsquare5")!,UIImage(named: "gridsquare6")!,UIImage(named: "gridsquare7")!,UIImage(named: "gridsquare8")!,
UIImage(named: "gridsquare9")!,]
override func viewDidLoad() {
super.viewDidLoad()
collectionView.dataSource = self
collectionView.delegate = self
//Timer creation
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timerElapsed), userInfo: nil, repeats: true)
// Do any additional setup after loading the view.
}
//Mark: - Timer methods
@objc func timerElapsed() {
seconds -= 1
timerLabel.text = "\(seconds)"
if seconds <= 0 {
timer?.invalidate()}
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return cellImages.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! CollectionViewCell
cell.yellowSquare.image = cellImages[indexPath.item]
let randomcell = cellImages.randomElement()
print(randomcell!)
if randomcell == UIImage(named: "gridsquare") {
cell.alpha = 0
}
else if randomcell == UIImage(named: "gridsquare2") {
cell.alpha = 0 }
else if randomcell == UIImage(named: "gridsquare3") {
cell.alpha = 0 }
else if randomcell == UIImage(named: "gridsquare4") {
cell.alpha = 0 }
else if randomcell == UIImage(named: "gridsquare5") {
cell.alpha = 0 }
else if randomcell == UIImage(named: "gridsquare6") {
cell.alpha = 0 }
else if randomcell == UIImage(named: "gridsquare7") {
cell.alpha = 0 }
else if randomcell == UIImage(named: "gridsquare8") {
cell.alpha = 0 }
else if randomcell == UIImage(named: "gridsquare9") {
cell.alpha = 0 }
else {
print("failure")
}
return cell
}
}
'''
Aucun commentaire:
Enregistrer un commentaire