Skip to content

Commit 61cd2dd

Browse files
committed
Added scrollToItem(index: Int) method for library.
Updated example for scrollToItem(index: Int) method.
1 parent 928760f commit 61cd2dd

File tree

7 files changed

+50
-29
lines changed

7 files changed

+50
-29
lines changed

Example/Podfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
PODS:
2-
- ScrollableStackView (1.0)
2+
- ScrollableStackView (1.0.1)
33

44
DEPENDENCIES:
55
- ScrollableStackView (from `../`)
66

77
EXTERNAL SOURCES:
88
ScrollableStackView:
9-
:path: ../
9+
:path: "../"
1010

1111
SPEC CHECKSUMS:
12-
ScrollableStackView: 82fe51adf0df6f9ea89f4eff0eed2bac01186da0
12+
ScrollableStackView: 3c5d4ec63d442eb757edf6aba5cbed55197c70c6
1313

1414
PODFILE CHECKSUM: ce2af2a366952d6b9046a4a798b61b3154f78e89
1515

Example/Pods/Local Podspecs/ScrollableStackView.podspec.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/Pods/Manifest.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/Pods/Target Support Files/ScrollableStackView/Info.plist

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/ScrollableStackView/ViewController.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ViewController: UIViewController {
2323
scrollable.stackView.axis = .vertical
2424
view.addSubview(scrollable)
2525

26-
for _ in 1 ..< 11 {
26+
for _ in 1 ..< 23 {
2727
let min:UInt32 = 30
2828
let max:UInt32 = 400
2929
let random = CGFloat(arc4random_uniform(max - min) + min) // between 30-130
@@ -42,6 +42,12 @@ class ViewController: UIViewController {
4242
scrollable.stackView.addArrangedSubview(label)
4343

4444
view.setNeedsUpdateConstraints()
45+
46+
self.perform(#selector(jumpToView), with: nil, afterDelay: 3.0)
47+
}
48+
49+
func jumpToView() {
50+
scrollable.scrollToItem(index: 11)
4551
}
4652

4753
override func updateViewConstraints() {

README.md

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,21 @@ Scrollable UIStackView. Scroll your stack view with a smile up on your face. Wri
99
[![License](https://img.shields.io/cocoapods/l/ScrollableStackView.svg?style=flat)](http://cocoapods.org/pods/ScrollableStackView)
1010
[![Platform](https://img.shields.io/cocoapods/p/ScrollableStackView.svg?style=flat)](http://cocoapods.org/pods/ScrollableStackView)
1111

12-
### TODO
13-
14-
- [ ] Add Storyboard in example project
15-
- [ ] Try NSLayoutAnchor style (requires iOS 9 and later)
16-
- [ ] Add Carthage support
17-
- [ ] Add Swift Package Manager support
18-
19-
## Example
12+
## Installation
2013

21-
Cocoapods
22-
To run the example project, clone the repo, and run `pod install` from the Example directory first.
14+
ScrollableStackView is available through [CocoaPods](http://cocoapods.org). To install
15+
it, simply add the following line to your Podfile:
2316

17+
```ruby
18+
platform :ios, '9.0'
19+
pod "ScrollableStackView"
20+
```
2421
## Usage
2522
### Sample Code (Swift)
2623

2724
```swift
25+
import ScrollableStackView
26+
2827
var scrollable = ScrollableStackView(frame: view.frame)
2928
view.addSubview(scrollable)
3029

@@ -38,6 +37,8 @@ scrollable.stackView.addArrangedSubview(rectangle)
3837
### Sample Code (Objective-C)
3938

4039
```objective-c
40+
@import ScrollableStackView
41+
4142
ScrollableStackView *scrollable = [[ScrollableStackView alloc] initWithFrame:self.view.frame];
4243
scrollable.stackView.distribution = UIStackViewDistributionFillProportionally;
4344
scrollable.stackView.alignment = UIStackViewAlignmentCenter;
@@ -51,20 +52,21 @@ UIView *rectangle = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 55)];
5152
[scrollable.stackView addArrangedSubview:rectangle];
5253
// ...
5354
```
55+
## Example Project
56+
57+
Cocoapods
58+
To run the example project, clone the repo, and run `pod install` from the Example directory first.
5459
5560
<!--## Code Snippets-->
5661
<!--## Wish List -->
5762
<!--## Requirements-->
5863
59-
## Installation
60-
61-
ScrollableStackView is available through [CocoaPods](http://cocoapods.org). To install
62-
it, simply add the following line to your Podfile:
64+
### TODO
6365
64-
```ruby
65-
platform :ios, '9.0'
66-
pod "ScrollableStackView"
67-
```
66+
- [ ] Add Storyboard in example project
67+
- [ ] Try NSLayoutAnchor style (requires iOS 9 and later)
68+
- [ ] Add Carthage support
69+
- [ ] Add Swift Package Manager support
6870
6971
## Author
7072

ScrollableStackView/Classes/ScrollableStackView.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,32 @@ import UIKit
3737
scrollView.addSubview(stackView)
3838
}
3939

40+
//MARK: View life cycle
4041
override public func didMoveToSuperview() {
4142
super.didMoveToSuperview()
4243

4344
setupUI()
4445
}
4546

47+
//MARK: UI
4648
func setupUI() {
4749
self.translatesAutoresizingMaskIntoConstraints = false
4850
clipsToBounds = true
4951

5052
self.setNeedsUpdateConstraints() // Bootstrap auto layout
5153
}
5254

55+
//MARK: Stack View
56+
@objc public func scrollToItem(index: Int) {
57+
if stackView.arrangedSubviews.count > 0 {
58+
var view = stackView.arrangedSubviews[index]
59+
60+
UIView.animate(withDuration: 1.45, animations: {
61+
self.scrollView.setContentOffset(CGPoint(x: 0, y:view.frame.origin.y), animated: true)
62+
})
63+
}
64+
}
65+
5366
func addItemToStack() {
5467
let random = CGFloat(arc4random_uniform(131) + 30) // between 30-130
5568
let rectangle = UIView(frame: CGRect(x: 0, y: 0, width: random, height: random))

0 commit comments

Comments
 (0)