Skip to content

Instantly share code, notes, and snippets.

@demonar
Last active September 15, 2015 02:33
Show Gist options
  • Save demonar/1882acdbbe3e3e215998 to your computer and use it in GitHub Desktop.
Save demonar/1882acdbbe3e3e215998 to your computer and use it in GitHub Desktop.
import UIKit
extension UIView {
func scrollToY(y: CGFloat) {
scrollToY(y, velocity: 0.25)
}
func scrollToView(view: UIView) {
scrollToView(view, velocity: 0.25)
}
func scrollElement(view: UIView, toPoint y: CGFloat) {
scrollElement(view, toPoint: y, velocity: 0.25)
}
func scrollToY(y: CGFloat, velocity: NSTimeInterval) {
UIView.animateWithDuration(velocity, animations: { () -> Void in
self.transform = CGAffineTransformMakeTranslation(0, y)
})
}
func scrollToView(view: UIView, velocity: NSTimeInterval) {
let theFrame = view.frame
var y : CGFloat = theFrame.origin.y - 15
y -= (y/1.7)
self.scrollToY(-y, velocity: velocity)
}
func scrollElement(view: UIView, toPoint y: CGFloat, velocity: NSTimeInterval) {
let originY = view.frame.origin.y
let diff = y - originY
if (diff < 0) {
self.scrollToY(diff, velocity: velocity)
}
else {
self.scrollToY(0, velocity: velocity)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment