UIRefreshControl 的替代品,并且具有更多功能。
ScrollEdgeControl 是一个类似于 UIRefreshControl 的 UI 组件,但它更进一步抽象化。
ScrollEdgeControl 可以附加到滚动视图的每个边缘。 例如,下拉、上拉、左拉、右拉来触发某些活动,例如刷新。(拉动激活)
它还支持禁用拉动激活,这在显示为列表底部的加载指示器时非常有用。
此控件上的内容支持显示您想要的任何视图。
垂直
顶部 | 底部 |
---|---|
![]() |
![]() |
水平
左侧 | 右侧 |
---|---|
![]() |
![]() |
更多模式
下拉刷新和额外加载 |
---|
![]() |
Cocoapods
包含自定义活动指示器
pod "ScrollEdgeControl"
如果您只需要核心组件
pod "ScrollEdgeControl/Core"
SwiftPM
dependencies: [
.package(url: "https://github.com/muukii/ScrollEdgeControl.git", exact: "<VERSION>")
]
设置
let scrollEdgeControl = ScrollEdgeControl(
edge: .top, // ✅ a target edge to add this control
configuration: .init(), // ✅ customizing behavior of this control
activityIndicatorView: ScrollEdgeActivityIndicatorView(color: .black) // ✅ Adding your own component to display on this control
)
let scrollableView: UIScrollView // ✅ could be `UIScrollView`, `UITableView`, `UICollectionView`
scrollableView.addSubview(scrollEdgeControl) // ✅ Could add multiple controls for each edge
处理
scrollEdgeControl.handlers.onDidActivate = { instance in
...
// after activity completed
instance.setActivityState(.inactive, animated: true)
}
ScrollEdgeControl 支持通过以下协议显示您想要的任何内容。
这意味着您可以创建完全自定义的设计来显示活动。
protocol ScrollEdgeActivityIndicatorViewType
class YourContent: ScrollEdgeActivityIndicatorViewType {
func update(withState state: ScrollEdgeControl.ActivatingState) {
// Needs implementation
}
}
let scrollEdgeControl: ScrollEdgeControl
let yourContent: YourContent
scrollEdgeControl.setActivityIndicatorView(yourContent)
创建一个像 UIRefreshControl 这样的组件相当困难。
观察滚动、布局、更新 content-inset。
当我们检查 UIRefreshControl 时,我们注意到 UIScrollView 的 content-inset 在刷新时返回 0。但 adjusted-content-inset 显示实际值。
(例如,content-inset-top: 0, adjusted-content-inset-top: 50)
因此 UIRefreshControl 在内部工作,通过更改 content-inset 来防止副作用扩散。
我们需要这个技巧来为 UIScrollView 创建我们自己的自定义控件。
最后,我们决定使用方法调配 (method-swizzling) 来完成这项工作。
交换 content-inset 的 getter 和 setter,管理本地 content-inset,然后我们返回给外部的值包括添加、减去实际的 content-inset 和本地 content-inset。
Advance 帮助滚动视图中的动画。
它是一个使用 CADisplayLink 运行具有完全可计算值的动画的库。
UIScrollView 的动画不在 CoreAnimation 中。
这些是在 CPU 中每帧计算的。这就是为什么我们可以在 UIScrollView 委托中处理它。
我们可以使用 UIView 动画更新 content offset,但有时会出现奇怪的动画。
为了解决它,使用 CADisplayLink,更新每帧的值。
参考
MIT