@@ -15,6 +15,7 @@ import UIKit
1515 fileprivate var scrollView : UIScrollView !
1616 @objc open var stackView : UIStackView !
1717 @objc @IBInspectable open var spacing : CGFloat = 8
18+ @objc open var durationForAnimations : TimeInterval = 1.45
1819
1920 required public init ? ( coder aDecoder: NSCoder ) {
2021 fatalError ( " init(coder:) has not been implemented " )
@@ -52,17 +53,35 @@ import UIKit
5253 self . setNeedsUpdateConstraints ( ) // Bootstrap auto layout
5354 }
5455
55- //MARK: Stack View
56+ // Scrolls to item at index
5657 @objc public func scrollToItem( index: Int ) {
5758 if stackView. arrangedSubviews. count > 0 {
5859 var view = stackView. arrangedSubviews [ index]
5960
60- UIView . animate ( withDuration: 1.45 , animations: {
61+ UIView . animate ( withDuration: durationForAnimations , animations: {
6162 self . scrollView. setContentOffset ( CGPoint ( x: 0 , y: view. frame. origin. y) , animated: true )
6263 } )
6364 }
6465 }
6566
67+ // Used to scroll till the end of scrollview
68+ @objc public func scrollToBottom( ) {
69+ if stackView. arrangedSubviews. count > 0 {
70+ UIView . animate ( withDuration: durationForAnimations, animations: {
71+ self . scrollView. scrollToBottom ( true )
72+ } )
73+ }
74+ }
75+
76+ // Scrolls to top of scrollable area
77+ @objc public func scrollToTop( ) {
78+ if stackView. arrangedSubviews. count > 0 {
79+ UIView . animate ( withDuration: durationForAnimations, animations: {
80+ self . scrollView. setContentOffset ( CGPoint ( x: 0 , y: 0 ) , animated: true )
81+ } )
82+ }
83+ }
84+
6685 func addItemToStack( ) {
6786 let random = CGFloat ( arc4random_uniform ( 131 ) + 30 ) // between 30-130
6887 let rectangle = UIView ( frame: CGRect ( x: 0 , y: 0 , width: random, height: random) )
@@ -109,7 +128,7 @@ import UIKit
109128 }
110129}
111130
112- // Used to scroll till the end after adding new items to atack view
131+ // Used to scroll till the end of scrollview
113132extension UIScrollView {
114133 func scrollToBottom( _ animated: Bool ) {
115134 if self . contentSize. height < self . bounds. size. height { return }
0 commit comments