-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCardSliderView.m
207 lines (164 loc) · 6.64 KB
/
CardSliderView.m
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//
// CardSliderView.m
// CardSliderViewProject
//
// Copyright 2012 David Sweetman
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import "CardSliderView.h"
#define kTransitionCardFrame CGRectMake(cardScroller.frame.size.width*2, 0, cardScroller.frame.size.width, cardScroller.frame.size.height);
#define kActiveCardFrame CGRectMake(cardScroller.frame.size.width, 0, cardScroller.frame.size.width, cardScroller.frame.size.height);
#define kFauxCardFrame CGRectMake(0, 0, cardScroller.frame.size.width, cardScroller.frame.size.height);
@interface CardSliderView() {
// The cardScroller is how we swipe from card to card. It is 3x as wide as self.frame.width, so
// we can always scroll 1 card-width left or right. A completed scroll (scrollViewDidEndDecelerating)
//immediately re-centers cardScroller, so it is ready for the next scroll.
UIScrollView *cardScroller;
UIView *transitionCardShell;
UIView *activeCard;
UIView *transitionCard;
BOOL didStartScrollingNext;
BOOL didStartScrollingPrev;
BOOL startedIncompleteNextScroll;
BOOL startedIncompletePreviousScroll;
UIView *incomingCardView;
UIView *currentCardView;
//dimmer is only used to create visual separation betw. background & foreground cards while swiping
UIView *dimmer;
}
@end
@implementation CardSliderView
//@synthesize currentCard;
@synthesize dataSource = _dataSource;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setupCardViews];
}
return self;
}
- (void)awakeFromNib {
[self setupCardViews];
}
- (void)didMoveToWindow {
incomingCardView = [self.dataSource viewForCurrentCardForCardSlider:self];
[activeCard addSubview:incomingCardView];
[cardScroller addSubview:activeCard];
}
- (void)setupCardViews {
self.backgroundColor = [UIColor clearColor];
cardScroller = [[UIScrollView alloc] initWithFrame:self.frame];
cardScroller.contentSize = CGSizeMake(self.frame.size.width*3, self.frame.size.height);
cardScroller.contentOffset = CGPointMake(cardScroller.frame.size.width, 0);
cardScroller.backgroundColor = [UIColor clearColor];
cardScroller.pagingEnabled = YES;
cardScroller.delegate = self;
cardScroller.showsHorizontalScrollIndicator = NO;
cardScroller.showsVerticalScrollIndicator = NO;
[cardScroller setBounces:NO];
activeCard = [[UIScrollView alloc] init];
activeCard.frame = kActiveCardFrame;
transitionCard = [[UIScrollView alloc] initWithFrame:cardScroller.frame];
transitionCardShell = [[UIScrollView alloc] initWithFrame:cardScroller.frame];
if (self.dataSource) {
incomingCardView = [self.dataSource viewForCurrentCardForCardSlider:self];
[activeCard addSubview:incomingCardView];
UIView *white = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
white.backgroundColor = [UIColor whiteColor];
[activeCard addSubview:white];
[cardScroller addSubview:activeCard];
}
dimmer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cardScroller.frame.size.width, cardScroller.frame.size.height)];
dimmer.backgroundColor = [UIColor blackColor];
dimmer.alpha = 0;
[transitionCardShell addSubview:transitionCard];
[transitionCardShell addSubview:dimmer];
[self addSubview:transitionCardShell];
[self addSubview:cardScroller];
}
#pragma mark - ScrollView Methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView.contentOffset.x > scrollView.contentSize.width/3+1) {
// NEW CARD COMING FROM RIGHT <---x
if (!didStartScrollingNext) {
if (startedIncompletePreviousScroll) {
startedIncompletePreviousScroll = NO;
currentCardView = [self.dataSource viewForNextCardForCardSlider:self];
}
if (!startedIncompleteNextScroll) {
startedIncompleteNextScroll = YES;
startedIncompleteNextScroll = YES;
currentCardView = [self.dataSource viewForCurrentCardForCardSlider:self];
incomingCardView = [self.dataSource viewForNextCardForCardSlider:self];
}
[transitionCard addSubview:incomingCardView];
transitionCard.frame = kTransitionCardFrame;
[cardScroller addSubview:transitionCard];
activeCard.frame = kFauxCardFrame;
[activeCard addSubview:currentCardView];
[transitionCardShell addSubview:activeCard];
didStartScrollingNext = YES;
didStartScrollingPrev = NO;
}
} else if (scrollView.contentOffset.x < scrollView.contentSize.width/3-1){
// OLD CARD GOING RIGHT x--->
activeCard.frame = kActiveCardFrame;
if (!didStartScrollingPrev) {
if (startedIncompleteNextScroll) {
startedIncompleteNextScroll = NO;
currentCardView = [self.dataSource viewForPreviousCardForCardSlider:self];
}
if (!startedIncompletePreviousScroll) {
startedIncompletePreviousScroll = YES;
startedIncompletePreviousScroll = YES;
currentCardView = [self.dataSource viewForCurrentCardForCardSlider:self];
incomingCardView = [self.dataSource viewForPreviousCardForCardSlider:self];
}
[transitionCard addSubview:incomingCardView];
transitionCard.frame = kFauxCardFrame;
[transitionCardShell addSubview:transitionCard];
[cardScroller addSubview:activeCard];
didStartScrollingPrev = YES;
didStartScrollingNext = NO;
}
}
//Dim the background card:
CGFloat scrollDistance = (scrollView.contentOffset.x-scrollView.frame.size.width)/1700;
if (scrollDistance < 0) {
scrollDistance = .188 + scrollDistance;
}
[dimmer setAlpha:(scrollDistance)];
[transitionCardShell bringSubviewToFront:dimmer];
}
-(void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
if (scrollView.contentOffset.x == 0) {
//DECREMENTED:
activeCard.frame = kActiveCardFrame;
[activeCard addSubview:incomingCardView];
startedIncompletePreviousScroll = NO;
} else if (scrollView.contentOffset.x == scrollView.frame.size.width) {
//currentCard DID NOT CHANGE
} else if (scrollView.contentOffset.x == scrollView.frame.size.width*2) {
//INCREMENTED:
startedIncompleteNextScroll = NO;
activeCard.frame = kActiveCardFrame;
[activeCard addSubview:incomingCardView];
[cardScroller addSubview:activeCard];
}
scrollView.contentOffset = CGPointMake(scrollView.frame.size.width, 0);
//This is the end of a move, so reset the did-'s:
didStartScrollingPrev = NO;
didStartScrollingNext = NO;
}
@end