ContainerController
UI组件。这是从应用https://www.apple.com/ios/maps/中复制滑动面板。
预览
要求
安装
CocoaPods
ContainerControllerSwift可通过CocoaPods获取。要安装它,只需将以下行添加到Podfile文件中
pod 'ContainerControllerSwift'
使用Xcode 11和Swift Package Manager
请按照此文档操作。
入门指南
import UIKit
import ContainerControllerSwift
class ViewController: UIViewController, ContainerControllerDelegate {
var container: ContainerController!
override func viewDidLoad() {
super.viewDidLoad()
// Create ContainerController Layout object
let layout = ContainerLayout()
layout.startPosition = .hide
layout.backgroundShadowShow = true
layout.positions = ContainerPosition(top: 70, middle: 250, bottom: 70)
// Create ContainerController object, along with the container.view
// Pass the current UIViewController
let container = ContainerController(addTo: self, layout: layout)
container.view.cornerRadius = 15
container.view.addShadow()
// Create subclass scrollView
let tableView = UITableView()
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView.delegate = self
tableView.dataSource = self
// Add scrollView to container
container.add(scrollView: tableView)
// Finishing settings ContainerController,
// Animated move position Top
container.move(type: .top)
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
// Remove the subviews ContainerController
container.remove()
container = nil
}
}
操作
通过动画移动位置
container.move(type: .top)
container.move(type: .middle)
container.move(type: .bottom)
向ContainerController视图添加可能的自定义子视图
ScrollView
添加 let tableView = UITableView()
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView.backgroundColor = .clear
tableView.tableFooterView = UIView()
tableView.delegate = self
tableView.dataSource = self
// Add scrollView to container
container.add(scrollView: tableView)
Delegate
👆
委托到自身 self
,那么需要在 ContainerController 中调用 4 个函数
如果将 ScrollView (TableView, CollectionView, TextView) 的委托实现到 extension ViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
container.scrollViewDidScroll(scrollView)
}
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
container.scrollViewWillBeginDragging(scrollView)
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
container.scrollViewDidEndDecelerating(scrollView)
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
container.scrollViewDidEndDragging(scrollView, willDecelerate: decelerate)
}
}
extension ViewController: UITableViewDelegate {
...
}
extension ViewController: UITableViewDataSource {
...
}
HeaderView
添加 let headerView = ExampleHeaderGripView()
headerView.height = 20
container.add(headerView: headerView)
FooterView
添加 let tabBarView = HeaderTabBarView()
tabBarView.height = 49.0
container.add(footerView: tabBarView)
View
添加自定义 // Add custom shadow
let layer = container.view.layer
layer.shadowOpacity = 0.5
layer.shadowColor = UIColor.red.cgColor
layer.shadowOffset = CGSize(width: 1, height: 4)
layer.shadowRadius = 5
// Add view in container.view
let viewRed = UIView(frame: CGRect(x: 50, y: 50, width: 50, height: 50))
viewRed.backgroundColor = .systemRed
container.view.addSubview(viewRed)
// Add view under scrollView container.view
let viewGreen = UIView(frame: CGRect(x: 25, y: 25, width: 50, height: 50))
viewGreen.backgroundColor = .systemGreen
container.view.insertSubview(viewGreen, at: 0)
⚙️
设置布局
ContainerLayout
来自定义布局
在初始化时使用创建子类 class NewContainerLayout: ContainerLayout {
override init() {
super.init()
// Initialization start position.
startPosition = .hide
// Disables any moving with gestures.
movingEnabled = true
// Sets the new value for positions of animated movement (top, middle, bottom).
positions = ContainerPosition(top: 70, middle: 250, bottom: 70)
// Sets insets container.view (left, right).
insets = ContainerInsets(right: 20, left: 20)
}
}
class ViewController: UIViewController {
var container: ContainerController!
override func viewDidLoad() {
super.viewDidLoad()
container = ContainerController(addTo: self, layout: NewContainerLayout())
container.move(type: .top)
}
}
ContainerLayout
或者创建对象override func viewDidLoad() {
super.viewDidLoad()
// Create ContainerController Layout object
let layout = ContainerLayout()
layout.startPosition = .hide
layout.backgroundShadowShow = true
layout.positions = ContainerPosition(top: 70, middle: 250, bottom: 70)
container = ContainerController(addTo: self, layout: layout)
}
立即更改设置
// Properties
container.set(movingEnabled: true)
container.set(trackingPosition: false)
container.set(footerPadding: 100)
// Add ScrollInsets Top/Bottom
container.set(scrollIndicatorTop: 5) // ↓
container.set(scrollIndicatorBottom: 5) // ↑
// Positions
container.set(top: 70) // ↓
container.set(middle: 250) // ↑
container.set(bottom: 80) // ↑
// Middle Enable/Disable
container.set(middle: 250)
container.set(middle: nil)
// Background Shadow
container.set(backgroundShadowShow: true)
// Insets View
container.set(left: 5) // →
container.set(right: 5) // ←
// Landscape params
container.setLandscape(top: 30)
container.setLandscape(middle: 150)
container.setLandscape(bottom: 70)
container.setLandscape(middle: nil)
container.setLandscape(backgroundShadowShow: false)
container.setLandscape(left: 10)
container.setLandscape(right: 100)
视图
ContainerController 使用现成的解决方案
ContainerView
在创建ContainerController时自动生成。使用现成的解决方案来更改半径、添加阴影和模糊效果。
圆角
更改 // Change cornerRadius global for all subviews
container.view.cornerRadius = 15
阴影
添加图层 container.view.addShadow(opacity: 0.1)
模糊
添加背景 // add blur UIVisualEffectView
container.view.addBlur(style: .dark)
更多详情
更改屏幕上的位置:顶部 中部 底部
// These parameters set the new position value.
container.layout.positions = ContainerPosition(top: 70, middle: 250, bottom: 70)
// Change settings right away
container.set(top: 70) // ↓
container.set(middle: 250) // ↑
container.set(bottom: 80) // ↑
为视图自定义缩进
// Sets insets container.view (left, right).
container.layout.insets = ContainerInsets(right: 20, left: 20)
container.layout.landscapeInsets = ContainerInsets(right: 20, left: 100)
// Change settings right away
container.set(left: 5) // →
container.set(right: 5) // ←
container.setLandscape(left: 10)
container.setLandscape(right: 100)
为景观方向定制
// Sets the background shadow under container. (Default: backgroundShadowShow).
container.layout.landscapeBackgroundShadowShow = false
// Sets the new value for positions of animated movement (top, middle, bottom). (Default: positions).
container.layout.landscapePositions = ContainerPosition(top: 20, middle: 150, bottom: 70)
// Sets insets container.view (left, right). (Default: insets).
container.layout.landscapeInsets = ContainerInsets(right: 20, left: 100)
// Change settings right away
container.setLandscape(top: 30)
container.setLandscape(middle: 150)
container.setLandscape(bottom: 70)
container.setLandscape(middle: nil)
container.setLandscape(backgroundShadowShow: false)
container.setLandscape(left: 10)
container.setLandscape(right: 100)
控制footerView参数
// Padding-top from container.view, if headerView is added, then its + height is summed.
container.layout.footerPadding = 100
// Tracking position container.view during animated movement.
container.layout.trackingPosition = false
// Change settings right away
container.set(footerPadding: 100)
container.set(trackingPosition: false)
委托
ContainerController class ViewController: UIViewController, ContainerControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let container = ContainerController(addTo: self, layout: layout)
container.delegate = self
}
}
/// Reports rotation and orientation changes
func containerControllerRotation(_ containerController: ContainerController) {
...
}
/// Reports a click on the background shadow
func containerControllerShadowClick(_ containerController: ContainerController) {
...
}
/// Reports the changes current position of the container, after its use
func containerControllerMove(_ containerController: ContainerController, position: CGFloat, type: ContainerMoveType, animation: Bool) {
...
}
作者
许可
ContainerController 授权MIT许可。更多信息请查看LICENSE文件。