This repository was archived by the owner on Jan 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZOECyleView.m
More file actions
173 lines (149 loc) · 5.46 KB
/
ZOECyleView.m
File metadata and controls
173 lines (149 loc) · 5.46 KB
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
//
// ZOECyleViwe.m
// TestCyleImage
//
// Created by zhangwei on 16/4/18.
// Copyright © 2016年 zhangwei. All rights reserved.
//
#import "ZOECyleView.h"
#import "ZoeLayout.h"
#import "ZOECollectionViewCell.h"
@interface ZOECyleView()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic, nonnull,strong)UICollectionView *mainView;
@property(nonatomic, nonnull,strong)ZoeLayout *layout;
@property (nonatomic,strong)ZOECollectionViewCell *cell;
@property(nonatomic,weak)ZOECollectionViewCell *currentCell;
@property(nonatomic,copy)selectItemNum block;
@property(nonatomic,assign)NSInteger stopCellIndex;
@property(nonatomic,assign)BOOL openView;
@property(nonatomic,assign)BOOL fistRun;
@property(nonatomic,strong)NSIndexPath *fistINdexPath;
@end
static NSString *const kCellId = @"CycleCell";
@implementation ZOECyleView
+(instancetype)ZOECyleViewinitWithFrame:(CGRect)frame andMonthsWithTitles:(NSArray *)titlesAry selectItem:(selectItemNum)callBack
{
ZOECyleView *view = [[ZOECyleView alloc]initWithFrame:frame];
view.titlesAry = titlesAry;
view.block = callBack;
return view;
}
-(void)layoutSubviews
{
[super layoutSubviews];
[self addSubview: self.mainView];
self.fistINdexPath = [NSIndexPath indexPathForItem:300 / 2 inSection:0];
[self.mainView scrollToItemAtIndexPath:self.fistINdexPath
atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
}
-(void)restarViewForMiddleView
{
self.fistINdexPath = [NSIndexPath indexPathForItem:300 / 2 inSection:0];
[self.mainView scrollToItemAtIndexPath:self.fistINdexPath
atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 3 * 100;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
self.cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellId forIndexPath:indexPath];
if (indexPath.row == self.fistINdexPath.row) {
[self.cell setCellSelectViewHide: NO];
}else{
[self.cell setCellSelectViewHide: YES];
}
NSDictionary *dict = self.titlesAry[indexPath.row % 3];
if (dict) {
NSString *titleString = dict.allValues[0];
NSString *monthString = dict.allKeys[0];
[self.cell setTitle:titleString];
[self.cell setMonth: monthString];
}
return self.cell;
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView // any offset changes
{
if (self.mainView.visibleCells.count > 0) {
for (ZOECollectionViewCell *view in self.mainView.visibleCells) {
[view close];
}
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self changeCell];
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
[self changeCell];
}
-(void)changeCell
{
for (ZOECollectionViewCell *view in self.mainView.visibleCells) {
CGFloat distance = ABS(view.center.x - self.mainView.frame.size.width * 0.5 - self.mainView.contentOffset.x);
if (distance <= 1) {
self.currentCell = view;
[self.currentCell run];
NSIndexPath *index = [self.mainView indexPathForCell:self.currentCell];
self.stopCellIndex = index.row % 3;
[self doBlock];
}else{
[view close];
}
}
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[collectionView setUserInteractionEnabled:NO];
[self performSelector:@selector(setCollectionViewUseFace:) withObject:@(1) afterDelay:0.5];
[self.mainView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
if (self.stopCellIndex == indexPath.row % 3) {
self.openView = YES;
[self doBlock];
}
}
-(void)doBlock
{
self.block(self.stopCellIndex, self.openView);
self.openView = NO;
}
-(void)setCollectionViewUseFace:(BOOL)object
{
[self.mainView setUserInteractionEnabled:!self.mainView.isUserInteractionEnabled];
}
-(UICollectionView *)mainView
{
if (_mainView == nil) {
_mainView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:self.layout];
[_mainView registerClass:[ZOECollectionViewCell class] forCellWithReuseIdentifier:kCellId];
[_mainView setDataSource: self];
[_mainView setDelegate: self];
[_mainView setShowsHorizontalScrollIndicator:NO];
[_mainView setShowsVerticalScrollIndicator:NO];
[_mainView setUserInteractionEnabled:YES];
[_mainView setBackgroundColor:[UIColor clearColor]];
[_mainView setUserInteractionEnabled: NO];
[self.layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
self.layout.minimumInteritemSpacing = 0;
[self.layout setItemSize:CGSizeMake(_mainView.bounds.size.width / 3, _mainView.bounds.size.width / 3)];
}
return _mainView;
}
-(void)setTitlesAry:(NSArray *)titlesAry
{
_titlesAry = titlesAry;
[_mainView setUserInteractionEnabled: YES];
[_mainView reloadData];
}
-(ZoeLayout *)layout
{
if (_layout == nil) {
_layout = [[ZoeLayout alloc]init];
}
return _layout;
}
@end