/
AFViewController.m
92 lines (70 loc) · 2.95 KB
/
AFViewController.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
//
// AFViewController.m
// UICollectionViewFlowLayoutExample
//
// Created by Ash Furrow on 2013-02-05.
// Copyright (c) 2013 Ash Furrow. All rights reserved.
//
#import "AFViewController.h"
#import "AFCollectionViewFlowLargeLayout.h"
#import "AFCollectionViewFlowSmallLayout.h"
#import "AFCollectionViewCell.h"
@interface AFViewController ()
@property (nonatomic, strong) AFCollectionViewFlowLargeLayout *largeLayout;
@property (nonatomic, strong) AFCollectionViewFlowSmallLayout *smallLayout;
@property (nonatomic, strong) NSArray *images;
@end
static NSString *ItemIdentifier = @"ItemIdentifier";
@implementation AFViewController
-(void)loadView
{
// Important to override this when not using a nib. Otherwise, the collection
// view will be instantiated with a nil layout, crashing the app.
self.smallLayout = [[AFCollectionViewFlowSmallLayout alloc] init];
self.largeLayout = [[AFCollectionViewFlowLargeLayout alloc] init];
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.smallLayout];
[self.collectionView registerClass:[AFCollectionViewCell class] forCellWithReuseIdentifier:ItemIdentifier];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
self.collectionView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"Small", @"Large"]];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.selectedSegmentIndex = 0;
[segmentedControl addTarget:self action:@selector(segmentedControlValueDidChange:) forControlEvents:UIControlEventValueChanged];
self.navigationItem.titleView = segmentedControl;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - User Interface Methods
-(void)segmentedControlValueDidChange:(id)sender
{
if (self.collectionView.collectionViewLayout == self.smallLayout)
{
[self.largeLayout invalidateLayout];
[self.collectionView setCollectionViewLayout:self.largeLayout animated:YES];
}
else
{
[self.smallLayout invalidateLayout];
[self.collectionView setCollectionViewLayout:self.smallLayout animated:YES];
}
}
#pragma mark - UICollectionView DataSource & Delegate methods
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 100;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
AFCollectionViewCell *cell = (AFCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:ItemIdentifier forIndexPath:indexPath];
[cell setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", indexPath.item % 4]]];
return cell;
}
@end