Skip to content

Commit 6e04fe1

Browse files
committed
Added the collection view to SnappingPresentationViewController
1 parent 5150e2f commit 6e04fe1

4 files changed

Lines changed: 63 additions & 12 deletions

File tree

Example/SnappingLayout.xcodeproj/project.pbxproj

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@
401401
ONLY_ACTIVE_ARCH = YES;
402402
SDKROOT = iphoneos;
403403
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
404+
SWIFT_VERSION = 4.2;
404405
};
405406
name = Debug;
406407
};
@@ -446,6 +447,7 @@
446447
MTL_ENABLE_DEBUG_INFO = NO;
447448
SDKROOT = iphoneos;
448449
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
450+
SWIFT_VERSION = 4.2;
449451
VALIDATE_PRODUCT = YES;
450452
};
451453
name = Release;
@@ -461,7 +463,7 @@
461463
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)";
462464
PRODUCT_NAME = "$(TARGET_NAME)";
463465
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
464-
SWIFT_VERSION = 4.0;
466+
SWIFT_VERSION = 4.2;
465467
};
466468
name = Debug;
467469
};
@@ -476,7 +478,7 @@
476478
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)";
477479
PRODUCT_NAME = "$(TARGET_NAME)";
478480
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
479-
SWIFT_VERSION = 4.0;
481+
SWIFT_VERSION = 4.2;
480482
};
481483
name = Release;
482484
};
@@ -497,7 +499,7 @@
497499
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.$(PRODUCT_NAME:rfc1034identifier)";
498500
PRODUCT_NAME = "$(TARGET_NAME)";
499501
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
500-
SWIFT_VERSION = 4.0;
502+
SWIFT_VERSION = 4.2;
501503
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/SnappingLayout_Example.app/SnappingLayout_Example";
502504
};
503505
name = Debug;
@@ -515,7 +517,7 @@
515517
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.$(PRODUCT_NAME:rfc1034identifier)";
516518
PRODUCT_NAME = "$(TARGET_NAME)";
517519
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
518-
SWIFT_VERSION = 4.0;
520+
SWIFT_VERSION = 4.2;
519521
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/SnappingLayout_Example.app/SnappingLayout_Example";
520522
};
521523
name = Release;

Example/SnappingLayout/AppDelegate.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1111
var window: UIWindow?
1212

1313

14-
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
14+
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
1515
window = UIWindow(frame: UIScreen.main.bounds)
16-
window?.rootViewController = ListViewController()
16+
window?.rootViewController = UINavigationController(rootViewController: ListViewController())
1717
window?.makeKeyAndVisible()
1818

1919
return true

Example/SnappingLayout/ListViewController.swift

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,24 @@ import SnappingLayout
88

99
class ListViewController: UIViewController {
1010

11+
// MARK: - Constants
12+
13+
private enum Constants {
14+
static let cellReuseIdentifier = "cell"
15+
}
16+
1117
// MARK: - Views
1218

1319
private lazy var tableView: UITableView = {
1420
let tableView = UITableView()
15-
tableView.register(UITableViewCell.self, forCellReuseIdentifier: self.cellReuseIdentifier)
21+
tableView.register(UITableViewCell.self, forCellReuseIdentifier: Constants.cellReuseIdentifier)
1622
tableView.delegate = self
1723
tableView.dataSource = self
1824
tableView.translatesAutoresizingMaskIntoConstraints = false
1925
return tableView
2026
}()
2127
// MARK: - Properties
22-
23-
private let cellReuseIdentifier = "cell"
28+
2429
private let dataSource: [SnappingLayout.SnapPositionType] = [
2530
.left,
2631
.center,
@@ -45,7 +50,7 @@ extension ListViewController: UITableViewDelegate, UITableViewDataSource {
4550
}
4651

4752
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
48-
let cell = tableView.dequeueReusableCell(withIdentifier: self.cellReuseIdentifier)!
53+
let cell = tableView.dequeueReusableCell(withIdentifier: Constants.cellReuseIdentifier)!
4954

5055
switch dataSource[indexPath.row] {
5156
case .left:
@@ -58,6 +63,14 @@ extension ListViewController: UITableViewDelegate, UITableViewDataSource {
5863

5964
return cell
6065
}
66+
67+
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
68+
tableView.deselectRow(at: indexPath, animated: false)
69+
let selectedSnappingLayoutType = dataSource[indexPath.row]
70+
71+
let viewController = SnappingPresentationViewController(snappingLayoutType: selectedSnappingLayoutType)
72+
navigationController?.pushViewController(viewController, animated: true)
73+
}
6174
}
6275

6376
// MARK: - Private

Example/SnappingLayout/SnappingPresentationViewController.swift

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,49 @@
44
//
55

66
import UIKit
7+
import SnappingLayout
78

89
class SnappingPresentationViewController: UIViewController {
910

1011
// MARK: - Constants
1112

1213
private enum Constants {
13-
14+
static let cellReuseIdentifier = "cell"
15+
static let cellSize = CGSize(width: 30, height: 60)
1416
}
1517

1618
// MARK: - Views
1719

20+
private lazy var collectionView: UICollectionView = {
21+
let layout = SnappingLayout()
22+
layout.snapPosition = self.snappingLayoutType
23+
layout.scrollDirection = .horizontal
24+
layout.itemSize = Constants.cellSize
25+
layout.minimumLineSpacing = 12
26+
27+
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
28+
collectionView.backgroundColor = .clear
29+
collectionView.showsHorizontalScrollIndicator = false
30+
collectionView.decelerationRate = .fast
31+
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: Constants.cellReuseIdentifier)
32+
return collectionView
33+
}()
34+
1835
// MARK: - Properties
1936

37+
private let snappingLayoutType: SnappingLayout.SnapPositionType
38+
39+
// MARK: - Initialization
40+
41+
init(snappingLayoutType: SnappingLayout.SnapPositionType) {
42+
self.snappingLayoutType = snappingLayoutType
43+
super.init(nibName: nil, bundle: nil)
44+
}
45+
46+
required init?(coder _: NSCoder) {
47+
fatalError("init(coder:) has not been implemented")
48+
}
49+
2050
// MARK: - Life-cycle
2151

2252
override func viewDidLoad() {
@@ -31,9 +61,15 @@ class SnappingPresentationViewController: UIViewController {
3161
private extension SnappingPresentationViewController {
3262

3363
func setupViews() {
64+
view.addSubview(collectionView)
3465
}
3566

3667
func setupConstraints() {
68+
let collectionViewConstraints = [
69+
collectionView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
70+
collectionView.leftAnchor.constraint(equalTo: view.leftAnchor),
71+
collectionView.rightAnchor.constraint(equalTo: view.rightAnchor),
72+
]
73+
NSLayoutConstraint.activate(collectionViewConstraints)
3774
}
3875
}
39-

0 commit comments

Comments
 (0)