日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

IOS开发基础之网易新闻UICollectionView的使用第3天

發(fā)布時(shí)間:2023/12/18 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IOS开发基础之网易新闻UICollectionView的使用第3天 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

IOS開發(fā)基礎(chǔ)之網(wǎng)易新聞UICollectionView的使用第3天

由于第3天的UICollectionView 并不實(shí)現(xiàn),我查閱相關(guān)資料,也沒解決,先從本地的plist加載的數(shù)據(jù),不是網(wǎng)絡(luò)的上的數(shù)據(jù)。有一定的參考意義。
父類是UIViewController 通過純代碼添加進(jìn)去的,addSubView 方式添加進(jìn)去的,這跟UITableViewController類似,可以拖控件,指定相應(yīng)的類,但是這種方法并沒有學(xué)會。源碼在我的主頁下面。等回頭這塊知識健全了,再補(bǔ)充回來。

// // ViewController.m // imageRunLoop1 // // Created by 魯軍 on 2021/4/11. //#import "HMImageLoopController.h" #import "YYCell.h" #import "YYNews.h" #define kWidth [UIScreen mainScreen].bounds.size.width #define kHeight [UIScreen mainScreen].bounds.size.height #define MaxSections 100 @interface HMImageLoopController()<UICollectionViewDataSource,UICollectionViewDelegate> @property (nonatomic , strong) UICollectionView *collectionView; @property (nonatomic , strong) UIPageControl *pageControl; @property (nonatomic , strong) NSMutableArray *newses; @property (nonatomic , strong) NSTimer *timer; @property (nonatomic, strong) NSMutableArray *news; @end @implementation HMImageLoopController -(NSArray *)newses{if (_newses == nil) {NSBundle *bundle = [NSBundle mainBundle];NSString *path =[bundle pathForResource:@"resource.plist" ofType:nil];NSArray *array = [NSArray arrayWithContentsOfFile:path];_newses=[NSMutableArray array];for (NSDictionary *dict in array) {[_newses addObject: [YYNews newsWithDict:dict]];}}return _newses; }- (void)viewDidLoad{[super viewDidLoad];[self.view addSubview:self.collectionView];[self.view addSubview:self.pageControl];// 注冊cell[self.collectionView registerClass:[YYCell class] forCellWithReuseIdentifier:@"Cell"];// 添加定時(shí)器 實(shí)現(xiàn)輪播功能呢[self addTimer];}- (void)addTimer {_timer = [NSTimer scheduledTimerWithTimeInterval:8.0 target:self selector:@selector(nextPage) userInfo:NULL repeats:YES];} // 定時(shí)器的內(nèi)容 - (void)nextPage {// 獲取當(dāng)前的 indexPathNSIndexPath *currentIndexPath = [[self.collectionView indexPathsForVisibleItems] lastObject];NSIndexPath *currentIndexPathSet = [NSIndexPath indexPathForItem:currentIndexPath.item inSection:MaxSections / 2];[self.collectionView scrollToItemAtIndexPath:currentIndexPathSet atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];// 設(shè)置下一個(gè)滾動的item的indexPathNSInteger nextItem = currentIndexPathSet.item + 1;NSInteger nextSection = currentIndexPathSet.section;if (nextItem == self.news.count) {// 當(dāng)item等于輪播圖的總個(gè)數(shù)的時(shí)候// item等于0, 分區(qū)加1// 未達(dá)到的時(shí)候永遠(yuǎn)在50分區(qū)中nextItem = 0;nextSection ++;}// NSLog(@"----%ld---%ld", nextItem, nextSection);NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:nextItem inSection:nextSection];[self.collectionView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES]; }#pragma mark ----ScrollView 代理方法 -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {// 添加定時(shí)器[self addTimer]; } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {// 移除定時(shí)器[self.timer invalidate];self.timer = nil; }-(void)scrollViewDidScroll:(UIScrollView *)scrollView {// 滾動時(shí) 動態(tài)設(shè)置 pageControl.currentPage//int page = (int)(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5) % self.news.count;int page = (int)(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5) % 5;self.pageControl.currentPage = page; }#pragma mark ---- 創(chuàng)建集合視圖// 創(chuàng)建集合視圖 - (UICollectionView *)collectionView {if (!_collectionView) {// 創(chuàng)建UICollectionViewFlowLayout約束對象UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];// 設(shè)置item的Size大小flowLayout.itemSize = CGSizeMake(kWidth, kHeight);// 設(shè)置uicollection 的 橫向滑動flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;flowLayout.minimumLineSpacing = 0;_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight) collectionViewLayout:flowLayout];// 設(shè)置代理_collectionView.delegate = self;_collectionView.dataSource = self;// 設(shè)置不展示滑動條_collectionView.showsHorizontalScrollIndicator = NO;// 設(shè)置整頁滑動_collectionView.pagingEnabled = YES;_collectionView.backgroundColor = [UIColor clearColor];// 設(shè)置當(dāng)前collectionView 到哪個(gè)位置(indexPath row 0 section 取中間(50個(gè)))[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:MaxSections / 2] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];}return _collectionView;}- (UIPageControl *)pageControl {if (!_pageControl) {UIPageControl *pageControl = [[UIPageControl alloc] init];pageControl.center = CGPointMake(kWidth / 2, kHeight - 100);pageControl.numberOfPages = _news.count;pageControl.bounds = CGRectMake(0, 0, 150, 40);pageControl.enabled = NO;pageControl.pageIndicatorTintColor = [UIColor blueColor];pageControl.currentPageIndicatorTintColor = [UIColor redColor];[self.view addSubview:pageControl];_pageControl = pageControl;}return _pageControl; } #pragma mark --- 數(shù)據(jù)源 - (NSMutableArray *)news {if (!_news) {NSString *path = [[NSBundle mainBundle] pathForResource:@"resource.plist" ofType:nil];NSArray *arr = [NSArray arrayWithContentsOfFile:path];_news = [NSMutableArray array];for (NSDictionary *dic1 in arr) {[_news addObject:[YYNews newsWithDict:dic1]];}}return _news; } /**[HMHeadline headlines:^(NSArray * _Nonnull array) {self.headlines = array;} errorBlock:^(NSError * _Nonnull err) {NSLog(@"出錯(cuò)了");}];*/#pragma mark --- 實(shí)現(xiàn)collectionView代理方法 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {return MaxSections; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {return self.news.count;}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {static NSString *string = @"Cell";YYCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:string forIndexPath:indexPath];if (!cell) {cell = [[YYCell alloc] init];}cell.news = self.news[indexPath.row];return cell; } @end // // YYCell.m // 圖片輪播(無限循環(huán)) // // Created by yixiang on 14/12/12. // Copyright (c) 2014年 yixiang. All rights reserved. //#import "YYCell.h" #import "YYNews.h"@interface YYCell() @property (weak , nonatomic) UILabel *label; @property (weak , nonatomic) UIImageView *imageView; @end @implementation YYCell-(instancetype)initWithFrame:(CGRect)frame{self =[super initWithFrame:frame];if (self) {UIImageView *img = [[UIImageView alloc] init];[self.contentView addSubview:img];self.imageView = img;UILabel *lab = [[UILabel alloc] init];[self.contentView addSubview:lab];self.label = lab;}return self; }-(void)setNews:(YYNews *)news {_news=news;[self settingData];[self settingFrame]; }#pragma mark 給子控件賦值 -(void) settingData{self.imageView.image = [UIImage imageNamed:_news.icon];self.label.text = _news.title;}#pragma mark 設(shè)置子控件的frame -(void) settingFrame{CGFloat screenWidth = self.contentView.frame.size.width;self.imageView.frame = CGRectMake(0, 0, screenWidth, 200);self.label.frame = CGRectMake(0, 0, screenWidth, 200);self.label.font = [UIFont systemFontOfSize:30];self.label.textAlignment = NSTextAlignmentCenter; }@end #import <UIKit/UIKit.h> @class YYNews;@interface YYCell : UICollectionViewCell @property (nonatomic, strong) YYNews *news; @end #import <Foundation/Foundation.h>@interface YYNews : NSObject @property (nonatomic, copy) NSString *title; @property (nonatomic, copy) NSString *icon;-(id)initWithDict:(NSDictionary *)dict; +(id)newsWithDict : (NSDictionary *) dict; @end #import "YYNews.h"@implementation YYNews-(id)initWithDict:(NSDictionary *)dict{if (self=[super init]) {self.title = dict[@"title"];self.icon = dict[@"icon"];}return self; }+(id)newsWithDict:(NSDictionary *)dict{return [[self alloc] initWithDict:dict]; } @end

總結(jié)

以上是生活随笔為你收集整理的IOS开发基础之网易新闻UICollectionView的使用第3天的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。