ios簡單製作淘搶購活動 滑動效果
阿新 • • 發佈:2019-01-25
最近做專案要做一個類似淘寶淘搶購 的活動效果,就簡單做了一個demo。菜鳥水平~~~~
直接貼程式碼
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
_width = self.view.frame.size.width;
[self.view addSubview:self.scrollerViewTime];
self.scrollerViewTime.delegate = self;
self .scrollerViewTime.contentSize = CGSizeMake(_width * self.arrayTime.count / 5, 0);
for (NSInteger i = 0; i < self.arrayTime.count; i++) {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(_width / 5 * i, 0, _width / 5, 50)];
button.tag = 100 + i;
[button setTitle:self .arrayTime[i] forState:UIControlStateNormal];
button.backgroundColor = [UIColor colorWithWhite:0.2 alpha:1];
[self.scrollerViewTime addSubview:button];
[button addTarget:self action:@selector(clcikTimeButton:) forControlEvents:UIControlEventTouchUpInside];
if (i == 0 || i == 1 || i == self.arrayTime.count - 1 || i == self.arrayTime.count - 2) {
button.enabled = NO;
}
}
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm"];
NSString *time = [formatter stringFromDate:date];
NSLog(@"%@", time);
// time = @"20:00";
for (NSInteger i = 0; i < self.arrayTime.count; i ++) {
NSComparisonResult result = [time compare:@"12:00"];
if (result == NSOrderedAscending || result == NSOrderedSame) {
NSComparisonResult newResult = [time compare:self.arrayTime[i]];
if (newResult == NSOrderedSame || newResult == NSOrderedAscending) {
self.scrollerViewTime.contentOffset = CGPointMake(_width / 5 * (i - 2), 0);
break;
}
} else {
NSComparisonResult newResult = [time compare:self.arrayTime[i]];
if (newResult == NSOrderedSame || newResult == NSOrderedAscending) {
self.scrollerViewTime.contentOffset = CGPointMake(_width / 5 * (i - 2), 0);
self.collectionView.contentOffset = CGPointMake(_width * (i - 2), 0);
break;
}
}
}
UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(_width * 2/ 5 , 0, _width / 5, 50)];
backView.backgroundColor = [UIColor redColor];
backView.alpha = 0.6;
[self.view addSubview:backView];
[self.view addSubview:self.collectionView];
self.collectionView.backgroundColor = [UIColor yellowColor];
self.collectionView.pagingEnabled = YES;
// self.collectionView.pa
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
self.collectionView.bounces = NO;
// self.collectionView.scrollDirection
// Do any additional setup after loading the view.
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ActivCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor colorWithRed:arc4random()%255 /255.0 green:arc4random()%255 /255.0 blue:arc4random()% 255/255.0 alpha:1];
for (UIView *view in cell.contentView.subviews) {
[view removeFromSuperview];
}
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 200, 200, 200)];
label.text = [NSString stringWithFormat:@"第%ld張", indexPath.item];
[cell.contentView addSubview:label];
return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.arrayTime.count - 4;
}
- (UIScrollView *)scrollerViewTime
{
if (!_scrollerViewTime) {
_scrollerViewTime = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
_scrollerViewTime.backgroundColor = [UIColor colorWithWhite:0.2 alpha:1];
}
return _scrollerViewTime;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(@"%lf", scrollView.contentOffset.x);
if ([scrollView isEqual:self.scrollerViewTime]) {
CGFloat scrollX = self.scrollerViewTime.contentOffset.x;
NSInteger page = scrollX * 5 / _width;
CGFloat wide_space = scrollX - page * _width / 5;
if (wide_space > _width / 10 && page < self.arrayTime.count - 1) {
page = page + 1;
}
[UIView animateWithDuration:0.5 animations:^{
self.collectionView.contentOffset = CGPointMake(_width * page, 0);
self.scrollerViewTime.contentOffset = CGPointMake(_width / 5 * page, 0);
}];
}
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
if(!decelerate){
//這裡複製scrollViewDidEndDecelerating裡的程式碼
if ([scrollView isEqual:self.scrollerViewTime]) {
CGFloat scrollX = self.scrollerViewTime.contentOffset.x;
NSInteger page = scrollX * 5 / _width;
CGFloat wide_space = scrollX - page * _width / 5;
if (wide_space > _width / 10 && page < self.arrayTime.count - 1) {
page = page + 1;
}
[UIView animateWithDuration:0.5 animations:^{
self.collectionView.contentOffset = CGPointMake(_width * page, 0);
self.scrollerViewTime.contentOffset = CGPointMake(_width / 5 * page, 0);
}];
}
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if ([scrollView isEqual:self.scrollerViewTime]) {
} else {
CGFloat collectionX = self.collectionView.contentOffset.x;
self.scrollerViewTime.contentOffset= CGPointMake(collectionX / 5, 0);
}
}
- (UICollectionView *)collectionView
{
if (!_collectionView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake(_width, self.view.frame.size.height - 100);
layout.minimumLineSpacing = 0;
layout.minimumInteritemSpacing = 0;
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 60, _width, self.view.frame.size.height - 100) collectionViewLayout:layout];
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"ActivCell"];
}
return _collectionView;
}
- (NSMutableArray *)arrayTime
{
if (!_arrayTime) {
NSArray *array = @[@"", @"", @"10:00",@"12:00",@"14:00",@"16:00",@"18:00",@"19:00",@"20:00",@"21:00",@"22:00",@"23:00",@"08:00",@"09:00",@"10:00",@"12:00",@"14:00",@"16:00",@"18:00",@"19:00",@"20:00",@"21:00",@"22:00",@"23:00",@"08:00",@"09:00", @"", @""];//可以自定義
_arrayTime = [NSMutableArray arrayWithArray:array];
}
return _arrayTime;
}
- (void)clcikTimeButton:(UIButton *)sender
{
CGFloat x = 0;
x = _width / 5 * (sender.tag - 102);
self.scrollerViewTime.contentOffset = CGPointMake(x, 0);
self.collectionView.contentOffset = CGPointMake(x * 5, 0);
}
//有好方法以後在完善吧