Skip to content

Commit

Permalink
fix a few bugs with the new snapshot system, add videoPlayer example
Browse files Browse the repository at this point in the history
  • Loading branch information
lkzhao committed Feb 5, 2017
1 parent d2fa3b4 commit 3c09d57
Show file tree
Hide file tree
Showing 28 changed files with 371 additions and 429 deletions.
189 changes: 60 additions & 129 deletions Examples/HeroExamples.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "poster.jpg",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
231 changes: 0 additions & 231 deletions Examples/HeroExamples/Base.lproj/AppleHomePage.storyboard

This file was deleted.

80 changes: 62 additions & 18 deletions Examples/HeroExamples/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
<userDefinedRuntimeAttribute type="string" keyPath="heroID" value="back"/>
</userDefinedRuntimeAttributes>
<connections>
<action selector="hero_unwindToRootViewController:" destination="zcb-C5-i3s" eventType="touchUpInside" id="hOy-ho-D9j"/>
<action selector="hero_unwindToRootViewController" destination="zcb-C5-i3s" eventType="touchUpInside" id="xDF-5K-otU"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="yH9-LF-tZp">
Expand Down Expand Up @@ -291,7 +291,7 @@
<userDefinedRuntimeAttribute type="string" keyPath="heroID" value="back"/>
</userDefinedRuntimeAttributes>
<connections>
<action selector="hero_unwindToRootViewController:" destination="wDI-eb-elr" eventType="touchUpInside" id="Z9U-bp-yNX"/>
<action selector="hero_unwindToRootViewController" destination="wDI-eb-elr" eventType="touchUpInside" id="5wS-Y1-0nx"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Q39-Fs-YPd">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class ImageViewController: UICollectionViewController {
Hero.shared.apply(modifiers: [.position(currentPos)], to: cell.imageView)
}
default:
if progress + panGR.velocity(in: nil).y / collectionView!.bounds.height > 0.15 {
if progress + panGR.velocity(in: nil).y / collectionView!.bounds.height > 0.3 {
Hero.shared.end()
} else {
Hero.shared.cancel()
Expand All @@ -81,7 +81,7 @@ extension ImageViewController {
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(x:0.6, y:0.6), .fade, .zPositionIfMatched(100)]
imageCell.imageView.heroModifiers = [.position(CGPoint(x:view.bounds.width/2, y:view.bounds.height+view.bounds.width/2)), .scale(0.6), .fade, .zPositionIfMatched(100)]
imageCell.topInset = topLayoutGuide.length
return imageCell
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
<userDefinedRuntimeAttribute type="string" keyPath="heroID" value="back"/>
</userDefinedRuntimeAttributes>
<connections>
<action selector="hero_unwindToRootViewController:" destination="Mgk-EP-GQc" eventType="touchUpInside" id="fHe-iQ-NX2"/>
<action selector="hero_unwindToRootViewController" destination="Mgk-EP-GQc" eventType="touchUpInside" id="5VM-Fk-Xjc"/>
</connections>
</button>
</subviews>
Expand Down Expand Up @@ -294,7 +294,7 @@
<userDefinedRuntimeAttribute type="string" keyPath="heroID" value="back"/>
</userDefinedRuntimeAttributes>
<connections>
<action selector="hero_unwindToRootViewController:" destination="cry-03-6sb" eventType="touchUpInside" id="mOk-Io-4SU"/>
<action selector="hero_unwindToRootViewController" destination="cry-03-6sb" eventType="touchUpInside" id="k7w-R6-k4b"/>
</connections>
</button>
</subviews>
Expand Down
62 changes: 62 additions & 0 deletions Examples/HeroExamples/Examples/VideoPlayer/VideoPlayer.storyboard
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11762" systemVersion="16C68" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="Jlr-UW-RBl">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11757"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--Video Player View Controller-->
<scene sceneID="1nH-rV-HNC">
<objects>
<viewController id="Jlr-UW-RBl" customClass="VideoPlayerViewController" customModule="HeroExamples" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="hf8-Fp-Md5"/>
<viewControllerLayoutGuide type="bottom" id="GrU-da-mkb"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="sJC-Pq-CA9">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<button opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="z6Y-4L-zqp">
<rect key="frame" x="8" y="28" width="48" height="48"/>
<constraints>
<constraint firstAttribute="height" constant="48" id="9CC-aa-S8X"/>
<constraint firstAttribute="width" constant="48" id="Ny5-Ce-hwc"/>
</constraints>
<color key="tintColor" white="0.66666666666666663" alpha="1" colorSpace="calibratedWhite"/>
<state key="normal" image="ic_keyboard_arrow_down"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="cornerRadius">
<real key="value" value="24"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="string" keyPath="heroModifierString" value="fade translate(0, -150)"/>
<userDefinedRuntimeAttribute type="string" keyPath="heroID" value="back"/>
</userDefinedRuntimeAttributes>
<connections>
<action selector="hero_dismissViewController" destination="wAT-Zx-dlV" eventType="touchUpInside" id="eY3-Hc-QXA"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" white="0.0" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="z6Y-4L-zqp" firstAttribute="top" secondItem="hf8-Fp-Md5" secondAttribute="bottom" constant="8" id="3iS-CQ-7V0"/>
<constraint firstItem="z6Y-4L-zqp" firstAttribute="leading" secondItem="sJC-Pq-CA9" secondAttribute="leading" constant="8" id="x8a-3B-voa"/>
</constraints>
</view>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="boolean" keyPath="isHeroEnabled" value="YES"/>
</userDefinedRuntimeAttributes>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="wAT-Zx-dlV" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="20" y="-145.27736131934034"/>
</scene>
</scenes>
<resources>
<image name="ic_keyboard_arrow_down" width="24" height="24"/>
</resources>
</document>
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// The MIT License (MIT)
//
// Copyright (c) 2016 Luke Zhao <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import UIKit
import Hero
import AVKit
import AVFoundation

class AVPlayerView: UIView {
override class var layerClass: Swift.AnyClass {
return AVPlayerLayer.self
}
}

class VideoPlayerViewController: UIViewController {
var playerView: UIView!
var panGR: UIPanGestureRecognizer!

override func viewDidLoad() {
super.viewDidLoad()
let player = AVPlayer(url: URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")!)

playerView = AVPlayerView(frame: view.bounds)
(playerView.layer as! AVPlayerLayer).player = player
playerView.heroID = "videoPlayer"
playerView.heroModifiers = [.useNoSnapshot, .useScaleBasedSizeChange]
view.insertSubview(playerView, at: 0)

panGR = UIPanGestureRecognizer(target: self, action: #selector(pan))
view.addGestureRecognizer(panGR)

// skip the initial splash screen of the video
player.seek(to: CMTimeMakeWithSeconds(0.35, 1000), toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero)
player.play()
}

func pan() {
let translation = panGR.translation(in: nil)
let progress = translation.y / 2 / view.bounds.height
switch panGR.state {
case .began:
hero_dismissViewController()
case .changed:
Hero.shared.update(progress: Double(progress))
let currentPos = CGPoint(x: translation.x + view.center.x, y: translation.y + view.center.y)
Hero.shared.apply(modifiers: [.position(currentPos)], to: playerView)
default:
if progress + panGR.velocity(in: nil).y / view.bounds.height > 0.3 {
Hero.shared.end()
} else {
Hero.shared.cancel()
}
}
}

override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
playerView.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.width / 16 * 9)
playerView.center = view.center
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// must re-insert the playerView since we used .useNoSnapshot modifier on it.
// Hero will take it out of the view hierarchy during the transition.
playerView.frame = view.bounds
view.insertSubview(playerView, at: 0)
}
}
24 changes: 2 additions & 22 deletions Examples/HeroExamples/InitialViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class InitialViewController: UITableViewController {

var storyboards: [[String]] = [
[],
["Basic", "MusicPlayer", "Menu"],
["CityGuide", "ImageViewer", "AppleHomePage", "ListToGrid", "ImageGallery"],
["Basic", "MusicPlayer", "Menu", "VideoPlayer"],
["CityGuide", "ImageViewer", "VideoPlayer", "AppleHomePage", "ListToGrid", "ImageGallery"],
["LiveInjection", "Debug", "LabelMorph"]
]

Expand All @@ -24,24 +24,4 @@ class InitialViewController: UITableViewController {
present(vc, animated: true, completion: nil)
}
}

override func viewWillAppear(_ animated: Bool) {
print("viewWillAppear")
super.viewWillAppear(animated)
}

override func viewDidAppear(_ animated: Bool) {
print("viewDidAppear")
super.viewDidAppear(animated)
}

override func viewWillDisappear(_ animated: Bool) {
print("viewWillDisappear")
super.viewWillDisappear(animated)
}

override func viewDidDisappear(_ animated: Bool) {
print("viewDidDisappear")
super.viewDidDisappear(animated)
}
}
37 changes: 24 additions & 13 deletions Sources/Hero.swift
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ internal extension Hero {
let tContext = transitionContext
let completion = completionCallback
let fvc = fromViewController
let tvc = fromViewController
let tvc = toViewController

progressUpdateObservers = nil
transitionContainer = nil
Expand All @@ -433,22 +433,33 @@ internal extension Hero {
forceNotInteractive = false
progress = 0
totalDuration = 0

if let fvc = fvc, let tvc = tvc {
closureProcessForHeroDelegate(vc: fvc) {
$0.heroDidEndAnimatingTo?(viewController: tvc)
$0.heroDidEndTransition?()
}

closureProcessForHeroDelegate(vc: tvc) {
$0.heroDidEndAnimatingFrom?(viewController: fvc)
$0.heroDidEndTransition?()
}
}


if finished {
if let fvc = fvc, let tvc = tvc {
closureProcessForHeroDelegate(vc: fvc) {
$0.heroDidEndAnimatingTo?(viewController: tvc)
$0.heroDidEndTransition?()
}

closureProcessForHeroDelegate(vc: tvc) {
$0.heroDidEndAnimatingFrom?(viewController: fvc)
$0.heroDidEndTransition?()
}
}
tContext?.finishInteractiveTransition()
} else {
if let fvc = fvc, let tvc = tvc {
closureProcessForHeroDelegate(vc: fvc) {
$0.heroDidCancelAnimatingTo?(viewController: tvc)
$0.heroDidCancelTransition?()
}

closureProcessForHeroDelegate(vc: tvc) {
$0.heroDidCancelAnimatingFrom?(viewController: fvc)
$0.heroDidCancelTransition?()
}
}
tContext?.cancelInteractiveTransition()
}
tContext?.completeTransition(finished)
Expand Down
9 changes: 3 additions & 6 deletions Sources/HeroContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,9 @@ extension HeroContext {
view.layer.cornerRadius = oldCornerRadius
view.alpha = oldAlpha

if !(view is UINavigationBar) {
if !(view is UINavigationBar), let contentView = snapshot.subviews.get(0) {
// the Snapshot's contentView must have hold the cornerRadius value,
// since the snapshot might not have maskToBounds set
let contentView = snapshot.subviews[0]
contentView.layer.cornerRadius = view.layer.cornerRadius
contentView.layer.masksToBounds = true
}
Expand All @@ -184,9 +183,7 @@ extension HeroContext {
snapshot.frame = containerView.convert(view.bounds, from: view)
snapshot.heroID = view.heroID

if snapshotType != .noSnapshot {
hide(view: view)
}
hide(view: view)

containerView.addSubview(snapshot)
snapshotViews[view] = snapshot
Expand All @@ -206,7 +203,7 @@ extension HeroContext {
// internal
extension HeroContext {
public func hide(view: UIView) {
if viewAlphas[view] == nil {
if viewAlphas[view] == nil, self[view]?.snapshotType != .noSnapshot {
viewAlphas[view] = view.alpha
view.alpha = 0
}
Expand Down
3 changes: 3 additions & 0 deletions Sources/HeroDefaultAnimator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ public class HeroDefaultAnimator: HeroAnimator {
}

public func clean() {
for vc in viewContexts.values {
vc.clean()
}
viewContexts.removeAll()
}
}
Loading

0 comments on commit 3c09d57

Please sign in to comment.