iOS7 Adding Custom Interactions to Collection Views

####iOS7 Programming Cookbook 第五章 Adding Custom Interactions to Collection Views

#####AppDelegate.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
@implementation AppDelegate

- (UICollectionViewFlowLayout *) flowLayout{
//UICollectionViewFlowLayout类是一个具体的布局对象,组织项目为一个网格可选为每一节页眉和页脚视图。
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//行与行之间的最小间距使用网格中的项目
flowLayout.minimumLineSpacing = 20.0f;
//使用项目之间的最小间距相同的行
flowLayout.minimumInteritemSpacing = 10.0f;
//使用默认大小的cell
flowLayout.itemSize = CGSizeMake(80.0f, 120.0f);
//垂直布局卷轴内容
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
//布置内容
flowLayout.sectionInset = UIEdgeInsetsMake(10.0f, 20.0f, 10.0f, 20.0f);

return flowLayout;
}

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

/* Instantiate the collection view controller with a valid flow layout */
ViewController *viewController =[[ViewController alloc] initWithCollectionViewLayout:[self flowLayout]];

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

self.window.backgroundColor = [UIColor whiteColor];

self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
return YES;

}

#####MyCollectionViewCell.h

1
2
3
4
5
@interface MyCollectionViewCell : UICollectionViewCell

@property (weak, nonatomic) IBOutlet UIImageView *imageViewBackgroundImage;

@end

#####MyCollectionViewCell.m

1
2
3
4
5
6
7
8
9
10
11
@implementation MyCollectionViewCell

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code

}
return self;
}

#####ViewController.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


static NSString *kCollectionViewCellIdentifier = @"Cells";

@implementation ViewController

- (NSArray *) allImages{

static NSArray *AllSectionImages = nil;

if (AllSectionImages == nil){
AllSectionImages = @[
[UIImage imageNamed:@"1"],
[UIImage imageNamed:@"2"],
[UIImage imageNamed:@"3"]
];
}

return AllSectionImages;

}

- (UIImage *) randomImage{
//arc4random_uniform(3) 0x00000002
return [self allImages][arc4random_uniform([self allImages].count)];
}

//初始化动态动画师与指定的collectionView
- (instancetype) initWithCollectionViewLayout:(UICollectionViewLayout *)layout{

self = [super initWithCollectionViewLayout:layout];
if (self != nil){
//检索nib
UINib *nib = [UINib nibWithNibName:NSStringFromClass([MyCollectionViewCell class])
bundle:[NSBundle mainBundle]];
//ib对象包含一个cell视图下指定的标识符
[self.collectionView registerNib:nib forCellWithReuseIdentifier:kCollectionViewCellIdentifier];
}
return self;

}

- (void) handlePinches:(UIPinchGestureRecognizer *)paramSender{

CGSize DefaultLayoutItemSize = CGSizeMake(80.0f, 120.0f);

UICollectionViewFlowLayout *layout =
(UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;

layout.itemSize =
CGSizeMake(DefaultLayoutItemSize.width * paramSender.scale,
DefaultLayoutItemSize.height * paramSender.scale);

[layout invalidateLayout];
}

- (void) viewDidLoad{
[super viewDidLoad];
self.collectionView.backgroundColor = [UIColor whiteColor];


//捏和缩放
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinches:)];
//遍历collectionView手势识别器
for (UIGestureRecognizer *recognizer in self.collectionView.gestureRecognizers){
if ([recognizer isKindOfClass:[pinch class]]){
[recognizer requireGestureRecognizerToFail:pinch];
}
}
[self.collectionView addGestureRecognizer:pinch];

}


- (NSInteger)numberOfSectionsInCollectionView
:(UICollectionView *)collectionView{
/* 3 - 6 sections */
return 3 + arc4random_uniform(4);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section{
/* 之间有10-15个cell */
return 10 + arc4random_uniform(6);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath{
//列出可重用单元与重用标识符
MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewCellIdentifier forIndexPath:indexPath];
//随机返回图片
cell.imageViewBackgroundImage.image = [self randomImage];
//保持长宽比的大小,选择规模内容视图。任何剩余的视图区域的边界是透明的。
cell.imageViewBackgroundImage.contentMode = UIViewContentModeScaleAspectFit;

return cell;

}

@end

Reference

坚持原创技术分享,您的支持将鼓励我继续创作!
0%