Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
187 lines (149 loc) · 5.45 KB

File metadata and controls

187 lines (149 loc) · 5.45 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//
// CSScrollview.m
// Coding_iOS
//
// Created by pan Shiyu on 15/7/16.
// Copyright (c) 2015年 Coding. All rights reserved.
//
#import "CSScrollview.h"
@interface CSScrollview () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView* listView;
@property (nonatomic, strong) UIPageControl* pageControl;
@property (nonatomic, strong) NSTimer* timer;
@end
@implementation CSScrollview
- (instancetype)initWithFrame:(CGRect)frame layout:(UICollectionViewFlowLayout*)layout
{
self = [super initWithFrame:frame];
if (self) {
_listView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) collectionViewLayout:layout];
_listView.delegate = self;
_listView.dataSource = self;
_listView.backgroundColor = [UIColor clearColor];
_listView.showsHorizontalScrollIndicator = NO;
_listView.showsVerticalScrollIndicator = NO;
_listView.pagingEnabled = YES;
[_listView registerClass:[CSScrollUnit class] forCellWithReuseIdentifier:@"CSScrollUnit"];
[self addSubview:_listView];
_pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, frame.size.height - 20, frame.size.width, 20)];
_pageControl.currentPageIndicatorTintColor = [UIColor redColor];
_pageControl.pageIndicatorTintColor = [UIColor greenColor];
// [_pageControl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged]; //用户点击UIPageControl的响应函数
[self addSubview:_pageControl];
}
return self;
}
- (void)update:(NSArray*)datas
{
if (datas.count > 0) {
// dispatch_sync(dispatch_get_main_queue(),^{
_items = [NSArray arrayWithArray:datas];
[_listView reloadData];
_pageControl.currentPage = 0;
_pageControl.numberOfPages = _items.count;
CGSize size= [_pageControl sizeForNumberOfPages:_items.count];
_pageControl.size = CGSizeMake(size.width, 20);
_pageControl.bottom = self.height;
_pageControl.centerX = self.width/2;
if (self.autoScrollEnable) {
[self beginAutoScroll];
}
// });
}
}
- (void)setShowPageControl:(BOOL)showPageControl
{
self.pageControl.hidden = !showPageControl;
}
#pragma mark - autoscroll
- (void)beginAutoScroll
{
if (!_autoScrollEnable) {
return;
}
if (_timer) {
[_timer invalidate];
_timer = nil;
}
_timer = [NSTimer scheduledTimerWithTimeInterval:CSScrollTime target:self selector:@selector(didAutoScroll) userInfo:nil repeats:YES];
[_timer fire];
}
- (void)didAutoScroll
{
if (!_items || _items.count == 0) {
return;
}
NSInteger curIndex = _pageControl.currentPage;
NSInteger willIndex = (curIndex + 1) % _pageControl.numberOfPages;
NSIndexPath* path = [NSIndexPath indexPathForRow:willIndex inSection:0];
[_listView scrollToItemAtIndexPath:path atScrollPosition:UICollectionViewScrollPositionRight animated:YES];
_pageControl.currentPage = willIndex;
// [self beginAutoScroll];
}
#pragma mark -
- (void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath
{
if (_tapBlk) {
_tapBlk(_items[indexPath.row]);
}
}
- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section
{
return _items.count;
}
- (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
{
CSScrollUnit* unit = [collectionView dequeueReusableCellWithReuseIdentifier:@"CSScrollUnit" forIndexPath:indexPath];
unit.refIteml = _items[indexPath.row];
return unit;
}
//- (void)scrollViewWillBeginDragging:(UIScrollView*)scrollView
//{
// [self beginAutoScroll];
//}
- (void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView
{
//更新UIPageControl的当前页
CGPoint offset = scrollView.contentOffset;
CGRect bounds = scrollView.frame;
[_pageControl setCurrentPage:offset.x / bounds.size.width];
if (!_autoScrollEnable) {
return;
}
NSArray* visiblelist = self.listView.visibleCells;
if (visiblelist.count == 1) {
UICollectionViewCell* visibleCell = visiblelist.lastObject;
NSIndexPath* curIndexPath = [self.listView indexPathForCell:visibleCell];
_pageControl.currentPage = curIndexPath.row;
}
}
//- (void)pageTurn:(UIPageControl*)sender
//{
// //令UIScrollView做出相应的滑动显示
// CGSize viewSize = _listView.frame.size;
// CGRect rect = CGRectMake(sender.currentPage * viewSize.width, 0, viewSize.width, viewSize.height);
// [_listView scrollRectToVisible:rect animated:YES];
//}
@end
@implementation CSScrollItem
+ (instancetype)itemWithData:(id)data imgUrl:(NSString*)imgUrl
{
CSScrollItem* item = [[CSScrollItem alloc] init];
item.data = data;
item.imgUrl = imgUrl;
return item;
}
@end
@implementation CSScrollUnit {
UIImageView* _imgView;
}
- (void)setRefIteml:(CSScrollItem*)refIteml
{
if (!_imgView) {
_imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
_imgView.backgroundColor = kColorTableBG;
[self.contentView addSubview:_imgView];
}
[_imgView sd_setImageWithURL:[NSURL URLWithString:refIteml.imgUrl]];
}
@end
Morty Proxy This is a proxified and sanitized view of the page, visit original site.