Refactors GridCollectionViewCell to handle vibrancy on a per-cell-subview basis

Allows certain subviews to be vibrant while others aren’t
This commit is contained in:
Riley Testut 2016-08-08 19:06:08 -05:00
parent 89c584e4c4
commit d38b6bb981
6 changed files with 92 additions and 54 deletions

View File

@ -13,12 +13,40 @@ class GridCollectionViewCell: UICollectionViewCell
let imageView = UIImageView()
let textLabel = UILabel()
var isImageViewVibrancyEnabled = true {
didSet {
if self.isImageViewVibrancyEnabled
{
self.vibrancyView.contentView.addSubview(self.imageView)
}
else
{
self.contentView.addSubview(self.imageView)
}
}
}
var isTextLabelVibrancyEnabled = true {
didSet {
if self.isTextLabelVibrancyEnabled
{
self.vibrancyView.contentView.addSubview(self.textLabel)
}
else
{
self.contentView.addSubview(self.textLabel)
}
}
}
var maximumImageSize: CGSize = CGSize(width: 100, height: 100) {
didSet {
self.updateMaximumImageSize()
}
}
private var vibrancyView = UIVisualEffectView(effect: UIVibrancyEffect(blurEffect: UIBlurEffect(style: .dark)))
private var imageViewWidthConstraint: NSLayoutConstraint!
private var imageViewHeightConstraint: NSLayoutConstraint!
@ -49,20 +77,37 @@ class GridCollectionViewCell: UICollectionViewCell
self.clipsToBounds = false
self.contentView.clipsToBounds = false
self.imageView.translatesAutoresizingMaskIntoConstraints = false
self.vibrancyView.translatesAutoresizingMaskIntoConstraints = false
self.vibrancyView.contentView.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(self.vibrancyView)
self.imageView.contentMode = .scaleAspectFit
#if os(tvOS)
self.imageView.adjustsImageWhenAncestorFocused = true
#endif
self.contentView.addSubview(self.imageView)
self.textLabel.translatesAutoresizingMaskIntoConstraints = false
self.textLabel.font = UIFont.boldSystemFont(ofSize: 12)
self.textLabel.textAlignment = .center
self.textLabel.numberOfLines = 0
self.contentView.addSubview(self.textLabel)
// Auto Layout
/* Auto Layout */
// Vibrancy View
// Need to add explicit constraints for vibrancyView + vibrancyView.contentView or else Auto Layout won't calculate correct size 🙄
self.vibrancyView.topAnchor.constraint(equalTo: self.contentView.topAnchor).isActive = true
self.vibrancyView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor).isActive = true
self.vibrancyView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor).isActive = true
self.vibrancyView.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor).isActive = true
self.vibrancyView.contentView.topAnchor.constraint(equalTo: self.vibrancyView.topAnchor).isActive = true
self.vibrancyView.contentView.bottomAnchor.constraint(equalTo: self.vibrancyView.bottomAnchor).isActive = true
self.vibrancyView.contentView.leadingAnchor.constraint(equalTo: self.vibrancyView.leadingAnchor).isActive = true
self.vibrancyView.contentView.trailingAnchor.constraint(equalTo: self.vibrancyView.trailingAnchor).isActive = true
// Image View
self.imageView.translatesAutoresizingMaskIntoConstraints = false
self.imageView.topAnchor.constraint(equalTo: self.contentView.topAnchor).isActive = true
self.imageView.centerXAnchor.constraint(equalTo: self.contentView.centerXAnchor).isActive = true
@ -74,6 +119,9 @@ class GridCollectionViewCell: UICollectionViewCell
self.imageViewHeightConstraint.isActive = true
// Text Label
self.textLabel.translatesAutoresizingMaskIntoConstraints = false
self.textLabel.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor).isActive = true
self.textLabel.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor).isActive = true
@ -96,7 +144,7 @@ class GridCollectionViewCell: UICollectionViewCell
self.updateMaximumImageSize()
}
override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator)
{
super.didUpdateFocus(in: context, with: coordinator)
@ -125,7 +173,7 @@ class GridCollectionViewCell: UICollectionViewCell
self.layoutIfNeeded()
}, completion: nil)
}, completion: nil)
}
}

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11163.2" systemVersion="15F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="Dt0-nV-isV">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11185.3" systemVersion="15G31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="Dt0-nV-isV">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11133"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11151.4"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
@ -24,34 +24,17 @@
<frame key="frameInset"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<visualEffectView opaque="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Rcd-Nl-fKP" userLabel="Vibrancy View">
<view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" id="3hz-aG-NZy" userLabel="Vibrancy Content View">
<frame key="frameInset"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<containerView opaque="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="rqN-NB-jbb">
<connections>
<segue destination="sWv-Ky-VGs" kind="embed" identifier="embedNavigationController" id="1Ja-XW-uoT"/>
</connections>
</containerView>
</subviews>
<constraints>
<constraint firstItem="rqN-NB-jbb" firstAttribute="leading" secondItem="3hz-aG-NZy" secondAttribute="leading" id="OOk-do-LIf"/>
<constraint firstAttribute="trailing" secondItem="rqN-NB-jbb" secondAttribute="trailing" id="hcS-M2-FYQ"/>
<constraint firstAttribute="bottom" secondItem="rqN-NB-jbb" secondAttribute="bottom" id="iMd-TA-TXf"/>
<constraint firstItem="rqN-NB-jbb" firstAttribute="top" secondItem="3hz-aG-NZy" secondAttribute="top" id="yOz-Ra-ndK"/>
</constraints>
</view>
<vibrancyEffect>
<blurEffect style="dark"/>
</vibrancyEffect>
</visualEffectView>
<containerView opaque="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="rqN-NB-jbb">
<connections>
<segue destination="sWv-Ky-VGs" kind="embed" identifier="embedNavigationController" id="1Ja-XW-uoT"/>
</connections>
</containerView>
</subviews>
<constraints>
<constraint firstAttribute="bottom" secondItem="Rcd-Nl-fKP" secondAttribute="bottom" id="054-eA-dPp"/>
<constraint firstAttribute="trailing" secondItem="Rcd-Nl-fKP" secondAttribute="trailing" id="RCo-1V-Ryf"/>
<constraint firstItem="Rcd-Nl-fKP" firstAttribute="leading" secondItem="eyD-0d-RHe" secondAttribute="leading" id="WHs-Ir-0EG"/>
<constraint firstItem="Rcd-Nl-fKP" firstAttribute="top" secondItem="eyD-0d-RHe" secondAttribute="top" id="zXg-uo-6GK"/>
<constraint firstAttribute="bottom" secondItem="rqN-NB-jbb" secondAttribute="bottom" id="3XJ-2M-uVD"/>
<constraint firstAttribute="trailing" secondItem="rqN-NB-jbb" secondAttribute="trailing" id="NQ7-cS-8T5"/>
<constraint firstItem="rqN-NB-jbb" firstAttribute="top" secondItem="eyD-0d-RHe" secondAttribute="top" id="PLj-9S-0y0"/>
<constraint firstItem="rqN-NB-jbb" firstAttribute="leading" secondItem="eyD-0d-RHe" secondAttribute="leading" id="U92-Lx-LsK"/>
</constraints>
</view>
<blurEffect style="dark"/>
@ -199,7 +182,7 @@
<frame key="frameInset" minY="92" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="e8g-ZW-5lQ" id="AHE-Jk-ULE">
<frame key="frameInset" width="375" height="43.5"/>
<frame key="frameInset" width="375" height="43"/>
<autoresizingMask key="autoresizingMask"/>
<color key="backgroundColor" red="0.0" green="0.0" blue="0.0" alpha="0.0" colorSpace="custom" customColorSpace="sRGB"/>
</tableViewCellContentView>
@ -236,10 +219,10 @@
<tableViewSection headerTitle="Name" id="QT6-DZ-g70">
<cells>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="none" indentationWidth="10" id="ZeC-rg-QFa">
<rect key="frame" x="0.0" y="119.5" width="375" height="44"/>
<rect key="frame" x="0.0" y="120" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" tableViewCell="ZeC-rg-QFa" id="UIF-fK-ApW">
<frame key="frameInset" width="375" height="43.5"/>
<frame key="frameInset" width="375" height="43"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="Cheat Name" minimumFontSize="17" clearButtonMode="whileEditing" translatesAutoresizingMaskIntoConstraints="NO" id="DD1-X0-hg7">
@ -264,10 +247,10 @@
<tableViewSection headerTitle="Type" footerTitle="Description" id="rvn-VK-2uH">
<cells>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="none" indentationWidth="10" id="Tst-zn-e04">
<rect key="frame" x="0.0" y="227" width="375" height="44"/>
<rect key="frame" x="0.0" y="228" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" tableViewCell="Tst-zn-e04" id="gwV-zS-RWQ">
<frame key="frameInset" width="375" height="43.5"/>
<frame key="frameInset" width="375" height="43"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<segmentedControl opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="top" segmentControlStyle="plain" selectedSegmentIndex="0" translatesAutoresizingMaskIntoConstraints="NO" id="xrD-ue-96Q">
@ -292,10 +275,10 @@
<tableViewSection headerTitle="Code" footerTitle="Description" id="rHC-nA-ga0">
<cells>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="none" indentationWidth="10" rowHeight="210" id="xxc-cz-sb7">
<rect key="frame" x="0.0" y="346.5" width="375" height="210"/>
<rect key="frame" x="0.0" y="348" width="375" height="210"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" tableViewCell="xxc-cz-sb7" id="agU-SE-fNa">
<frame key="frameInset" width="375" height="209.5"/>
<frame key="frameInset" width="375" height="209"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" text="XXXXXXXX YYYYYYYY" translatesAutoresizingMaskIntoConstraints="NO" id="a17-LB-QXD" customClass="CheatTextView" customModule="Delta" customModuleProvider="target">

View File

@ -88,6 +88,9 @@ private extension PauseMenuViewController
cell.imageView.tintColor = UIColor.white
cell.imageView.backgroundColor = UIColor.clear
}
cell.isImageViewVibrancyEnabled = true
cell.isTextLabelVibrancyEnabled = true
}
func toggleSelectedStateForPauseItemAtIndexPath(_ indexPath: IndexPath)

View File

@ -68,14 +68,6 @@ class PauseViewController: UIViewController, PauseInfoProviding
extension PauseViewController
{
override func viewDidLayoutSubviews()
{
super.viewDidLayoutSubviews()
// Ensure navigation bar is always positioned correctly despite being outside the navigation controller's view
self.pauseNavigationController.navigationBar.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.pauseNavigationController.navigationBar.bounds.height)
}
override func targetViewController(forAction action: Selector, sender: AnyObject?) -> UIViewController?
{
return self.pauseNavigationController
@ -96,9 +88,6 @@ extension PauseViewController
let pauseMenuViewController = self.pauseNavigationController.topViewController as! PauseMenuViewController
pauseMenuViewController.items = self.pauseItems
// Keep navigation bar outside the UIVisualEffectView's
self.view.addSubview(self.pauseNavigationController.navigationBar)
case "saveStates":
let saveStatesViewController = segue.destination as! SaveStatesViewController
saveStatesViewController.delegate = self.saveStatesViewControllerDelegate

View File

@ -28,6 +28,11 @@ class SaveStatesCollectionHeaderView: UICollectionReusableView
private func initialize()
{
let vibrancyView = UIVisualEffectView(effect: UIVibrancyEffect(blurEffect: UIBlurEffect(style: .dark)))
vibrancyView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
vibrancyView.frame = CGRect(x: 0, y: 0, width: self.bounds.width, height: self.bounds.height)
self.addSubview(vibrancyView)
self.textLabel.translatesAutoresizingMaskIntoConstraints = false
self.textLabel.textColor = UIColor.white
@ -36,7 +41,7 @@ class SaveStatesCollectionHeaderView: UICollectionReusableView
self.textLabel.font = UIFont(descriptor: fontDescriptor, size: 0.0)
self.textLabel.textAlignment = .center
self.addSubview(self.textLabel)
vibrancyView.contentView.addSubview(self.textLabel)
// Auto Layout
NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "H:|-20-[textLabel]-20-|", options: [], metrics: nil, views: ["textLabel": self.textLabel]))

View File

@ -84,13 +84,18 @@ extension SaveStatesViewController
{
super.viewDidLoad()
self.backgroundView = RSTBackgroundView(frame: self.view.bounds)
let vibrancyView = UIVisualEffectView(effect: UIVibrancyEffect(blurEffect: UIBlurEffect(style: .dark)))
vibrancyView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
vibrancyView.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
self.view.insertSubview(vibrancyView, at: 0)
self.backgroundView = RSTBackgroundView(frame: CGRect(x: 0, y: 0, width: vibrancyView.bounds.width, height: vibrancyView.bounds.height))
self.backgroundView.isHidden = true
self.backgroundView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.backgroundView.textLabel.text = NSLocalizedString("No Save States", comment: "")
self.backgroundView.textLabel.textColor = UIColor.white
self.backgroundView.detailTextLabel.textColor = UIColor.white
self.view.insertSubview(self.backgroundView, at: 0)
vibrancyView.contentView.addSubview(self.backgroundView)
let collectionViewLayout = self.collectionViewLayout as! GridCollectionViewLayout
let averageHorizontalInset = (collectionViewLayout.sectionInset.left + collectionViewLayout.sectionInset.right) / 2
@ -188,6 +193,9 @@ private extension SaveStatesViewController
cell.imageView.backgroundColor = UIColor.white
cell.imageView.image = UIImage(named: "DeltaPlaceholder")
cell.isTextLabelVibrancyEnabled = true
cell.isImageViewVibrancyEnabled = true
if !ignoreOperations
{
let imageOperation = LoadImageOperation(URL: saveState.imageFileURL)
@ -198,6 +206,8 @@ private extension SaveStatesViewController
{
cell.imageView.backgroundColor = nil
cell.imageView.image = image
cell.isImageViewVibrancyEnabled = false
}
}