EARestrictedScrollView
UIScrollView
子类,具有限制滚动区域的功能。
在普通的UIScrollView
中,只能更改contentSize
,而不能更改滚动区域的起点。这个简单且通用的解决方案允许使用CGRect
限制滚动区域。
对于Objective-C项目,您可以使用1.1.0
版本。
安装
您可以使用Carthage、CocoaPods、Swift Package Manager或完全手动设置EARestrictedScrollView。
Carthage
-
将EARestrictedScrollView添加到项目的
Cartfile
github "ealeksandrov/EARestrictedScrollView"
-
在项目目录中运行
carthage update
。 -
在您应用程序目标的“常规”设置选项卡中,在“链接框架和库”部分,从磁盘上的
Carthage/Build/iOS/
文件夹中将EARestrictedScrollView.framework
拖放到“链接框架和库”列表中。 -
在你的应用的“构建阶段”设置标签页中,点击“+”图标,选择“新建运行脚本阶段”。创建一个包含以下内容的运行脚本
/usr/local/bin/carthage copy-frameworks
并在“输入文件”下添加框架的路径
$(SRCROOT)/Carthage/Build/iOS/EARestrictedScrollView.framework
CocoaPods
-
将 EARestrictedScrollView 添加到你的
Podfile
项目中。pod 'EARestrictedScrollView', '~> 2.1.0'
-
在你的项目目录中运行
pod update
或pod install
。
Swift 包管理器
- 将 EARestrictedScrollView 添加到你的项目“Swift 包”部分或
Package.swift
文件。
.package(url: "https://github.com/ealeksandrov/EARestrictedScrollView.git", from: "2.1.0")
手动设置
-
从 Github 克隆 EARestrictedScrollView。
-
复制并添加
EARestrictedScrollView
头文件和实现代码到你的项目中。 -
现在你可以通过添加以下导入来使用 EARestrictedScrollView:
import EARestrictedScrollView
使用方法
可以从代码中创建,就像通常一样
override func viewDidLoad() {
super.viewDidLoad()
restrictedScrollView = EARestrictedScrollView(frame: view.bounds)
restrictedScrollView.alwaysBounceVertical = true
restrictedScrollView.alwaysBounceHorizontal = true
view.addSubview(restrictedScrollView)
let imageView = UIImageView(image: UIImage(named: "milky-way"))
restrictedScrollView.addSubview(imageView)
restrictedScrollView.contentSize = imageView.frame.size
}
或从 Interface Builder 创建
使用新的 restrictionArea
属性更新滚动区域。将通过将 CGRectZero
传递给 restrictionArea
来重置限制。
func flipSwitch(sender: UISwitch) {
if sender.on {
restrictedScrollView.restrictionArea = sender.superview!.frame
} else {
restrictedScrollView.restrictionArea = CGRectZero
}
}
要访问子视图,请使用 containedSubviews
属性。由于 subviews
重写引起了一些 自动布局问题,因此它在 0.2.0 版本中添加。
let subviews = restrictedScrollView.containedSubviews
作者
由 Evgeny Aleksandrov 创建和维护(@ealeksandrov)。
许可证
EARestrictedScrollView
在MIT许可证下可用。更多信息请参阅LICENSE.md文件。