Skip to content

Commit

Permalink
fixed cells
Browse files Browse the repository at this point in the history
  • Loading branch information
lukehasawii committed Apr 26, 2023
1 parent bdefb97 commit 8f55d11
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 42 deletions.
18 changes: 15 additions & 3 deletions SoundboardQuiz/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@
<nil key="highlightedColor"/>
</label>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="-1" estimatedSectionHeaderHeight="-1" sectionFooterHeight="-1" estimatedSectionFooterHeight="-1" id="t26-Si-8kE">
<rect key="frame" x="-6" y="170" width="393" height="641"/>
<rect key="frame" x="-6" y="170" width="392" height="641"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" reuseIdentifier="categoryCell" id="N0p-1K-NE1" customClass="CategoryCellTableViewCell" customModule="SoundboardQuiz" customModuleProvider="target">
<rect key="frame" x="0.0" y="50" width="393" height="43.666667938232422"/>
<rect key="frame" x="0.0" y="50" width="392" height="43.666667938232422"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="N0p-1K-NE1" id="Y9C-qI-fgd">
<rect key="frame" x="0.0" y="0.0" width="393" height="43.666667938232422"/>
<rect key="frame" x="0.0" y="0.0" width="392" height="43.666667938232422"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="3NR-GS-yw4">
Expand Down Expand Up @@ -318,9 +318,21 @@
<collectionViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" insetsLayoutMarginsFromSafeArea="NO" id="jx4-11-57y">
<rect key="frame" x="0.0" y="0.0" width="111.00000000000001" height="115"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="njJ-Ga-52s">
<rect key="frame" x="34" y="47" width="42" height="21"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
</collectionViewCellContentView>
<color key="backgroundColor" systemColor="systemRedColor"/>
<size key="customSize" width="111.00000000000001" height="115"/>
<connections>
<outlet property="label" destination="njJ-Ga-52s" id="0Jw-Lg-eeL"/>
</connections>
</collectionViewCell>
</cells>
<connections>
Expand Down
26 changes: 26 additions & 0 deletions SoundboardQuiz/ImageCollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,30 @@ import UIKit
class ImageCollectionViewCell: UICollectionViewCell {
public static let REUSE_IDENTIFIER = "imagePanel";

@IBOutlet var label: UILabel!
private var secretName: String?

public var isMatched = false

public var name: String? {
get{
return secretName
}
set{
secretName = newValue
label.text = secretName
}
}

private var secretImageFile: UIImage?

public var imageFile: UIImage? {
get{
return secretImageFile
}
set{
secretImageFile = newValue
}
}

}
24 changes: 14 additions & 10 deletions SoundboardQuiz/MainGameViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import UIKit
class MainGameViewController: UIViewController {
// @IBOutlet var titleBar: UINavigationItem!

private var embeddedViewController1: CustomViewController!
private var embeddedViewController2: CustomViewController!
private var embeddedViewController1: SquareCollectionViewController!
//private var embeddedViewController2: CustomViewController!

@IBOutlet var containerView1: UIView!
@IBOutlet var containerView2: UIView!
Expand All @@ -31,15 +31,18 @@ class MainGameViewController: UIViewController {
// Do any additional setup after loading the view.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? CustomViewController,
if let vc = segue.destination as? SquareCollectionViewController,
segue.identifier == "soundViewSegue" {
self.embeddedViewController1 = vc
}else if let vc = segue.destination as? CustomViewController, segue.identifier == "imageViewSegue" {
self.embeddedViewController2 = vc
}
}//else if let vc = segue.destination as? SquareCollectionViewController, segue.identifier == "imageViewSegue" {
// self.embeddedViewController2 = vc
//}
}


func stopDragAndGreyOutSoundCell(name: String){
let didSucceed = embeddedViewController1.correctCell(name: name)
print("did succeed? \(didSucceed)")
}

/*
// MARK: - Navigation
Expand All @@ -52,8 +55,9 @@ class MainGameViewController: UIViewController {
*/

}
// This is your custom view controller contained in `MainViewController`.
class CustomViewController: UIViewController {
func myMethod() {}

protocol ClickableCell {
func correctCell(name: String) -> Bool
//DO STUFF HERE TO GREY OUT CELL ETC.

}
26 changes: 26 additions & 0 deletions SoundboardQuiz/SoundCollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,30 @@ class SoundCollectionViewCell: UICollectionViewCell {
public static let REUSE_IDENTIFIER = "soundPanel"

@IBOutlet var label: UILabel!

public var canMove = true

private var secretName: String?

public var name: String? {
get{
return secretName
}
set{
secretName = newValue
label.text = secretName
}
}

private var secretSoundFile: URL?

public var imageFile: URL? {
get{
return secretSoundFile
}
set{
secretSoundFile = newValue
}
}

}
176 changes: 147 additions & 29 deletions SoundboardQuiz/SquareCollectionViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,98 @@

import UIKit

class SquareCollectionViewController: UICollectionViewController, UICollectionViewDragDelegate, UICollectionViewDropDelegate {
class SquareCollectionViewController: UICollectionViewController, UICollectionViewDragDelegate, UICollectionViewDropDelegate, ClickableCell {

func correctCell(name: String) -> Bool {
guard let collectionView = collectionView as? SoundCollectionView else{
print("correctCell attempted on an ImageView cell!")
return false
}
let cells = collectionView.visibleCells

for cell in cells{
if let cell = cell as? SoundCollectionViewCell{
if(cell.name == name){
cell.backgroundColor = UIColor(white: 0.8, alpha: 0.6)
cell.canMove = false
cell.isUserInteractionEnabled = false

return true
}
}
}
return false
}

override func collectionView(
_ collectionView: UICollectionView,
canMoveItemAt indexPath: IndexPath) -> Bool {
print("Checking canMoveItemAt")
if collectionView is ImageCollectionView{
return false
}else if collectionView is SoundCollectionView{ //= collectionView as? SoundCollectionView {
if let cell = collectionView.cellForItem(at: indexPath) as? SoundCollectionViewCell{
return cell.canMove
}else{
print("ERROR! SoundCollectionView cell is not a SoundCollectionViewCell object")
return true
}
}else{
print("ERROR! Views are neither Sound nor Image collection view.")
return false
}
}

//UIViewController, UICollectionViewDragDelegate, UICollectionViewDropDelegate, UICollectionViewDataSource, UICollectionViewDelegate
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.size.width, height: 50)
}

//MARK: - Collection Setup
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if type(of: collectionView) == ImageCollectionViewCell.self {
// if type(of: collectionView) == ImageCollectionViewCell.self {
return 4
}else{
return 5-1//4
}
// }else{
// return 5-1//4
// }
}
var sounds: [Sound]?
var currentIndex: Int = 0
var currentIndex: Int = -1

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

if(sounds == nil){
do{
try sounds = (self.parent as! MainGameViewController).sounds?.sorted(by: {(firstSound, secondSound) throws -> Bool in return firstSound.name ?? "" > secondSound.name ?? ""})

}catch{
print(error.localizedDescription)
}
}

if(currentIndex == -1){
currentIndex = 0
}
else if(currentIndex >= (sounds!.count - 1)){
currentIndex = 0
}else{
currentIndex = (sounds?.index(after: currentIndex)) ?? 0

}


if type(of: collectionView) == ImageCollectionView.self{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ImageCollectionViewCell.REUSE_IDENTIFIER, for: indexPath)
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ImageCollectionViewCell.REUSE_IDENTIFIER, for: indexPath) as! ImageCollectionViewCell

cell.name = sounds?[currentIndex].name

return cell
}else if type(of: collectionView) == SoundCollectionView.self{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SoundCollectionViewCell.REUSE_IDENTIFIER, for: indexPath) as! SoundCollectionViewCell
if(sounds == nil){
do{
try sounds = (self.parent as! MainGameViewController).sounds?.sorted(by: {(firstSound, secondSound) throws -> Bool in return firstSound.name ?? "" > secondSound.name ?? ""})

}catch{
print(error.localizedDescription)
}
}

cell.label.text = sounds?[currentIndex].name ?? "error nil"
if(currentIndex >= (sounds!.count - 1)){
currentIndex = 0
}else{
currentIndex = (sounds?.index(after: currentIndex)) ?? 0
cell.name = sounds?[currentIndex].name
// cell.label.text = sounds?[currentIndex].name ?? "error nil"

}

return cell
}else{
Expand Down Expand Up @@ -74,9 +126,14 @@ class SquareCollectionViewController: UICollectionViewController, UICollectionVi

// imagesView.dropDelegate = self
// soundsView.dropDelegate = self
self.collectionView.dragDelegate = self
if(collectionView is SoundCollectionView){
print("Sound Collection View found!")
self.collectionView.dragDelegate = self
self.collectionView.dragInteractionEnabled = true
}else{
print(type(of: collectionView))
}
self.collectionView.dropDelegate = self
self.collectionView.dragInteractionEnabled = true
self.collectionView.reorderingCadence = .fast
// imagesView.dragInteractionEnabled = true
//soundsView.dragInteractionEnabled = true
Expand Down Expand Up @@ -144,8 +201,19 @@ class SquareCollectionViewController: UICollectionViewController, UICollectionVi
}
else
{
// ITEM HAS BEEN DROPPED
// TODO: GREY OUT COLLECTION VIEW ITEM HERE
// item is about to be dropped
if(destinationIndexPath == nil){
return UICollectionViewDropProposal(operation: .forbidden)
}
if let cell = (collectionView.cellForItem(at: destinationIndexPath!)) as? ImageCollectionViewCell{
if(cell.isMatched){
return UICollectionViewDropProposal(operation: .cancel, intent: .insertIntoDestinationIndexPath)
}else{
print("cell is not matched")
}
}else{
print("not an image collection view cell")
}
return UICollectionViewDropProposal(operation: .copy, intent: .insertIntoDestinationIndexPath)
}
}
Expand Down Expand Up @@ -182,14 +250,42 @@ class SquareCollectionViewController: UICollectionViewController, UICollectionVi
var indexPaths = [IndexPath]()
for (index, item) in coordinator.items.enumerated()
{
print("item: \(item)")
//Destination index path for each item is calculated separately using the destinationIndexPath fetched from the coordinator
let indexPath = IndexPath(row: destinationIndexPath.row + index, section: destinationIndexPath.section)

// items.insert(item.dragItem.localObject as! String, at: indexPath.row)
indexPaths.append(indexPath)
// coordinator.drop(item, to: <#T##UICollectionViewDropPlaceholder#>)

if type(of: collectionView) == ImageCollectionView.self{
guard let cell = collectionView.cellForItem(at: indexPath) as? ImageCollectionViewCell
else{
print("ERROR")
return;
}
if let name = cell.name{
if(name == item.dragItem.localObject as! String){
//cell.name = "0\(item.dragItem.localObject as? String ?? "err")"
let uialert = UIAlertController(title: "Correct", message: "The item you selected was correct!", preferredStyle: UIAlertController.Style.alert)
uialert.addAction(UIAlertAction(title: "Okay", style: UIAlertAction.Style.default, handler: nil))
self.present(uialert, animated: true, completion: nil)

(self.parent as! MainGameViewController).stopDragAndGreyOutSoundCell(name: name)
//TODO: stopAllowDragOfDragItem()
// greyOutDragItemAndTargetCell()
cell.backgroundColor = UIColor(white: 0.8, alpha: 0.8)
cell.isMatched = true
}else{
let uialert = UIAlertController(title: "Game Over", message: "Sorry, the correct name of the image you selected is \(name)", preferredStyle: UIAlertController.Style.alert)
uialert.addAction(UIAlertAction(title: "Okay", style: UIAlertAction.Style.default, handler: nil))
self.present(uialert, animated: true, completion: nil)

//TODO: gameOver()
}
}
}
}
collectionView.insertItems(at: indexPaths)
// collectionView.insertItems(at: indexPaths)
})

break
Expand All @@ -198,11 +294,33 @@ class SquareCollectionViewController: UICollectionViewController, UICollectionVi
return
}
}

func collectionView(_ collectionView: UICollectionView, dragPreviewParametersForItemAt indexPath: IndexPath) -> UIDragPreviewParameters?
{
let previewParameters = UIDragPreviewParameters()
previewParameters.visiblePath = UIBezierPath(rect: CGRect(x: 25, y: 25, width: 120, height: 120))

let bounds = collectionView.layoutAttributesForItem(at: indexPath)?.bounds

let cgPath = CGMutablePath()
let botLeftPt = CGPoint(x: bounds!.minX, y: bounds!.maxY)
let botRightPt = CGPoint(x: bounds!.maxX, y: bounds!.maxY)
guard let topLeftPoint = bounds?.origin else {
print("Error: you should not see this")
return previewParameters
}
let topRightPoint = CGPoint(x: bounds!.maxX, y: bounds!.minY)
cgPath.move(to: botLeftPt)
cgPath.addArc(tangent1End: topLeftPoint, tangent2End: topRightPoint, radius: 20)
cgPath.addLine(to: topRightPoint)
cgPath.addArc(tangent1End: topRightPoint, tangent2End: botRightPt, radius: 20)
cgPath.move(to: topRightPoint)
cgPath.addArc(tangent1End: botRightPt, tangent2End: botLeftPt, radius: 20)
cgPath.addLine(to: botLeftPt)
cgPath.addArc(tangent1End: botLeftPt, tangent2End: topLeftPoint, radius: 20)

let bezierPath = UIBezierPath(cgPath: cgPath)

previewParameters.visiblePath = bezierPath //UIBezierPath(rect: CGRect(x: 25, y: 25, width: 120, height: 120))
return previewParameters
}

Expand Down

0 comments on commit 8f55d11

Please sign in to comment.