/
ChoosePersonViewController.swift
188 lines (162 loc) · 8.87 KB
/
ChoosePersonViewController.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//
// ChoosePersonViewController.swift
// SwiftLikedOrNope
//
// Copyright (c) 2014 to present, Richard Burdish @rjburdish
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import UIKit
class ChoosePersonViewController: UIViewController, MDCSwipeToChooseDelegate {
var people:[Person] = []
let ChoosePersonButtonHorizontalPadding:CGFloat = 80.0
let ChoosePersonButtonVerticalPadding:CGFloat = 20.0
var currentPerson:Person!
var frontCardView:ChoosePersonView!
var backCardView:ChoosePersonView!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.people = defaultPeople()
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
self.people = defaultPeople()
// Here you can init your properties
}
override func viewDidLoad(){
super.viewDidLoad()
// Display the first ChoosePersonView in front. Users can swipe to indicate
// whether they like or dislike the person displayed.
self.setMyFrontCardView(self.popPersonViewWithFrame(frontCardViewFrame())!)
self.view.addSubview(self.frontCardView)
// Display the second ChoosePersonView in back. This view controller uses
// the MDCSwipeToChooseDelegate protocol methods to update the front and
// back views after each user swipe.
self.backCardView = self.popPersonViewWithFrame(backCardViewFrame())!
self.view.insertSubview(self.backCardView, belowSubview: self.frontCardView)
// Add buttons to programmatically swipe the view left or right.
// See the `nopeFrontCardView` and `likeFrontCardView` methods.
constructNopeButton()
constructLikedButton()
}
func suportedInterfaceOrientations() -> UIInterfaceOrientationMask{
return UIInterfaceOrientationMask.Portrait
}
// This is called when a user didn't fully swipe left or right.
func viewDidCancelSwipe(view: UIView) -> Void{
print("You couldn't decide on \(self.currentPerson.Name)");
}
// This is called then a user swipes the view fully left or right.
func view(view: UIView, wasChosenWithDirection: MDCSwipeDirection) -> Void{
// MDCSwipeToChooseView shows "NOPE" on swipes to the left,
// and "LIKED" on swipes to the right.
if(wasChosenWithDirection == MDCSwipeDirection.Left){
print("You noped: \(self.currentPerson.Name)")
}
else{
print("You liked: \(self.currentPerson.Name)")
}
// MDCSwipeToChooseView removes the view from the view hierarchy
// after it is swiped (this behavior can be customized via the
// MDCSwipeOptions class). Since the front card view is gone, we
// move the back card to the front, and create a new back card.
if(self.backCardView != nil){
self.setMyFrontCardView(self.backCardView)
}
backCardView = self.popPersonViewWithFrame(self.backCardViewFrame())
//if(true){
// Fade the back card into view.
if(backCardView != nil){
self.backCardView.alpha = 0.0
self.view.insertSubview(self.backCardView, belowSubview: self.frontCardView)
UIView.animateWithDuration(0.5, delay: 0.0, options: .CurveEaseInOut, animations: {
self.backCardView.alpha = 1.0
},completion:nil)
}
}
func setMyFrontCardView(frontCardView:ChoosePersonView) -> Void{
// Keep track of the person currently being chosen.
// Quick and dirty, just for the purposes of this sample app.
self.frontCardView = frontCardView
self.currentPerson = frontCardView.person
}
func defaultPeople() -> [Person]{
// It would be trivial to download these from a web service
// as needed, but for the purposes of this sample app we'll
// simply store them in memory.
return [Person(name: "Finn", image: UIImage(named: "finn"), age: 21, sharedFriends: 3, sharedInterest: 4, photos: 5), Person(name: "Jake", image: UIImage(named: "jake"), age: 21, sharedFriends: 3, sharedInterest: 4, photos: 5), Person(name: "Fiona", image: UIImage(named: "fiona"), age: 21, sharedFriends: 3, sharedInterest: 4, photos: 5), Person(name: "P.Gumball", image: UIImage(named: "prince"), age: 21, sharedFriends: 3, sharedInterest: 4, photos: 5)]
}
func popPersonViewWithFrame(frame:CGRect) -> ChoosePersonView?{
if(self.people.count == 0){
return nil;
}
// UIView+MDCSwipeToChoose and MDCSwipeToChooseView are heavily customizable.
// Each take an "options" argument. Here, we specify the view controller as
// a delegate, and provide a custom callback that moves the back card view
// based on how far the user has panned the front card view.
let options:MDCSwipeToChooseViewOptions = MDCSwipeToChooseViewOptions()
options.delegate = self
//options.threshold = 160.0
options.onPan = { state -> Void in
if(self.backCardView != nil){
let frame:CGRect = self.frontCardViewFrame()
self.backCardView.frame = CGRectMake(frame.origin.x, frame.origin.y-(state.thresholdRatio * 10.0), CGRectGetWidth(frame), CGRectGetHeight(frame))
}
}
// Create a personView with the top person in the people array, then pop
// that person off the stack.
let personView:ChoosePersonView = ChoosePersonView(frame: frame, person: self.people[0], options: options)
self.people.removeAtIndex(0)
return personView
}
func frontCardViewFrame() -> CGRect{
let horizontalPadding:CGFloat = 20.0
let topPadding:CGFloat = 60.0
let bottomPadding:CGFloat = 200.0
return CGRectMake(horizontalPadding,topPadding,CGRectGetWidth(self.view.frame) - (horizontalPadding * 2), CGRectGetHeight(self.view.frame) - bottomPadding)
}
func backCardViewFrame() ->CGRect{
let frontFrame:CGRect = frontCardViewFrame()
return CGRectMake(frontFrame.origin.x, frontFrame.origin.y + 10.0, CGRectGetWidth(frontFrame), CGRectGetHeight(frontFrame))
}
func constructNopeButton() -> Void{
let button:UIButton = UIButton(type: UIButtonType.System)
let image:UIImage = UIImage(named:"nope")!
button.frame = CGRectMake(ChoosePersonButtonHorizontalPadding, CGRectGetMaxY(self.frontCardView.frame) + ChoosePersonButtonVerticalPadding, image.size.width, image.size.height)
button.setImage(image, forState: UIControlState.Normal)
button.tintColor = UIColor(red: 247.0/255.0, green: 91.0/255.0, blue: 37.0/255.0, alpha: 1.0)
button.addTarget(self, action: "nopeFrontCardView", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
}
func constructLikedButton() -> Void{
let button:UIButton = UIButton(type: UIButtonType.System)
let image:UIImage = UIImage(named:"liked")!
button.frame = CGRectMake(CGRectGetMaxX(self.view.frame) - image.size.width - ChoosePersonButtonHorizontalPadding, CGRectGetMaxY(self.frontCardView.frame) + ChoosePersonButtonVerticalPadding, image.size.width, image.size.height)
button.setImage(image, forState:UIControlState.Normal)
button.tintColor = UIColor(red: 29.0/255.0, green: 245.0/255.0, blue: 106.0/255.0, alpha: 1.0)
button.addTarget(self, action: "likeFrontCardView", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
}
func nopeFrontCardView() -> Void{
self.frontCardView.mdc_swipe(MDCSwipeDirection.Left)
}
func likeFrontCardView() -> Void{
self.frontCardView.mdc_swipe(MDCSwipeDirection.Right)
}
}