标签:
初始化项目
.h
1 #import <UIKit/UIKit.h> 2 @class iCocosNews; 3 @interface iCocosNewsCell : UICollectionViewCell 4 @property (nonatomic, strong) iCocosNews *news; 5 @end
.m
1 #import "iCocosNewsCell.h" 2 #import "iCocosNews.h" 3 4 @interface iCocosNewsCell() 5 @property (weak, nonatomic) IBOutlet UILabel *titleLabel; 6 @property (weak, nonatomic) IBOutlet UIImageView *iconView; 7 @end 8 9 @implementation iCocosNewsCell 10 - (void)setNews:(iCocosNews *)news 11 { 12 _news = news; 13 14 self.iconView.image = [UIImage imageNamed:news.icon]; 15 self.titleLabel.text = NSString(@" %@", news.title); 16 } 17 18 @end
.h
1 #import <Foundation/Foundation.h> 2 3 @interface iCocosNews : NSObject 4 @property (copy, nonatomic) NSString *title; 5 @property (copy, nonatomic) NSString *icon; 6 @end
一:无限滚动
1 #import "iCocosNewsViewController.h" 2 #import "iCocosNewsCell.h" 3 #import "MJExtension.h" 4 #import "iCocosNews.h" 5 6 #define iCocosCellIdentifier @"news" 7 #define iCocosMaxSections 100 8 9 @interface iCocosNewsViewController () <UICollectionViewDataSource, UICollectionViewDelegate> 10 @property (weak, nonatomic) IBOutlet UICollectionView *collectionView; 11 @property (nonatomic, strong) NSArray *newses; 12 @end 13 14 @implementation iCocosNewsViewController 15 16 //- (NSMutableArray *)newses 17 //{ 18 // if (_newses == nil) { 19 // self.newses = [NSMutableArray array]; 20 // 21 // for (int i = 0; i<200; i++) { 22 // NSArray *array = [iCocosNews objectArrayWithFilename:@"newses.plist"]; 23 // [self.newses addObjectsFromArray:array]; 24 // } 25 // } 26 // return _newses; 27 //} 28 29 - (NSArray *)newses 30 { 31 if (_newses == nil) { 32 self.newses = [iCocosNews objectArrayWithFilename:@"newses.plist"]; 33 } 34 return _newses; 35 } 36 37 - (void)viewDidLoad 38 { 39 [super viewDidLoad]; 40 41 // 注册cell 42 [self.collectionView registerNib:[UINib nibWithNibName:@"iCocosNewsCell" bundle:nil] forCellWithReuseIdentifier:iCocosCellIdentifier]; 43 44 // [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:500 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES]; 45 46 [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:iCocosMaxSections/2] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES]; 47 } 48 49 #pragma mark - UICollectionViewDataSource 50 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 51 { 52 return self.newses.count; 53 } 54 55 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 56 { 57 return iCocosMaxSections; 58 } 59 60 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 61 { 62 iCocosNewsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:iCocosCellIdentifier forIndexPath:indexPath]; 63 64 cell.news = self.newses[indexPath.item]; 65 66 return cell; 67 } 68 69 70 @end
:需要使用MJExtension实现模型数据的转换
二:自动滚动
1 #import "iCocosNewsViewController.h" 2 #import "iCocosNewsCell.h" 3 #import "MJExtension.h" 4 #import "iCocosNews.h" 5 6 #define iCocosCellIdentifier @"news" 7 #define iCocosMaxSections 100 8 9 @interface iCocosNewsViewController () <UICollectionViewDataSource, UICollectionViewDelegate> 10 @property (weak, nonatomic) IBOutlet UICollectionView *collectionView; 11 @property (nonatomic, weak) IBOutlet UIPageControl *pageContol; 12 @property (nonatomic, strong) NSArray *newses; 13 @property (nonatomic, strong) NSTimer *timer; 14 @end 15 16 @implementation iCocosNewsViewController 17 18 - (NSArray *)newses 19 { 20 if (_newses == nil) { 21 self.newses = [iCocosNews objectArrayWithFilename:@"newses.plist"]; 22 self.pageContol.numberOfPages = self.newses.count; 23 } 24 return _newses; 25 } 26 27 - (void)viewDidLoad 28 { 29 [super viewDidLoad]; 30 31 // 注册cell 32 [self.collectionView registerNib:[UINib nibWithNibName:@"iCocosNewsCell" bundle:nil] forCellWithReuseIdentifier:iCocosCellIdentifier]; 33 34 // 默认显示最中间的那组 35 [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:iCocosMaxSections/2] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO]; 36 37 // 添加定时器 38 [self addTimer]; 39 } 40 41 /** 42 * 添加定时器 43 */ 44 - (void)addTimer 45 { 46 NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES]; 47 [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; 48 self.timer = timer; 49 } 50 51 /** 52 * 移除定时器 53 */ 54 - (void)removeTimer 55 { 56 // 停止定时器 57 [self.timer invalidate]; 58 self.timer = nil; 59 } 60 61 - (NSIndexPath *)resetIndexPath 62 { 63 // 当前正在展示的位置 64 NSIndexPath *currentIndexPath = [[self.collectionView indexPathsForVisibleItems] lastObject]; 65 // 马上显示回最中间那组的数据 66 NSIndexPath *currentIndexPathReset = [NSIndexPath indexPathForItem:currentIndexPath.item inSection:iCocosMaxSections/2]; 67 [self.collectionView scrollToItemAtIndexPath:currentIndexPathReset atScrollPosition:UICollectionViewScrollPositionLeft animated:NO]; 68 return currentIndexPathReset; 69 } 70 71 /** 72 * 下一页 73 */ 74 - (void)nextPage 75 { 76 // 1.马上显示回最中间那组的数据 77 NSIndexPath *currentIndexPathReset = [self resetIndexPath]; 78 79 // 2.计算出下一个需要展示的位置 80 NSInteger nextItem = currentIndexPathReset.item + 1; 81 NSInteger nextSection = currentIndexPathReset.section; 82 if (nextItem == self.newses.count) { 83 nextItem = 0; 84 nextSection++; 85 } 86 NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:nextItem inSection:nextSection]; 87 88 // 3.通过动画滚动到下一个位置 89 [self.collectionView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES]; 90 } 91 92 #pragma mark - UICollectionViewDataSource 93 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 94 { 95 return self.newses.count; 96 } 97 98 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 99 { 100 return iCocosMaxSections; 101 } 102 103 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 104 { 105 iCocosNewsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:iCocosCellIdentifier forIndexPath:indexPath]; 106 107 cell.news = self.newses[indexPath.item]; 108 109 return cell; 110 } 111 112 #pragma mark - UICollectionViewDelegate 113 /** 114 * 当用户即将开始拖拽的时候就调用 115 */ 116 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView 117 { 118 [self removeTimer]; 119 } 120 121 /** 122 * 当用户停止拖拽的时候就调用 123 */ 124 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate 125 { 126 // NSLog(@"scrollViewDidEndDragging--松开"); 127 [self addTimer]; 128 } 129 130 - (void)scrollViewDidScroll:(UIScrollView *)scrollView 131 { 132 int page = (int)(scrollView.contentOffset.x / scrollView.bounds.size.width + 0.5) % self.newses.count; 133 self.pageContol.currentPage = page; 134 } 135 @end
最后给大家介绍一种常用的截图方法
1 // 1.开启图形上下文 2 CGSize imageSize = self.view.bounds.size; 3 UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0); 4 5 // 2.将某个view的所有内容渲染到图形上下文中 6 CGContextRef context = UIGraphicsGetCurrentContext(); 7 [self.view.layer renderInContext:context]; 8 9 // 3.取得图片 10 UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 11 12 CGImageRef subimageRef = CGImageCreateWithImageInRect(image.CGImage, CGRectMake(0, 0, 640, 480)); 13 UIImage *subImage = [UIImage imageWithCGImage:subimageRef]; 14 [UIImagePNGRepresentation(subImage) writeToFile:@"/Users/apple/Desktop/view.png" atomically:YES]; 15 16 // 4.关闭上下文 17 UIGraphicsEndImageContext();
标签:
原文地址:http://www.cnblogs.com/iCocos/p/4540986.html