使用UIcollectionView实现图片轮播

使用UIcollectionView实现图片轮播

此处使用collectionView的偏移特性实现图片的轮播功能,其实挺简单

下面实现的完整代码:

DKAdvertisementView.h

#import <UIKit/UIKit.h>

@interface DKAdvertisementView : UIView <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
{
    NSInteger inddexad;
}
@property (nonatomic, strong)NSTimer *timer;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIImageView *allView;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *dataArray;

+ (DKAdvertisementView *)manager;


@end
DKAdvertisementView.m

#define KWidth  [UIScreen mainScreen].bounds.size.width/667
#define KHeight  [UIScreen mainScreen].bounds.size.height/375
#define kMainColor [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1]

#import "DKAdvertisementView.h"
#import "AdvertisementCell.h"

//#define KWidth  [UIScreen mainScreen].bounds.size.width / 667
//#define KHeight  [UIScreen mainScreen].bounds.size.height / 375

@implementation DKAdvertisementView
static DKAdvertisementView *advertisementView;




- (void)awakeFromNib {
    [super awakeFromNib];
    [self.collectionView registerNib:[UINib nibWithNibName:@"AdvertisementCell" bundle:nil] forCellWithReuseIdentifier:@"AdvertisementCell"];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.dataArray = [NSMutableArray arrayWithCapacity:1];
    NSArray *arr = @[@"lauch1",@"lauch2",@"lauch1",@"lauch1",@"lauch1",];
    [self.dataArray addObjectsFromArray:arr];
    self.backgroundColor = kMainColor;
    [self performSelector:@selector(addTimer) withObject:self afterDelay:5];
}



- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = frame;
    }
    return self;
}

- (void)addTimer
{
    inddexad = 0;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)nextPage
{
    if (inddexad <= 4) {
        [self.collectionView setContentOffset:CGPointMake((self.frame.size.width)*inddexad, 0)];
        inddexad ++;
    }
    else if (inddexad > 4)
    {
        [self.timer invalidate];
        [self.collectionView setContentOffset:CGPointMake(0, 0)];
    }
    
}

+ (DKAdvertisementView *)manager {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        advertisementView = [[[NSBundle mainBundle] loadNibNamed:@"DKAdvertisementView" owner:self options:nil] lastObject];
    });
    return advertisementView;
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    AdvertisementCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AdvertisementCell" forIndexPath:indexPath];
    NSString *imageToLoad = [NSString stringWithFormat:@"testImage%ld.png", indexPath.row];
    cell.pictureView.image = [UIImage imageNamed:imageToLoad];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        [self performSelector:@selector(addTimer) withObject:self afterDelay:5];
    }
    NSLog(@"点击了cell");
}


#pragma mark - UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(295 * KWidth, 186 * KHeight);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

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

@end

AdvertisementCell.h

#import <UIKit/UIKit.h>

@interface AdvertisementCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UIImageView *pictureView;

@end
AdvertisementCell.m

#define KWidth  [UIScreen mainScreen].bounds.size.width/667
#define KHeight  [UIScreen mainScreen].bounds.size.height/375
#define kMainColor [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1]

#import "AdvertisementCell.h"

@implementation AdvertisementCell

- (void)awakeFromNib {
    // Initialization code
    self.backgroundColor = kMainColor;
}

@end

ViewController.m中使用

#import "ViewController.h"
#import "DKAdvertisementView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [DKAdvertisementView manager].frame =CGRectMake(331*KWidth, 89, 295*KWidth, 231*KHeight);
    //        sementView.frame = CGRectMake(331*KWidth, 89, 295*KWidth, 231*KHeight);
    [self.view addSubview:[DKAdvertisementView manager]];}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

总结: 按照你正常的创建一个collectionView 添加图片 然后添加调用一下方法就可以了

通过定时器  改变collectionView的偏移量就可以了

- (void)addTimer
{
    inddexad = 0;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)nextPage
{
    if (inddexad <= 4) {
        [self.collectionView setContentOffset:CGPointMake((self.frame.size.width)*inddexad, 0)];
        inddexad ++;
    }
    else if (inddexad > 4)
    {
        [self.timer invalidate];
        [self.collectionView setContentOffset:CGPointMake(0, 0)];
    }
    
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值