Compare commits

..

No commits in common. "f79d5a6b593533ac824dc6cb55bc198c496406df" and "df65b785ef804f76c103a74d22e2afea30ff2f8c" have entirely different histories.

5 changed files with 50 additions and 272 deletions

View File

@ -7,7 +7,6 @@
objects = {
/* Begin PBXBuildFile section */
D6055E46234D1B31007BEF52 /* ContentTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6055E45234D1B31007BEF52 /* ContentTableViewController.swift */; };
D610D2D6233945AF009EB06A /* SheetImagePicker.h in Headers */ = {isa = PBXBuildFile; fileRef = D610D2D4233945AF009EB06A /* SheetImagePicker.h */; settings = {ATTRIBUTES = (Public, ); }; };
D610D2E3233945B9009EB06A /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D610D2E2233945B9009EB06A /* AppDelegate.swift */; };
D610D2E5233945B9009EB06A /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D610D2E4233945B9009EB06A /* SceneDelegate.swift */; };
@ -49,7 +48,6 @@
/* End PBXCopyFilesBuildPhase section */
/* Begin PBXFileReference section */
D6055E45234D1B31007BEF52 /* ContentTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentTableViewController.swift; sourceTree = "<group>"; };
D610D2D1233945AF009EB06A /* SheetImagePicker.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SheetImagePicker.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D610D2D4233945AF009EB06A /* SheetImagePicker.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SheetImagePicker.h; sourceTree = "<group>"; };
D610D2D5233945AF009EB06A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@ -126,7 +124,6 @@
D610D2E2233945B9009EB06A /* AppDelegate.swift */,
D610D2E4233945B9009EB06A /* SceneDelegate.swift */,
D610D2E6233945B9009EB06A /* ViewController.swift */,
D6055E45234D1B31007BEF52 /* ContentTableViewController.swift */,
D610D2E8233945B9009EB06A /* Main.storyboard */,
D610D2EB233945BB009EB06A /* Assets.xcassets */,
D610D2ED233945BB009EB06A /* LaunchScreen.storyboard */,
@ -270,7 +267,6 @@
files = (
D610D2E7233945B9009EB06A /* ViewController.swift in Sources */,
D610D2E3233945B9009EB06A /* AppDelegate.swift in Sources */,
D6055E46234D1B31007BEF52 /* ContentTableViewController.swift in Sources */,
D610D2E5233945B9009EB06A /* SceneDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
@ -482,13 +478,13 @@
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = 46G5674823;
DEVELOPMENT_TEAM = HGYVAQA9FW;
INFOPLIST_FILE = SheetImagePickerTest/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = net.shadowfacts.dev.SheetImagePickerTest;
PRODUCT_BUNDLE_IDENTIFIER = net.shadowfacts.SheetImagePickerTest;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
@ -501,13 +497,13 @@
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = 46G5674823;
DEVELOPMENT_TEAM = HGYVAQA9FW;
INFOPLIST_FILE = SheetImagePickerTest/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = net.shadowfacts.dev.SheetImagePickerTest;
PRODUCT_BUNDLE_IDENTIFIER = net.shadowfacts.SheetImagePickerTest;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";

View File

@ -11,8 +11,6 @@ import UIKit
public protocol SheetContainerViewControllerDelegate {
func sheetContainer(_ sheetContainer: SheetContainerViewController, willSnapToDetent detent: Detent) -> Bool
func sheetContainer(_ sheetContainer: SheetContainerViewController, didSnapToDetent detent: Detent)
func sheetContainerContentScrollView(_ sheetContainer: SheetContainerViewController) -> UIScrollView?
func sheetContainer(_ sheetContainer: SheetContainerViewController, topContentOffsetForScrollView scrollView: UIScrollView) -> CGFloat
}
// default no-op implementation
@ -21,19 +19,13 @@ public extension SheetContainerViewControllerDelegate {
return true
}
func sheetContainer(_ sheetContainer: SheetContainerViewController, didSnapToDetent detent: Detent) {}
func sheetContainerContentScrollView(_ sheetContainer: SheetContainerViewController) -> UIScrollView? {
return nil
}
func sheetContainer(_ sheetContainer: SheetContainerViewController, topContentOffsetForScrollView scrollView: UIScrollView) -> CGFloat {
return 0
}
}
public class SheetContainerViewController: UIViewController {
public var delegate: SheetContainerViewControllerDelegate?
public let content: UIViewController
let content: UIViewController
public var detents: [Detent] = [.bottom, .middle, .top]
var topDetent: (detent: Detent, offset: CGFloat) {
@ -52,10 +44,6 @@ public class SheetContainerViewController: UIViewController {
var dimmingView: UIView!
public var minimumDimmingAlpha: CGFloat = 0
public var maximumDimmingAlpha: CGFloat = 0.75
var contentScrollView: UIScrollView? {
delegate?.sheetContainerContentScrollView(self) ?? content.view as? UIScrollView
}
public init(content: UIViewController) {
self.content = content
@ -97,12 +85,7 @@ public class SheetContainerViewController: UIViewController {
])
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(panGestureRecognized(_:)))
panGesture.delegate = self
content.view.addGestureRecognizer(panGesture)
if let scrollView = contentScrollView {
scrollView.panGestureRecognizer.addTarget(self, action: #selector(scrollViewPanGestureRecognized))
}
}
@objc func panGestureRecognized(_ recognizer: UIPanGestureRecognizer) {
@ -112,88 +95,44 @@ public class SheetContainerViewController: UIViewController {
case .changed:
let translation = recognizer.translation(in: content.view)
let realOffset = initialConstant + translation.y
setTopOffset(realOffset)
var realOffset = initialConstant + translation.y
let topOffset = topDetent.offset
if realOffset < topOffset {
let smoothed = smoothstep(value: realOffset, from: topOffset, to: 0)
realOffset = topOffset - smoothed * maximumStretchDistance
}
topConstraint.constant = realOffset
dimmingView.alpha = lerp(realOffset, min: topOffset, max: bottomDetent.offset, from: maximumDimmingAlpha, to: minimumDimmingAlpha)
case .ended:
let velocity = recognizer.velocity(in: view)
springToNearestDetent(verticalVelocity: velocity.y)
default:
break
}
}
@objc func scrollViewPanGestureRecognized(_ recognizer: UIPanGestureRecognizer) {
guard let scrollView = recognizer.view as? UIScrollView else { return }
let velocity = recognizer.velocity(in: scrollView)
let topContentOffset: CGFloat = delegate?.sheetContainer(self, topContentOffsetForScrollView: scrollView) ?? 0
let shouldMoveSheetDown = scrollView.contentOffset.y <= topContentOffset && velocity.y > 0 // scrolled to top and dragging down
let shouldMoveSheetUp = topConstraint.constant > topDetent.offset && velocity.y < 0 // not fully expanded and dragging up
let shouldMoveSheet = shouldMoveSheetDown || shouldMoveSheetUp
if shouldMoveSheet {
scrollView.bounces = false
scrollView.setContentOffset(CGPoint(x: 0, y: topContentOffset), animated: false)
}
switch recognizer.state {
case .changed:
if shouldMoveSheet {
let translation = recognizer.translation(in: scrollView)
setTopOffset(topConstraint.constant + translation.y)
recognizer.setTranslation(.zero, in: scrollView)
let springToDetent: (Detent, CGFloat)
if abs(velocity.y) > minimumDetentJumpVelocity,
let detentInVelocityDirection = nearestDetentOffset(currentOffset: topConstraint.constant, direction: velocity.y) {
springToDetent = detentInVelocityDirection
} else if let nearestDetent = nearestDetentOffset(currentOffset: topConstraint.constant) {
springToDetent = nearestDetent
} else {
return
}
case .ended:
scrollView.bounces = true
if shouldMoveSheet {
springToNearestDetent(verticalVelocity: velocity.y)
}
default:
break
}
}
func setTopOffset(_ offset: CGFloat) {
var offset = offset
let topOffset = topDetent.offset
if offset < topOffset {
let smoothed = smoothstep(value: offset, from: topOffset, to: 0)
offset = topOffset - smoothed * maximumStretchDistance
}
topConstraint.constant = offset
dimmingView.alpha = lerp(offset, min: topOffset, max: bottomDetent.offset, from: maximumDimmingAlpha, to: minimumDimmingAlpha)
}
func springToNearestDetent(verticalVelocity velocity: CGFloat) {
let springToDetent: (Detent, CGFloat)
if abs(velocity) > minimumDetentJumpVelocity,
let detentInVelocityDirection = nearestDetentOffset(currentOffset: topConstraint.constant, direction: velocity) {
springToDetent = detentInVelocityDirection
} else if let nearestDetent = nearestDetentOffset(currentOffset: topConstraint.constant) {
springToDetent = nearestDetent
} else {
if delegate?.sheetContainer(self, willSnapToDetent: springToDetent.0) ?? true {
let springDistance = abs(topConstraint.constant - springToDetent.1)
self.topConstraint.constant = springToDetent.1
let springVelocity = velocity.y / springDistance
UIView.animate(withDuration: 0.35, delay: 0, usingSpringWithDamping: 0.75, initialSpringVelocity: springVelocity, animations: {
self.view.layoutIfNeeded()
self.dimmingView.alpha = lerp(springToDetent.1, min: self.topDetent.offset, max: self.bottomDetent.offset, from: self.maximumDimmingAlpha, to: self.minimumDimmingAlpha)
}, completion: { (finished) in
self.delegate?.sheetContainer(self, didSnapToDetent: springToDetent.0)
})
}
default:
return
}
if delegate?.sheetContainer(self, willSnapToDetent: springToDetent.0) ?? true {
let springDistance = abs(topConstraint.constant - springToDetent.1)
self.topConstraint.constant = springToDetent.1
let springVelocity = velocity / springDistance
UIView.animate(withDuration: 0.35, delay: 0, usingSpringWithDamping: 0.75, initialSpringVelocity: springVelocity, animations: {
self.view.layoutIfNeeded()
self.dimmingView.alpha = lerp(springToDetent.1, min: self.topDetent.offset, max: self.bottomDetent.offset, from: self.maximumDimmingAlpha, to: self.minimumDimmingAlpha)
}, completion: { (finished) in
self.delegate?.sheetContainer(self, didSnapToDetent: springToDetent.0)
})
}
}
func nearestDetentOffset(currentOffset: CGFloat) -> (Detent, CGFloat)? {
@ -215,15 +154,6 @@ public class SheetContainerViewController: UIViewController {
}
extension SheetContainerViewController: UIGestureRecognizerDelegate {
public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
if topConstraint.constant <= topDetent.offset {
return (gestureRecognizer as! UIPanGestureRecognizer).translation(in: gestureRecognizer.view!).y > 0
}
return true
}
}
extension SheetContainerViewController: UIViewControllerTransitioningDelegate {
public func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return SheetContainerPresentationAnimationController()

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="15702" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="15400" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="15704"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="15404"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
@ -15,44 +15,18 @@
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" translatesAutoresizingMaskIntoConstraints="NO" id="hIF-hV-InX">
<rect key="frame" x="173" y="388" width="68" height="120"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="tfd-T0-fMO">
<rect key="frame" x="0.0" y="0.0" width="68" height="30"/>
<state key="normal" title="Plain"/>
<connections>
<action selector="plainPressed:" destination="BYZ-38-t0r" eventType="touchUpInside" id="tmi-VL-j61"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="fT8-yG-J2R">
<rect key="frame" x="0.0" y="30" width="68" height="30"/>
<state key="normal" title="Table"/>
<connections>
<action selector="tablePressed:" destination="BYZ-38-t0r" eventType="touchUpInside" id="PZl-oC-0t4"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="39x-b2-4Hx">
<rect key="frame" x="0.0" y="60" width="68" height="30"/>
<state key="normal" title="Nav"/>
<connections>
<action selector="navPressed:" destination="BYZ-38-t0r" eventType="touchUpInside" id="Jc9-R9-VZW"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="eUF-7N-Qgr">
<rect key="frame" x="0.0" y="90" width="68" height="30"/>
<state key="normal" title="Nav Table"/>
<connections>
<action selector="navTablePressed:" destination="BYZ-38-t0r" eventType="touchUpInside" id="fSY-4h-jE3"/>
</connections>
</button>
</subviews>
</stackView>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="tfd-T0-fMO">
<rect key="frame" x="184" y="433" width="46" height="30"/>
<state key="normal" title="Button"/>
<connections>
<action selector="buttonPressed:" destination="BYZ-38-t0r" eventType="touchUpInside" id="tmi-VL-j61"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
<constraints>
<constraint firstItem="hIF-hV-InX" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="MT3-wG-T9R"/>
<constraint firstItem="hIF-hV-InX" firstAttribute="centerY" secondItem="8bC-Xf-vdC" secondAttribute="centerY" id="Unx-Jo-GQK"/>
<constraint firstItem="tfd-T0-fMO" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="Cpn-cM-hYr"/>
<constraint firstItem="tfd-T0-fMO" firstAttribute="centerY" secondItem="8bC-Xf-vdC" secondAttribute="centerY" id="YJH-oX-yBz"/>
</constraints>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
</view>

View File

@ -1,60 +0,0 @@
//
// ContentTableViewController.swift
// SheetImagePickerTest
//
// Created by Shadowfacts on 10/8/19.
// Copyright © 2019 Shadowfacts. All rights reserved.
//
import UIKit
class ContentTableViewController: UITableViewController {
init() {
super.init(style: .plain)
}
required init?(coder: NSCoder) {
fatalError()
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "testCell")
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 40
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "testCell", for: indexPath)
cell.textLabel!.text = "\(indexPath.row)"
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let navController = navigationController else { return }
let vc = UIViewController()
vc.view.backgroundColor = .systemBackground
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "\(indexPath.row)"
vc.view.addSubview(label)
NSLayoutConstraint.activate([
label.centerXAnchor.constraint(equalTo: vc.view.centerXAnchor),
label.centerYAnchor.constraint(equalTo: vc.view.centerYAnchor)
])
navController.pushViewController(vc, animated: true)
}
}

View File

@ -17,9 +17,12 @@ class ViewController: UIViewController {
view.backgroundColor = .green
}
@IBAction func plainPressed(_ sender: Any) {
@IBAction func buttonPressed(_ sender: Any) {
let content = UIViewController()
content.view.translatesAutoresizingMaskIntoConstraints = false
content.view.layer.masksToBounds = true
content.view.layer.cornerRadius = view.bounds.width * 0.02
let blurEffect = UIBlurEffect(style: .systemChromeMaterial)
let blurView = UIVisualEffectView(effect: blurEffect)
blurView.translatesAutoresizingMaskIntoConstraints = false
@ -36,19 +39,6 @@ class ViewController: UIViewController {
label.centerXAnchor.constraint(equalTo: blurView.contentView.centerXAnchor),
label.centerYAnchor.constraint(equalTo: blurView.contentView.centerYAnchor)
])
let sheet = SheetContainerViewController(content: content)
sheet.delegate = self
sheet.detents = [.bottom, .middle, .top]
present(sheet, animated: true)
}
@IBAction func tablePressed(_ sender: Any) {
let content = ContentTableViewController()
content.view.translatesAutoresizingMaskIntoConstraints = false
content.view.layer.masksToBounds = true
content.view.layer.cornerRadius = view.bounds.width * 0.02
let sheet = SheetContainerViewController(content: content)
sheet.delegate = self
@ -57,44 +47,6 @@ class ViewController: UIViewController {
present(sheet, animated: true)
}
@IBAction func navPressed(_ sender: Any) {
let root = UIViewController()
root.view.backgroundColor = .systemBackground
let label = UILabel()
label.text = "Root VC"
label.translatesAutoresizingMaskIntoConstraints = false
root.view.addSubview(label)
NSLayoutConstraint.activate([
label.centerXAnchor.constraint(equalTo: root.view.centerXAnchor),
label.centerYAnchor.constraint(equalTo: root.view.centerYAnchor)
])
let nav = UINavigationController(rootViewController: root)
nav.view.translatesAutoresizingMaskIntoConstraints = false
nav.view.layer.masksToBounds = true
nav.view.layer.cornerRadius = view.bounds.width * 0.02
let sheet = SheetContainerViewController(content: nav)
sheet.delegate = self
sheet.detents = [.bottom, .middle, .top]
present(sheet, animated: true)
}
@IBAction func navTablePressed(_ sender: Any) {
let table = ContentTableViewController()
let nav = UINavigationController(rootViewController: table)
nav.view.translatesAutoresizingMaskIntoConstraints = false
nav.view.layer.masksToBounds = true
nav.view.layer.cornerRadius = view.bounds.width * 0.02
let sheet = SheetContainerViewController(content: nav)
sheet.delegate = self
sheet.detents = [.bottom, .middle, .top]
present(sheet, animated: true)
}
}
extension ViewController: SheetContainerViewControllerDelegate {
@ -105,18 +57,4 @@ extension ViewController: SheetContainerViewControllerDelegate {
}
return true
}
func sheetContainerContentScrollView(_ sheetContainer: SheetContainerViewController) -> UIScrollView? {
if let navController = sheetContainer.content as? UINavigationController, let scrollView = navController.visibleViewController?.view as? UIScrollView {
return scrollView
} else {
return nil
}
}
func sheetContainer(_ sheetContainer: SheetContainerViewController, topContentOffsetForScrollView scrollView: UIScrollView) -> CGFloat {
if let navController = sheetContainer.content as? UINavigationController, navController.visibleViewController?.view is UIScrollView {
return -navController.navigationBar.bounds.height
} else {
return 0
}
}
}