TestsTested | ✗ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Feb 2017 |
SPMSupports SPM | ✗ |
Maintained by ktanaka117.
ScrollingFollowView is a simple view which follows UIScrollView scrolling.
ScrollingFollowView only supports Autolayout use. (I don’t force this feature on library users. I’m going to modify this feature. Plz look Future Improvements.) You must make layouts with InterfaceBuilder like under sample image.
@IBOutlet weak var scrollingFollowView: ScrollingFollowView!
// NSLayoutConstraint of moving edge.
@IBOutlet weak var constraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
let scrollingFollowViewHeight = scrollingFollowView.frame.size.height
let statusBarHeight = UIApplication.sharedApplication().statusBarFrame.size.height
// First setup
// In default use, maxFollowPoint should be maxPoint of following to scroll DOWN.
// In default use, minFollowPoint should be maxPoint of following to scroll UP.
scrollingFollowView.setup(constraint: constraint, maxFollowPoint: scrollingFollowViewHeight + statusBarHeight, minFollowPoint: 0)
}
func scrollViewDidScroll(scrollView: UIScrollView) {
// scrollingFollowView follows UIScrollView scrolling.
scrollingFollowView.didScroll(scrollView)
}
You can use show(animated:)
and hide(animated:)
methods to ScrollingFollowView object like these:
// show
@IBAction func showButton() {
scrollingFollowView.show(true)
scrollingFollowView.resetPreviousPoint(scrollView)
}
// hide
@IBAction func hideButton() {
scrollingFollowView.hide(false)
scrollingFollowView.resetPreviousPoint(scrollView)
}
resetPreviousPoint(scrollView:)
must call after show(animated:)
and hide(animated)
, previousPoint
is ScrollingFollowView’s private property which used changing constraint.constant value.
And show and hide methods are declared as:
func show(animated: Bool, duration: Double = 0.2, completionHandler: (()->())? = nil)
func hide(animated: Bool, duration: Double = 0.2, completionHandler: (()->())? = nil)
so you can set animation duration
and completionHandler
which used after animation:
// show
@IBAction func showButton() {
scrollingFollowView.show(true, duration: 0.6) { print("showed") }
scrollingFollowView.resetPreviousPoint(scrollView)
}
// hide
@IBAction func hideButton() {
scrollingFollowView.hide(true, duration: 0.5) { print("hid") }
scrollingFollowView.resetPreviousPoint(scrollView)
}
You can use hide and show delay points like under gif:
Add setupDelayPoints(pointOfStartingHiding:pointOfStartingShowing:)
. For example, write in viewDidLoad()
:
override func viewDidLoad() {
super.viewDidLoad()
let scrollingFollowViewHeight = scrollingFollowView.frame.size.height
let statusBarHeight = UIApplication.sharedApplication().statusBarFrame.size.height
scrollingFollowView.setup(constraint: constraint, maxFollowPoint: scrollingFollowViewHeight + statusBarHeight, minFollowPoint: 0)
scrollingFollowView.setupDelayPoints(pointOfStartingHiding: 100, pointOfStartingShowing: 50)
}
You can control half-display state like under gif:
allowHalfDisplay
disallowHalfDisplay(Auto Animation)
You have to add parameter allowHalfDisplay
to setup(constraint:maxFollowPoint:minFollowPoint:allowHalfDesplay:)
function.
override func viewDidLoad() {
super.viewDidLoad()
let scrollingFollowViewHeight = scrollingFollowView.frame.size.height
let statusBarHeight = UIApplication.sharedApplication().statusBarFrame.size.height
// allowHalfDisplay: Default is false.
// When allowHalfDisplay is true, ScrollingFollowView can display following scrolling state.
// When allowHalfDisplay is false, ScrollingFollowView can't display following scrolling state. (If ScrollingFollowView is half-display state, it automatically animates.)
scrollingFollowView.setup(constraint: constraint, maxFollowPoint: scrollingFollowViewHeight + statusBarHeight, minFollowPoint: 0, allowHalfDisplay: true)
}
and you also have to call didEndScrolling()
and didEndScrolling(_ decelerate:)
at scrollViewDidEndDecelerating(_ scrollView:)
and scrollViewDidEndDragging(_ scrollView:willDecelerate:)
.
extension ViewController: UIScrollViewDelegate {
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
navBarScrollingFollowView.didEndScrolling()
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
navBarScrollingFollowView.didEndScrolling(decelerate)
}
}
When you have some opinions or ideas about this library, send me a reply on Twitter.
ScrollingFollowView is released under the MIT license. Go read the LICENSE file for more information.