使用UIcollectionView实现图片轮播
2016-03-30 16:56
537 查看
使用UIcollectionView实现图片轮播
此处使用collectionView的偏移特性实现图片的轮播功能,其实挺简单
下面实现的完整代码:
DKAdvertisementView.h
AdvertisementCell.h
ViewController.m中使用
总结: 按照你正常的创建一个collectionView 添加图片 然后添加调用一下方法就可以了
通过定时器 改变collectionView的偏移量就可以了
此处使用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; @endDKAdvertisementView.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; @endAdvertisementCell.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)]; } }
相关文章推荐
- SpannableString与SpannableStringBuilder使用【原创】
- 给UITextView 头部插入视图
- 用JS生成UUID的方法实例
- iOS程序-UITableView分组展示数据 - 纯手写代码
- QuickTest Professionar 快捷键
- ArrayBlockingQueue源码浅析
- UI设计中ID软件书籍排版时目录的制作你会吗?【太原UI设计师】分享
- iOS开发学习笔记——分段视图(UISegmentedControl)
- 应用自动布局让UITableviewCell自适应高度出现下面问题
- IOS开发-UI学习-UIImageView控件
- UIView的alpha、hidden和opaque属性之间的关系和区别
- 基于QtQuick2.0应用程序运行于XP系统的诸多问题
- 判断Android 系统是MIUI 的,魅族的Flyme ,还是原生的?
- ios UITableView性能优化
- UUID生成方法总结
- UIview需要知道的一些事情:setNeedsDisplay、setNeedsLayout
- Android UI设计——EditText
- HBase源码分析之KeyValue
- QSqlQuery的使用
- Android RoboGuice 使用指南