Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* - HeroCompatible added
- UIView+Hero translation WIP

* - Added HeroCompatible protocol and constrained HeroExtension
- Adopted UIView+Hero, UIViewController+Hero to new constrained extension
- Updated Examples and Source files with changes

* - Code refactoring: .hero.ID replaced with .here.id
  • Loading branch information
dDomovoj authored and lkzhao committed Jan 31, 2018
1 parent b41b7da commit 1e0b445
Show file tree
Hide file tree
Showing 21 changed files with 429 additions and 227 deletions.
16 changes: 8 additions & 8 deletions Examples/Examples/AppleHomePage/AppleHomePage.storyboard
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11762" systemVersion="16A313a" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="hC7-Hw-3Wq">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13771" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="hC7-Hw-3Wq">
<device id="retina5_5" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11757"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13772"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand Down Expand Up @@ -76,7 +76,7 @@ SERIES 2</string>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Introducing Series 2." textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="mXx-VP-ibg">
<rect key="frame" x="75.666666666666686" y="531" width="263" height="34"/>
<rect key="frame" x="79.666666666666671" y="531" width="255.33333333333331" height="34"/>
<fontDescription key="fontDescription" type="system" weight="medium" pointSize="28"/>
<color key="textColor" red="0.13798230889999999" green="0.121344544" blue="0.12544536589999999" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
Expand All @@ -99,7 +99,7 @@ SERIES 2</string>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
<connections>
<action selector="hero_dismissViewController" destination="RcI-CE-08V" eventType="touchUpInside" id="KTp-Gj-3H1"/>
<action selector="hero_dismissViewController" destination="RcI-CE-08V" eventType="touchUpInside" id="w3K-61-m0H"/>
</connections>
</button>
</subviews>
Expand Down Expand Up @@ -144,13 +144,13 @@ SERIES 2</string>
<rect key="frame" x="0.0" y="280" width="414" height="476"/>
</imageView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="A touch of genius." textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="NJl-s5-uPY">
<rect key="frame" x="91.666666666666686" y="181" width="231" height="34"/>
<rect key="frame" x="95.666666666666671" y="181" width="223.33333333333331" height="34"/>
<fontDescription key="fontDescription" type="system" weight="medium" pointSize="28"/>
<color key="textColor" red="0.13798230889999999" green="0.121344544" blue="0.12544536589999999" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="MacBook Pro" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="eDw-4Q-WTZ">
<rect key="frame" x="140" y="154" width="134" height="27"/>
<rect key="frame" x="142" y="154" width="130" height="27"/>
<fontDescription key="fontDescription" type="system" weight="medium" pointSize="22"/>
<color key="textColor" red="0.13798230889999999" green="0.121344544" blue="0.12544536589999999" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
Expand Down Expand Up @@ -216,13 +216,13 @@ SERIES 2</string>
</constraints>
</imageView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="iPhone" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="X8U-ip-pdE">
<rect key="frame" x="171.66666666666666" y="153" width="71" height="26.666666666666657"/>
<rect key="frame" x="172.66666666666666" y="153" width="69.333333333333343" height="26.666666666666657"/>
<fontDescription key="fontDescription" type="system" weight="semibold" pointSize="22"/>
<color key="textColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="This is 7." textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="UPq-F1-1sn">
<rect key="frame" x="145.66666666666669" y="179.66666666666666" width="123.00000000000006" height="37"/>
<rect key="frame" x="147.33333333333334" y="179.66666666666666" width="119.66666666666666" height="37"/>
<fontDescription key="fontDescription" type="system" weight="semibold" pointSize="31"/>
<color key="textColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<nil key="highlightedColor"/>
Expand Down
20 changes: 10 additions & 10 deletions Examples/Examples/AppleHomePage/AppleProductViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ class AppleProductViewController: UIViewController, HeroViewControllerDelegate {
}

func applyShrinkModifiers() {
view.heroModifiers = nil
primaryLabel.heroModifiers = [.translate(x:-50, y:(view.center.y - primaryLabel.center.y)/10), .scale(0.9), HeroModifier.duration(0.3)]
secondaryLabel.heroModifiers = [.translate(x:-50, y:(view.center.y - secondaryLabel.center.y)/10), .scale(0.9), HeroModifier.duration(0.3)]
imageView.heroModifiers = [.translate(x:-80), .scale(0.9), HeroModifier.duration(0.3)]
view.hero.modifiers = nil
primaryLabel.hero.modifiers = [.translate(x:-50, y:(view.center.y - primaryLabel.center.y)/10), .scale(0.9), HeroModifier.duration(0.3)]
secondaryLabel.hero.modifiers = [.translate(x:-50, y:(view.center.y - secondaryLabel.center.y)/10), .scale(0.9), HeroModifier.duration(0.3)]
imageView.hero.modifiers = [.translate(x:-80), .scale(0.9), HeroModifier.duration(0.3)]
}

func applySlideModifiers() {
view.heroModifiers = [.translate(x: view.bounds.width), .duration(0.3), .beginWith(modifiers: [.zPosition(2)])]
primaryLabel.heroModifiers = [.translate(x:100), .duration(0.3)]
secondaryLabel.heroModifiers = [.translate(x:100), .duration(0.3)]
imageView.heroModifiers = nil
view.hero.modifiers = [.translate(x: view.bounds.width), .duration(0.3), .beginWith(modifiers: [.zPosition(2)])]
primaryLabel.hero.modifiers = [.translate(x:100), .duration(0.3)]
secondaryLabel.hero.modifiers = [.translate(x:100), .duration(0.3)]
imageView.hero.modifiers = nil
}

enum TransitionState {
Expand Down Expand Up @@ -85,7 +85,7 @@ class AppleProductViewController: UIViewController, HeroViewControllerDelegate {
nextVC!.applyShrinkModifiers()
}
state = nextState
hero_replaceViewController(with: nextVC!)
hero.replaceViewController(with: nextVC!)
} else {
let progress = abs(translateX / view.bounds.width)
Hero.shared.update(progress)
Expand All @@ -108,7 +108,7 @@ class AppleProductViewController: UIViewController, HeroViewControllerDelegate {

func heroWillStartAnimatingTo(viewController: UIViewController) {
if !(viewController is AppleProductViewController) {
view.heroModifiers = [.ignoreSubviewModifiers(recursive: true)]
view.hero.modifiers = [.ignoreSubviewModifiers(recursive: true)]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ class AnimationSelectTableViewController: UITableViewController {

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = self.storyboard!.instantiateViewController(withIdentifier: "animationSelect")
vc.heroModalAnimationType = animations[indexPath.item]
hero_replaceViewController(with: vc)
vc.hero.modalAnimationType = animations[indexPath.item]
hero.replaceViewController(with: vc)
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
Expand Down
2 changes: 1 addition & 1 deletion Examples/Examples/CityGuide/CityGuideCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class CityCell: UICollectionViewCell {
guard let city = city else { return }
let name = city.name

heroID = "\(name)"
hero.id = "\(name)"

nameLabel.text = name
imageView.image = city.image
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ImageGalleryViewController: UIViewController {
// animation is automatic! Holy
let next = (UIStoryboard(name: "ImageGallery", bundle: nil).instantiateViewController(withIdentifier: "imageGallery") as? ImageGalleryViewController)!
next.columns = columns == 3 ? 5 : 3
hero_replaceViewController(with: next)
hero.replaceViewController(with: next)
}
}

Expand All @@ -62,8 +62,8 @@ extension ImageGalleryViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let imageCell = (collectionView.dequeueReusableCell(withReuseIdentifier: "item", for: indexPath) as? ImageCell)!
imageCell.imageView.image = ImageLibrary.thumbnail(index:indexPath.item)
imageCell.imageView.heroID = "image_\(indexPath.item)"
imageCell.imageView.heroModifiers = [.fade, .scale(0.8)]
imageCell.imageView.hero.id = "image_\(indexPath.item)"
imageCell.imageView.hero.modifiers = [.fade, .scale(0.8)]
imageCell.imageView.isOpaque = true
return imageCell
}
Expand All @@ -78,26 +78,26 @@ extension ImageGalleryViewController: UICollectionViewDelegate, UICollectionView
extension ImageGalleryViewController: HeroViewControllerDelegate {
func heroWillStartAnimatingTo(viewController: UIViewController) {
if (viewController as? ImageGalleryViewController) != nil {
collectionView.heroModifiers = [.cascade(delta:0.015, direction:.bottomToTop, delayMatchedViews:true)]
collectionView.hero.modifiers = [.cascade(delta:0.015, direction:.bottomToTop, delayMatchedViews:true)]
} else if (viewController as? ImageViewController) != nil {
let cell = collectionView.cellForItem(at: collectionView.indexPathsForSelectedItems!.first!)!
collectionView.heroModifiers = [.cascade(delta: 0.015, direction: .radial(center: cell.center), delayMatchedViews: true)]
collectionView.hero.modifiers = [.cascade(delta: 0.015, direction: .radial(center: cell.center), delayMatchedViews: true)]
} else {
collectionView.heroModifiers = [.cascade(delta:0.015)]
collectionView.hero.modifiers = [.cascade(delta:0.015)]
}
}
func heroWillStartAnimatingFrom(viewController: UIViewController) {
view.heroModifiers = nil
view.hero.modifiers = nil
if (viewController as? ImageGalleryViewController) != nil {
collectionView.heroModifiers = [.cascade(delta:0.015), .delay(0.25)]
collectionView.hero.modifiers = [.cascade(delta:0.015), .delay(0.25)]
} else {
collectionView.heroModifiers = [.cascade(delta:0.015)]
collectionView.hero.modifiers = [.cascade(delta:0.015)]
}
if let vc = viewController as? ImageViewController,
let originalCellIndex = vc.selectedIndex,
let currentCellIndex = vc.collectionView?.indexPathsForVisibleItems[0],
let targetAttribute = collectionView.layoutAttributesForItem(at: currentCellIndex) {
collectionView.heroModifiers = [.cascade(delta:0.015, direction:.inverseRadial(center:targetAttribute.center))]
collectionView.hero.modifiers = [.cascade(delta:0.015, direction:.inverseRadial(center:targetAttribute.center))]
if !collectionView.indexPathsForVisibleItems.contains(currentCellIndex) {
// make the cell visible
collectionView.scrollToItem(at: currentCellIndex,
Expand Down
6 changes: 3 additions & 3 deletions Examples/Examples/ImageGallery/ImageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class ImageViewController: UICollectionViewController {
let progress = translation.y / 2 / collectionView!.bounds.height
switch panGR.state {
case .began:
hero_dismissViewController()
hero.dismissViewController()
case .changed:
Hero.shared.update(progress)
if let cell = collectionView?.visibleCells[0] as? ScrollingImageCell {
Expand All @@ -80,8 +80,8 @@ extension ImageViewController {
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let imageCell = (collectionView.dequeueReusableCell(withReuseIdentifier: "item", for: indexPath) as? ScrollingImageCell)!
imageCell.image = ImageLibrary.image(index:indexPath.item)
imageCell.imageView.heroID = "image_\(indexPath.item)"
imageCell.imageView.heroModifiers = [.position(CGPoint(x:view.bounds.width/2, y:view.bounds.height+view.bounds.width/2)), .scale(0.6), .fade]
imageCell.imageView.hero.id = "image_\(indexPath.item)"
imageCell.imageView.hero.modifiers = [.position(CGPoint(x:view.bounds.width/2, y:view.bounds.height+view.bounds.width/2)), .scale(0.6), .fade]
imageCell.topInset = topLayoutGuide.length
imageCell.imageView.isOpaque = true
return imageCell
Expand Down
16 changes: 8 additions & 8 deletions Examples/Examples/ListToGrid/GridCollectionViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class GridCollectionViewController: UICollectionViewController, UICollectionView
@IBAction func toList(_ sender: Any) {
let next = (UIStoryboard(name: "ListToGrid", bundle: nil).instantiateViewController(withIdentifier: "list") as? ListTableViewController)!
next.tableView.contentOffset.y = collectionView!.contentOffset.y + collectionView!.contentInset.top
hero_replaceViewController(with: next)
hero.replaceViewController(with: next)
}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
Expand All @@ -49,10 +49,10 @@ class GridCollectionViewController: UICollectionViewController, UICollectionView
let cell = (collectionView.dequeueReusableCell(withReuseIdentifier: "item", for: indexPath) as? GridImageCell)!

let image = ImageLibrary.thumbnail(index:indexPath.item)
cell.heroModifiers = [.fade, .translate(y:20)]
cell.hero.modifiers = [.fade, .translate(y:20)]
cell.imageView!.image = image
cell.imageView!.heroID = "image_\(indexPath.item)"
cell.imageView!.heroModifiers = [.arc]
cell.imageView!.hero.id = "image_\(indexPath.item)"
cell.imageView!.hero.modifiers = [.arc]
cell.imageView!.isOpaque = true
cell.textLabel!.text = "Item \(indexPath.item)"
cell.detailTextLabel!.text = "Description \(indexPath.item)"
Expand All @@ -76,25 +76,25 @@ extension GridCollectionViewController: HeroViewControllerDelegate {
let index = collectionView!.indexPathsForSelectedItems?[0],
let cell = collectionView!.cellForItem(at: index) as? GridImageCell {
let cellPos = view.convert(cell.imageView.center, from: cell)
collectionView!.heroModifiers = [.scale(3), .translate(x:view.center.x - cellPos.x, y:view.center.y + collectionView!.contentInset.top/2/3 - cellPos.y), .ignoreSubviewModifiers, .fade]
collectionView!.hero.modifiers = [.scale(3), .translate(x:view.center.x - cellPos.x, y:view.center.y + collectionView!.contentInset.top/2/3 - cellPos.y), .ignoreSubviewModifiers, .fade]
} else {
collectionView!.heroModifiers = [.cascade]
collectionView!.hero.modifiers = [.cascade]
}
}

func heroWillStartAnimatingFrom(viewController: UIViewController) {
if let vc = viewController as? ImageViewController,
let originalCellIndex = vc.selectedIndex,
let currentCellIndex = vc.collectionView?.indexPathsForVisibleItems[0] {
collectionView!.heroModifiers = [.cascade]
collectionView!.hero.modifiers = [.cascade]
if !collectionView!.indexPathsForVisibleItems.contains(currentCellIndex) {
// make the cell visible
collectionView!.scrollToItem(at: currentCellIndex,
at: originalCellIndex < currentCellIndex ? .bottom : .top,
animated: false)
}
} else {
collectionView!.heroModifiers = [.cascade, .delay(0.2)]
collectionView!.hero.modifiers = [.cascade, .delay(0.2)]
}
}
}
16 changes: 8 additions & 8 deletions Examples/Examples/ListToGrid/ListTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ class ListTableViewController: UITableViewController {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "item", for: indexPath)

cell.heroModifiers = [.fade, .translate(x:-100)]
cell.imageView!.heroID = "image_\(indexPath.item)"
cell.imageView!.heroModifiers = [.arc]
cell.hero.modifiers = [.fade, .translate(x:-100)]
cell.imageView!.hero.id = "image_\(indexPath.item)"
cell.imageView!.hero.modifiers = [.arc]
cell.imageView!.image = ImageLibrary.thumbnail(index:indexPath.item)
cell.imageView!.isOpaque = true
cell.textLabel!.text = "Item \(indexPath.item)"
Expand All @@ -60,7 +60,7 @@ class ListTableViewController: UITableViewController {
@IBAction func toGrid(_ sender: Any) {
let next = (UIStoryboard(name: "ListToGrid", bundle: nil).instantiateViewController(withIdentifier: "grid") as? GridCollectionViewController)!
next.collectionView?.contentOffset.y = tableView.contentOffset.y + tableView.contentInset.top
hero_replaceViewController(with: next)
hero.replaceViewController(with: next)
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
Expand All @@ -75,17 +75,17 @@ class ListTableViewController: UITableViewController {
extension ListTableViewController: HeroViewControllerDelegate {
func heroWillStartAnimatingTo(viewController: UIViewController) {
if let _ = viewController as? GridCollectionViewController {
tableView.heroModifiers = [.ignoreSubviewModifiers]
tableView.hero.modifiers = [.ignoreSubviewModifiers]
} else if viewController is ImageViewController {
} else {
tableView.heroModifiers = [.cascade]
tableView.hero.modifiers = [.cascade]
}
}
func heroWillStartAnimatingFrom(viewController: UIViewController) {
if let _ = viewController as? GridCollectionViewController {
tableView.heroModifiers = [.ignoreSubviewModifiers]
tableView.hero.modifiers = [.ignoreSubviewModifiers]
} else {
tableView.heroModifiers = [.cascade]
tableView.hero.modifiers = [.cascade]
}
if let vc = viewController as? ImageViewController,
let originalCellIndex = vc.selectedIndex,
Expand Down
Loading

0 comments on commit 1e0b445

Please sign in to comment.