15
15
16
16
@interface PestoSideViewCollectionViewCell : UICollectionViewCell
17
17
18
- @property (nonatomic ) NSString *title;
18
+ @property (nonatomic ) NSString *title;
19
19
20
20
@end
21
21
@@ -24,7 +24,6 @@ @implementation PestoSideViewCollectionViewCell
24
24
- (id )initWithFrame : (CGRect)frame {
25
25
self = [super initWithFrame: frame];
26
26
if (self) {
27
-
28
27
}
29
28
return self;
30
29
}
@@ -50,19 +49,29 @@ - (void)prepareForReuse {
50
49
51
50
@end
52
51
52
+ @class PestoSideContentView;
53
+ @protocol PestoSideContentViewDelegate <NSObject >
54
+
55
+ @optional
56
+ - (void )sideContentView : (PestoSideContentView *)sideContentView
57
+ didSelectItemWithTitle : (NSString *)title ;
58
+
59
+ @end
60
+
53
61
@interface PestoSideContentView : UIView <UICollectionViewDataSource,
54
62
UICollectionViewDelegateFlowLayout>
55
63
56
- @property (nonatomic ) NSArray *titles;
57
- @property (nonatomic ) NSCache *imageCache;
58
- @property (nonatomic ) UICollectionView *collectionView;
64
+ @property (nonatomic ) NSArray *titles;
65
+ @property (nonatomic ) NSCache *imageCache;
66
+ @property (nonatomic ) UICollectionView *collectionView;
67
+ @property (weak ) id <PestoSideContentViewDelegate> delegate;
59
68
60
69
@end
61
70
62
71
@implementation PestoSideContentView
63
72
64
73
- (void )layoutSubviews {
65
- _titles = @[ @" Home" , @" Favorite" , @" Saved" , @" Trending" , @" Settings" ];
74
+ _titles = @[ @" Home" , @" Favorite" , @" Saved" , @" Trending" , @" Settings" ];
66
75
67
76
CGRect avatarRect = CGRectMake (0 , 0 , kPestoSideViewAvatarDim , kPestoSideViewAvatarDim );
68
77
NSString *imageURL = [kPestoSideViewWidthBaseURL stringByAppendingString: @" avatar.jpg" ];
@@ -138,7 +147,7 @@ - (UIImageView *)imageViewWithURL:(NSString *)url {
138
147
dispatch_async (dispatch_get_global_queue (0 , 0 ), ^{
139
148
NSData *imageData = [_imageCache objectForKey: url];
140
149
if (!imageData) {
141
- imageData = [[NSData alloc ] initWithContentsOfURL: [NSURL URLWithString: url]];
150
+ imageData = [[NSData alloc ] initWithContentsOfURL: [NSURL URLWithString: url]];
142
151
[_imageCache setObject: imageData forKey: url];
143
152
}
144
153
if (imageData == nil ) {
@@ -156,7 +165,7 @@ + (Class)layerClass {
156
165
return [MDCShadowLayer class ];
157
166
}
158
167
159
- # pragma mark - UICollectionViewDataSource
168
+ #pragma mark - UICollectionViewDataSource
160
169
161
170
- (NSInteger )collectionView : (UICollectionView *)collectionView
162
171
numberOfItemsInSection : (NSInteger )section {
@@ -174,26 +183,30 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
174
183
return cell;
175
184
}
176
185
186
+ #pragma mark - UICollectionViewDelegateFlowLayout
187
+
177
188
- (void )collectionView : (UICollectionView *)collectionView
178
189
didSelectItemAtIndexPath : (NSIndexPath *)indexPath {
190
+ if (self.delegate &&
191
+ [self .delegate respondsToSelector: @selector (sideContentView:didSelectItemWithTitle: )]) {
192
+ [self .delegate sideContentView: self didSelectItemWithTitle: _titles[[indexPath row ]]];
193
+ }
179
194
}
180
195
181
- # pragma mark - UICollectionViewDelegateFlowLayout
182
-
183
196
- (CGSize)collectionView : (UICollectionView *)collectionView
184
- layout : (UICollectionViewLayout*)collectionViewLayout
197
+ layout : (UICollectionViewLayout *)collectionViewLayout
185
198
sizeForItemAtIndexPath : (NSIndexPath *)indexPath {
186
199
return CGSizeMake (collectionView.bounds .size .width , 40 .f );
187
200
}
188
201
189
202
@end
190
203
191
- @interface PestoSideView () <UIGestureRecognizerDelegate>
204
+ @interface PestoSideView () <UIGestureRecognizerDelegate, PestoSideContentViewDelegate >
192
205
193
- @property (nonatomic ) CGFloat xDelta;
194
- @property (nonatomic ) CGFloat xStart;
195
- @property (nonatomic ) PestoSideContentView *contentView;
196
- @property (nonatomic ) UIButton *dismissButton;
206
+ @property (nonatomic ) CGFloat xDelta;
207
+ @property (nonatomic ) CGFloat xStart;
208
+ @property (nonatomic ) PestoSideContentView *contentView;
209
+ @property (nonatomic ) UIButton *dismissButton;
197
210
198
211
@end
199
212
@@ -206,12 +219,15 @@ - (id)initWithFrame:(CGRect)frame {
206
219
CGRectMake (-kPestoSideViewWidth , 0 , kPestoSideViewWidth , self.frame .size .height );
207
220
_contentView = [[PestoSideContentView alloc ] initWithFrame: contentViewFrame];
208
221
_contentView.backgroundColor = [UIColor whiteColor ];
222
+ _contentView.delegate = self;
209
223
[self addSubview: _contentView];
210
224
211
225
UITapGestureRecognizer *tapRecognizer =
212
226
[[UITapGestureRecognizer alloc ] initWithTarget: self
213
227
action: @selector (hideSideView )];
228
+ tapRecognizer.delegate = self;
214
229
[self addGestureRecognizer: tapRecognizer];
230
+
215
231
UIPanGestureRecognizer *panRecognizer =
216
232
[[UIPanGestureRecognizer alloc ] initWithTarget: self
217
233
action: @selector (panGestureRecognized: )];
@@ -230,31 +246,39 @@ - (void)layoutSubviews {
230
246
231
247
- (void )showSideView {
232
248
[UIView animateWithDuration: kPestoSideViewAnimationDuration
233
- delay: 0
234
- options: UIViewAnimationOptionCurveEaseOut
235
- animations: ^ {
236
- _contentView.transform = [PestoSideView showTransform ];
237
- } completion: ^(BOOL finished) {
238
-
239
- }];
249
+ delay: 0
250
+ options: UIViewAnimationOptionCurveEaseOut
251
+ animations: ^{
252
+ _contentView.transform = [PestoSideView showTransform ];
253
+ }
254
+ completion: ^(BOOL finished){
255
+
256
+ }];
240
257
}
241
258
242
259
- (void )hideSideView {
243
260
[UIView animateWithDuration: kPestoSideViewAnimationDuration
244
- delay: 0
245
- options: UIViewAnimationOptionCurveEaseOut
246
- animations: ^ {
247
- _contentView.transform = [PestoSideView hideTransform ];
248
- } completion: ^(BOOL finished) {
249
- self.hidden = YES ;
250
- }];
261
+ delay: 0
262
+ options: UIViewAnimationOptionCurveEaseOut
263
+ animations: ^{
264
+ _contentView.transform = [PestoSideView hideTransform ];
265
+ }
266
+ completion: ^(BOOL finished) {
267
+ self.hidden = YES ;
268
+ }];
251
269
}
252
270
253
271
- (BOOL )gestureRecognizer : (UIGestureRecognizer *)gestureRecognizer
254
272
shouldRecognizeSimultaneouslyWithGestureRecognizer : (UIGestureRecognizer *)otherGestureRecognizer {
255
273
return YES ;
256
274
}
257
275
276
+ - (BOOL )gestureRecognizer : (UIGestureRecognizer *)gestureRecognizer
277
+ shouldReceiveTouch : (UITouch *)touch {
278
+ return [gestureRecognizer isKindOfClass: [UITapGestureRecognizer class ]] &&
279
+ ![touch.view isDescendantOfView: _contentView];
280
+ }
281
+
258
282
- (void )panGestureRecognized : (UIPanGestureRecognizer *)recognizer {
259
283
CGPoint tappedPoint = [recognizer locationInView: self ];
260
284
CGFloat xCoordinate = tappedPoint.x ;
@@ -288,4 +312,16 @@ + (CGAffineTransform)hideTransform {
288
312
return CGAffineTransformIdentity;
289
313
}
290
314
315
+ #pragma mark - PestoSideContentViewDelegate
316
+
317
+ - (void )sideContentView : (PestoSideContentView *)sideContentView
318
+ didSelectItemWithTitle : (NSString *)title {
319
+ if (self.delegate ) {
320
+ if ([title isEqualToString: @" Settings" ] &&
321
+ [self .delegate respondsToSelector: @selector (sideViewDidSelectSettings: )]) {
322
+ [self .delegate sideViewDidSelectSettings: self ];
323
+ }
324
+ }
325
+ }
326
+
291
327
@end
0 commit comments