-
Notifications
You must be signed in to change notification settings - Fork 0
/
MessagePageController.swift
147 lines (113 loc) · 5.26 KB
/
MessagePageController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//
// SocialFeedsViewController.swift
// comblie
//
// Created by Cal on 10/13/15.
// Copyright © 2015 Comblie. All rights reserved.
//
import UIKit
class MessagePageController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
//MARK: Properties
@IBOutlet weak var feedViewContainer: UIView!
@IBOutlet weak var customPageControls: UIStackView!
@IBOutlet weak var networkName: UILabel!
var pageViewController : UIPageViewController!
var pages : NSArray!
var pageQuery : String!
var viewControllers : NSArray!
var dotCollection : [UIView]!
// Set up for parameters that can be passed in
init(page : String)
{
self.pageQuery = page
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
self.pageQuery = ""
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Instantiate the view page controller and add it to the Main View as a child view
// Prepares the login-type views
self.pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("MessagePageViewController") as! UIPageViewController
self.pageViewController.delegate = self
self.pageViewController.dataSource = self
let allVC = createViewController("ConversationTableView")
let facebookVC = createViewController("ConversationTableView")
let twitterVC = createViewController("ConversationTableView")
let instagramVC = createViewController("ConversationTableView")
let tumblrVC = createViewController("ConversationTableView")
let vineVC = createViewController("ConversationTableView")
pages = NSArray(objects: allVC, facebookVC, twitterVC, instagramVC, tumblrVC, vineVC)
viewControllers = NSArray(object: allVC)
self.pageViewController.setViewControllers(viewControllers as? [UIViewController], direction: .Forward, animated: true, completion: nil)
self.addChildViewController(self.pageViewController)
self.pageViewController.view.frame = CGRect(x: 0, y: 0, width: self.feedViewContainer.frame.width, height: self.feedViewContainer.frame.height)
self.feedViewContainer.addSubview(self.pageViewController.view)
//self.view.addSubview(self.pageViewController.view)
self.pageViewController.didMoveToParentViewController(self)
dotCollection = customPageControls.subviews
// Position Page Control
initializeConstraints()
stylePageControls()
}
//MARK: Actions
// Creates and returns view controllers by storyboard ID
func createViewController(identifierID : String) -> UIViewController {
let vc = self.storyboard!.instantiateViewControllerWithIdentifier(identifierID) as UIViewController
return vc
}
// MARK: Page View Controller Data Source
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
let vc = viewController as UIViewController
var index = pages.indexOfObject(vc)
if (index == 0 || index == NSNotFound) {
return nil
}
index--
return pages[index] as! UIViewController
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
let vc = viewController as UIViewController
var index = pages.indexOfObject(vc)
if (index == pages.count-1 || index == NSNotFound) {
return nil
}
index++
return pages[index] as! UIViewController
}
// Triggers after it has landed on a new page after swipe
func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
// Set the current page
networkName.text = pageViewController.viewControllers?.last?.restorationIdentifier
let currentIndex = pages.indexOfObject((pageViewController.viewControllers?.last)!)
for (index, element) in dotCollection.enumerate() {
if index == currentIndex {
element.alpha = CGFloat(1.0)
} else {
element.alpha = CGFloat(0.25)
}
}
}
// MARK : Other
func initializeConstraints() {
let screenHeight = UIScreen.mainScreen().bounds.height
if (screenHeight <= 480) {
print("iPhone4")
} else if screenHeight == 568 {
print("iPhone5")
} else {
print("iPhone6")
}
}
func stylePageControls() {
for dots in dotCollection {
dots.layer.cornerRadius = dots.frame.height/2
dots.alpha = CGFloat(0.25)
}
var currentIndex = pages.indexOfObject(viewControllers[0])
dotCollection[currentIndex].alpha = CGFloat(1.0)
}
}