美文网首页轮播图汽车公司可能用到的知识点
UICollectionView创建轮播,原理:NSTimer(

UICollectionView创建轮播,原理:NSTimer(

作者: 阳光下的叶子呵 | 来源:发表于2021-03-05 22:58 被阅读0次
截屏2021-03-05 22.52.52.png
@interface LoopCycleView () <UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSTimer *timer;

@end

@implementation LoopCycleView


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        layout.sectionHeadersPinToVisibleBounds = NO;
        layout.sectionFootersPinToVisibleBounds = NO;
        
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:layout];
        
        collectionView.backgroundColor = [UIColor clearColor];
        collectionView.delegate = self;
        collectionView.dataSource = self;
        
        [collectionView registerClass:[LoopCycleCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([LoopCycleCollectionViewCell class])];
        collectionView.showsVerticalScrollIndicator = NO;
        collectionView.showsHorizontalScrollIndicator = NO;
        
        [self addSubview:collectionView];
        self.collectionView = collectionView;
        
//        self.loopArray = @[
//
//            @"rankAliPay_icon",
//            @"rankBatchSelect_icon",
//            @"rankTure_icon",
//            @"redRight_icon",
//            @"hotSales_icon",
//            @"giveTryMember_icon"
//        ];
        
//        NSMutableArray *loopArr = [self.loopArray mutableCopy];
//        for (int i = 0; i < 3; i++) {
//            [loopArr addObject:self.loopArray[i]];
//        }
//        self.loopArray = [loopArr copy];
        
    }
    return self;
}

- (void)setLoopArray:(NSArray *)loopArray {
    _loopArray = loopArray;
    
    if (_loopArray.count >= 3) {
        
        NSMutableArray *loopArr = [_loopArray mutableCopy];
        for (int i = 0; i < 3; i++) {
            [loopArr addObject:_loopArray[i]];
        }
        _loopArray = [loopArr copy];
        
    } else if (_loopArray.count == 2) {
        
        NSMutableArray *loopArr = [_loopArray mutableCopy];
        
        GroupBuyInfoModel *model = [GroupBuyInfoModel new];
        [loopArr addObject:model];
        for (int i = 0; i<3; i++) {
            [loopArr addObject:loopArr[i]];
        }

        _loopArray = [loopArr copy];
        
        
        
        
    } else if (_loopArray.count == 1) {
        
        NSMutableArray *loopArr = [self.loopArray mutableCopy];
        
        GroupBuyInfoModel *model = [GroupBuyInfoModel new];
        [loopArr addObject:model];
        [loopArr addObject:model];
        for (int i = 0; i < 3; i++) {
            [loopArr addObject:loopArr[i]];
        }
        
        _loopArray = [loopArr copy];
        
    }
    
    
    if (_timer) {
        [_timer invalidate];
        _timer = nil;
    }
    
//    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        [obj removeFromSuperview];
//        obj = nil;
//    }];
    
    
    if (_collectionView) {
        self.collectionView.contentOffset =  CGPointMake(0, 0);
    }
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(moveLoopHorizontal) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    
}

- (void)moveLoopHorizontal {
    // 水平,left 向左方向移动 (初始位置) cell数量 >= 3个时
    CGPoint contentOffset = self.collectionView.contentOffset;
    contentOffset.x += [UIScreen mainScreen].bounds.size.width/375*1;
    self.collectionView.contentOffset = contentOffset;
    
    
    // 一个屏幕宽 (显示3个cell)
    if (contentOffset.x >= [UIScreen mainScreen].bounds.size.width/375*(110+15) * (self.loopArray.count-3)) {
        contentOffset.x = 0;
        self.collectionView.contentOffset = contentOffset;
    }
    
}

- (void)dealloc {
    [_timer invalidate];
    _timer = nil;
}

//解决当父View释放时,当前视图因为被Timer强引用而不能释放的问题
- (void)willMoveToSuperview:(UIView *)newSuperview
{
    if (!newSuperview) {
        [_timer invalidate];
        _timer = nil;
    }
}

#pragma mark -- <UICollectionViewDelegate>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.loopArray.count;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    // item 的大小
    return CGSizeMake([UIScreen mainScreen].bounds.size.width/375*110, [UIScreen mainScreen].bounds.size.width/375*36);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return [UIScreen mainScreen].bounds.size.width/375*15;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return [UIScreen mainScreen].bounds.size.width/375*0.1;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    // section 边界
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (nonnull __kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LoopCycleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([LoopCycleCollectionViewCell class]) forIndexPath:indexPath];
    
    cell.backgroundColor = RGBA(0, 0, 0, 0.3);
    cell.layer.masksToBounds = YES;
    cell.layer.cornerRadius = [UIScreen mainScreen].bounds.size.width/375*36/2;
    
//    cell.headImageView.image = [UIImage imageNamed:self.loopArray[indexPath.row]];
    
    cell.userInteractionEnabled = YES;
    cell.headImageView.hidden = NO;
    cell.name.hidden = NO;
    
    if (self.loopArray[indexPath.row].head_img_address.length > 0) {
        
        
        if ([self.loopArray[indexPath.row].head_img_address hasPrefix:@"/"]) {
            [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://47.92.220.225:8080/%@", self.loopArray[indexPath.row].head_img_address]] placeholderImage:[UIImage imageNamed:@"videoLogo_icon"]];
        } else {
            [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", self.loopArray[indexPath.row].head_img_address]] placeholderImage:[UIImage imageNamed:@"videoLogo_icon"]];
        }
        
        if (self.loopArray[indexPath.row].nick_name.length > 0) {
            cell.name.text = [NSString stringWithFormat:@"%@", self.loopArray[indexPath.row].nick_name];
        } else {
            cell.name.text = @" ";
        }
        
    } else {
        // 没头像时 赋值:默认头像
//        cell.headImageView.image = [UIImage imageNamed:@"videoLogo_icon"];
        
        if (self.loopArray[indexPath.row].group_buy_uuid.length > 0) {
            
            cell.headImageView.image = [UIImage imageNamed:@"videoLogo_icon"];
            if (self.loopArray[indexPath.row].nick_name.length > 0) {
                cell.name.text = [NSString stringWithFormat:@"%@", self.loopArray[indexPath.row].nick_name];
            } else {
                cell.name.text = @" ";
            }
            
            
        } else {
            cell.headImageView.hidden = YES;
            cell.name.hidden = YES;
            cell.backgroundColor = [UIColor clearColor];
            cell.userInteractionEnabled = NO;
        }
    }
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.loopCycleCellBlock) {
        self.loopCycleCellBlock(self.loopArray[indexPath.row].group_buy_uuid);
    }
    
//    NSLog(@"点击的是第%ld个。。。", indexPath.row);
}



@end

相关文章

网友评论

    本文标题:UICollectionView创建轮播,原理:NSTimer(

    本文链接:https://www.haomeiwen.com/subject/bnndqltx.html