Skip to content

Instantly share code, notes, and snippets.

@pistatium
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pistatium/26abed62cfec8079c9f3 to your computer and use it in GitHub Desktop.
Save pistatium/26abed62cfec8079c9f3 to your computer and use it in GitHub Desktop.
スクロールで浮き沈みする奴 Swift版
/**
* Copyright (c) 2014 pistatium
*
* This software is released under the MIT License.
*
* http://opensource.org/licenses/mit-license.php
*/
/*
* cf.
* http://qiita.com/yimajo/items/34d6ba63e4eadfc8f421
* https://github.com/sergio83/Movable-Footer/tree/1d2603e7e5efbc1b38e53b372c6ee9b046cd2cbd
*/
import Foundation
import UIKit
public class MovableFooterController : NSObject
{
private let animationDuration = 0.22
private var footerView:UIView
private var scrollView:UIScrollView
private var containerView:UIView
private var beginScrollOffsetY:CGFloat = 0.0
enum ScrollState {
case Nutoral
case InAnimation
}
private var scrollState = ScrollState.Nutoral
init(footerView:UIView,scrollView:UIScrollView,containerView:UIView)
{
self.footerView = footerView
self.scrollView = scrollView
self.containerView = containerView
super.init()
}
// MARK: - UIScrollViewDelegate
//------------------------------------------------------------------------------------------------------------------------------------------
public func scrollViewWillBeginDragging(scrollView: UIScrollView!)
{
self.beginScrollOffsetY = scrollView.contentOffset.y
}
public func scrollViewDidScroll(scrollView: UIScrollView!)
{
if ScrollState.InAnimation == scrollState {
return
}
if (beginScrollOffsetY < scrollView.contentOffset.y && !footerView.hidden) {
UIView.animateWithDuration(animationDuration,
animations: {
self.scrollState = ScrollState.InAnimation
let rect = self.footerView.frame
self.footerView.frame = CGRectMake(
rect.origin.x,
rect.origin.y + rect.size.height,
rect.size.width,
rect.size.height
)
}, completion: { finished in
self.footerView.hidden = true
self.scrollState = ScrollState.Nutoral
}
)
} else if (scrollView.contentOffset.y < beginScrollOffsetY && footerView.hidden && 0.0 != beginScrollOffsetY) {
self.footerView.hidden = false
UIView.animateWithDuration(animationDuration,
animations: {
self.scrollState = ScrollState.InAnimation
let rect = self.footerView.frame
self.footerView.frame = CGRectMake(
rect.origin.x,
rect.origin.y - rect.size.height,
rect.size.width,
rect.size.height
)
}, completion: { finished in
self.scrollState = ScrollState.Nutoral
}
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment