@@ -15,6 +15,7 @@ import UIKit
15
15
fileprivate var scrollView : UIScrollView !
16
16
@objc open var stackView : UIStackView !
17
17
@objc @IBInspectable open var spacing : CGFloat = 8
18
+ @objc open var durationForAnimations : TimeInterval = 1.45
18
19
19
20
required public init ? ( coder aDecoder: NSCoder ) {
20
21
fatalError ( " init(coder:) has not been implemented " )
@@ -52,17 +53,35 @@ import UIKit
52
53
self . setNeedsUpdateConstraints ( ) // Bootstrap auto layout
53
54
}
54
55
55
- //MARK: Stack View
56
+ // Scrolls to item at index
56
57
@objc public func scrollToItem( index: Int ) {
57
58
if stackView. arrangedSubviews. count > 0 {
58
59
var view = stackView. arrangedSubviews [ index]
59
60
60
- UIView . animate ( withDuration: 1.45 , animations: {
61
+ UIView . animate ( withDuration: durationForAnimations , animations: {
61
62
self . scrollView. setContentOffset ( CGPoint ( x: 0 , y: view. frame. origin. y) , animated: true )
62
63
} )
63
64
}
64
65
}
65
66
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
+
66
85
func addItemToStack( ) {
67
86
let random = CGFloat ( arc4random_uniform ( 131 ) + 30 ) // between 30-130
68
87
let rectangle = UIView ( frame: CGRect ( x: 0 , y: 0 , width: random, height: random) )
@@ -109,7 +128,7 @@ import UIKit
109
128
}
110
129
}
111
130
112
- // Used to scroll till the end after adding new items to atack view
131
+ // Used to scroll till the end of scrollview
113
132
extension UIScrollView {
114
133
func scrollToBottom( _ animated: Bool ) {
115
134
if self . contentSize. height < self . bounds. size. height { return }
0 commit comments