Skip to content

Commit

Permalink
working on scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
lukehasawii committed Apr 28, 2023
1 parent 225dcb0 commit 923402c
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 11 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,7 +40,7 @@
<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="-7" y="169" width="392" height="641"/>
<rect key="frame" x="-7" y="167" width="392" height="642"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<prototypes>
Expand Down Expand Up @@ -283,7 +283,7 @@
<nil key="highlightedColor"/>
</label>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="shuffle" catalog="system" translatesAutoresizingMaskIntoConstraints="NO" id="NMK-3v-9w8">
<rect key="frame" x="16" y="10" width="44" height="38"/>
<rect key="frame" x="16" y="9.6666666666666643" width="44" height="38"/>
<color key="tintColor" systemColor="systemBackgroundColor"/>
</imageView>
</subviews>
Expand Down Expand Up @@ -332,7 +332,19 @@
<constraint firstItem="CeK-06-gX6" firstAttribute="top" secondItem="tVM-2t-eum" secondAttribute="bottom" constant="30" id="zNx-gS-0Lr"/>
</constraints>
</view>
<navigationItem key="navigationItem" title="Title of Navigation" id="jdW-xP-Ytc"/>
<navigationItem key="navigationItem" title="Title of Navigation" id="jdW-xP-Ytc">
<barButtonItem key="rightBarButtonItem" style="plain" id="62R-NF-EZH">
<button key="customView" opaque="NO" contentMode="scaleToFill" misplaced="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" id="PFY-53-eK4">
<rect key="frame" x="290" y="5" width="87" height="34.333333333333336"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="Button"/>
<buttonConfiguration key="configuration" style="gray" title="Animate"/>
<connections>
<action selector="doAnimateNewItems:" destination="tXV-QY-UTS" eventType="touchUpInside" id="c6z-5P-EcH"/>
</connections>
</button>
</barButtonItem>
</navigationItem>
<connections>
<outlet property="containerView1" destination="89A-kF-OLT" id="6n4-W9-n6e"/>
<outlet property="containerView2" destination="tVM-2t-eum" id="ZJw-jI-V1L"/>
Expand Down
2 changes: 1 addition & 1 deletion SoundboardQuiz/GameOverViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class GameOverViewController: UIViewController {
//TODO: below
newViewController.setCategoryInfo(catName: categoryName!, sounds: sounds!)
newViewController.populateSounds()

//newViewController.shuffle(itemCount: 4)


guard var viewControllers = navigationController?.viewControllers else{
Expand Down
1 change: 1 addition & 0 deletions SoundboardQuiz/LevelSelectTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ class LevelSelectTableViewController: UITableViewController {
print("populating category info and sounds")
destVC.setCategoryInfo(catName: soundCategory.name!, sounds: soundCategory.sounds!)
destVC.populateSounds()
// destVC.shuffle(itemCount: 4)

}
//MARK: - Setting Category Info For The Main Game View Controller
Expand Down
41 changes: 37 additions & 4 deletions SoundboardQuiz/MainGameViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ class MainGameViewController: UIViewController {
self.sounds = sounds as? Set<Sound>
}

func shuffle(){
func shuffle(itemCount: Int){


if let sub = sounds?.shuffled() {
let slice = sub[0..<min(4, sub.endIndex)]
subSounds = Array(slice)
var slice = Array(sub[0..<min(itemCount, sub.endIndex)])
while (slice.count < itemCount) {
slice.append(sub.randomElement()!)
}
print("size of slice: \(slice.count)")
subSounds = slice
}else{
print("error")
return
Expand All @@ -43,6 +47,28 @@ class MainGameViewController: UIViewController {
embeddedViewController1.shuffle()
embeddedViewController2.shuffle()


}
@IBAction func doAnimateNewItems(_ sender: Any) {

shuffle(itemCount: 8)
embeddedViewController1.reload()
embeddedViewController2.reload()

if var subSounds {
subSounds = Array(subSounds[4..<min(8, subSounds.endIndex)])
//TODO: FIX FILLER FUNCTION

self.subSounds = subSounds
print("subsounds shrunk")
}else{
print("ERROR: subsounds not shrunk")
}
// do animate here
embeddedViewController1.disableUserInteractionAndAnimate()
embeddedViewController2.disableUserInteractionAndAnimate()


}
func populateSounds() {
if let sub = sounds?.shuffled(){
Expand Down Expand Up @@ -92,7 +118,14 @@ class MainGameViewController: UIViewController {
}

@IBAction func shuffleTapped(_ sender: Any) {
shuffle()
shuffle(itemCount: 4)

embeddedViewController1.shuffle()
embeddedViewController2.shuffle()

embeddedViewController1.reload()
embeddedViewController2.reload()

}

/*
Expand Down
50 changes: 47 additions & 3 deletions SoundboardQuiz/SquareCollectionViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class SquareCollectionViewController: UICollectionViewController, UICollectionVi
}
}
// if type(of: collectionView) == ImageCollectionViewCell.self {
return 4
return sounds?.count ?? 4
// }else{
// return 5-1//4
// }
Expand All @@ -90,9 +90,53 @@ class SquareCollectionViewController: UICollectionViewController, UICollectionVi
self.sounds = self.sounds?.shuffled()
// print("SHUFFLED!")
// }
}
func reload(){
self.collectionView.reloadData()
}

func disableUserInteractionAndAnimate(){
collectionView.isUserInteractionEnabled = false;
reload()
//animate
// let lastItemIndexPath: IndexPath? = (collectionView.indexPathForItem(at: (collectionView.numberOfItems(inSection: 0)-4) ?? CGPoint())?)
let lastItemIndexPath = IndexPath(item: 4, section: 0)

print("collectionview size: \(self.collectionView.numberOfItems(inSection: 0))")

// self.collectionView.performBatchUpdates({ () -> Void in
// let indexSet = IndexSet(0...(collectionView.numberOfSections - 1))
// self.collectionView.insertSections(indexSet)
// self.collectionView.forFirstBaselineLayout.layer.speed = 0.5
// }, completion: { (finished) -> Void in
// self.collectionView.forFirstBaselineLayout.layer.speed = 1.0
// self.shuffle()
// self.reload()
// self.collectionView.isUserInteractionEnabled = true
// })

UIView.animate(withDuration: 5, animations: {
//[weak self]

if self.collectionView.numberOfItems(inSection: 0) >= 8 { //if the cell exists
print("scrolling now!")
self.collectionView.alwaysBounceVertical = false
self.collectionView.scrollToItem(at: lastItemIndexPath, at: .top, animated: false)
self.collectionView.layoutIfNeeded()
}else{
print("err: not enough items to scroll")
}
}, completion: {_ in
guard let sounds = ((self.parent as? MainGameViewController)?.subSounds) else {
return
}
self.sounds = sounds
self.reload()
self.collectionView.isUserInteractionEnabled = true
})



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


Expand All @@ -113,7 +157,7 @@ class SquareCollectionViewController: UICollectionViewController, UICollectionVi

// cell.name = sounds?[currentIndex].name
if let sounds, indexPath.row > sounds.count-1{
cell.name = "Out of Data"
cell.name = "SHOULD NEVER SEE!"
}else{
if let name = sounds?[indexPath.row].name{
cell.name = name
Expand Down

0 comments on commit 923402c

Please sign in to comment.