網(wǎng)格的用法

//引導

#import "ViewController.h"

#import "MainViewController.h"

@interface ViewController ()

{

? ? NSArray*imgArr;

? ? UIScrollView *scv;

? ? UIPageControl *page;


? ? NSTimer*timer;

? ? intk;

}

@end

@implementation ViewController

- (void)viewDidLoad

{

? ? [super viewDidLoad];


? ? timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(change) userInfo:nil repeats:YES];


? ? imgArr = @[@"1",@"2",@"3",@"4"];


? ? //新特性界面

? ? //滾動視圖

? ? scv = [[UIScrollView alloc]initWithFrame:self.view.frame];

? ? //上顏色

? ? scv.backgroundColor = [UIColor purpleColor];


? ? //設(shè)置滾動范圍

? ? scv.contentSize = CGSizeMake(self.view.frame.size.width *4, self.view.frame.size.height);

? ? //初始化圖片

? ? for(inti=0; i<4; i++)

? ? {


? ? ? ? UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(i*self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];


? ? ? ? //設(shè)置分頁

? ? ? ? scv.pagingEnabled = YES;


? ? ? ? //隱藏水平滾動條

? ? ? ? scv.showsHorizontalScrollIndicator = NO;


? ? ? ? //取消彈簧效果

? ? ? ? scv.bounces=NO;


? ? ? ? //設(shè)置滾動視圖的代理

? ? ? ? scv.delegate=self;


? ? ? ? //設(shè)置圖片

? ? ? ? imgV.image= [UIImageimageNamed:imgArr[i]];


? ? ? ? [scvaddSubview:imgV];

? ? }


? ? [self.view addSubview:scv];

}

-(void)scrollViewDidScroll:(UIScrollView*)scrollView

{

? ? page.currentPage = scv.contentOffset.x/self.view.frame.size.width;

? ? NSLog(@"%lf",scv.contentOffset.x);

}

-(void)change

{

? ? k = scv.contentOffset.x/self.view.frame.size.width;

? ? k++;


? ? scv.contentOffset = CGPointMake(k *self.view.frame.size.width, 0);


? ? if(k>=3)

? ? {

? ? ? ? [timerinvalidate];


? ? ? ? MainViewController * MainVc = [[MainViewController alloc]init];

? ? ? ? //跳轉(zhuǎn)下一個界面

? ? ? ? [self presentViewController:MainVc animated:YES completion:nil];

? ? }


}

@end


//---記得創(chuàng)建DemoCollectionViewCell.h類 ?繼承UICollectionViewCell

#import

@interfaceDemoCollectionViewCell :UICollectionViewCell

@property(nonatomic,strong)UIImageView *img;

@property(nonatomic,strong)UILabel *label;

@end

//----DemoCollectionViewCell.m類

#import "DemoCollectionViewCell.h"

@implementationDemoCollectionViewCell

-(instancetype)initWithFrame:(CGRect)frame

{

? ? if(self= [superinitWithFrame:frame])

? ? {

? ? ? ? self.img= [[UIImageViewalloc]init];

? ? ? ? self.img.backgroundColor= [UIColorredColor];

? ? ? ? [selfaddSubview:self.img];

? ? ? ? self.label= [[UILabelalloc]init];

? ? ? ? self.label.backgroundColor= [UIColorcyanColor];

? ? ? ? [selfaddSubview:self.label];


? ? }


? ? return self;

}

// 布局子控件

-(void)layoutSubviews

{

? ? [super layoutSubviews];

? ? // 布局子控件的位置

? ? self.img.frame=CGRectMake(0,0,90,90);

? ? self.label.frame=CGRectMake(0,90,90,30);

}

@end



//------主頁

#import "OneViewController.h"

#import "DemoCollectionViewCell.h"

@interface OneViewController ()

{

? ? //圖片數(shù)組

? ? NSArray*arr,*arr1,*arr2;

?? //表格

? ? UITableView *tbv;

? ? //創(chuàng)建滾動視圖

? ? UIScrollView*scrollV;


? ? //滾動視圖 圖片框

? ? UIImageView *imgV;

? ? //分頁

? ? UIPageControl *page;


? ? //創(chuàng)建網(wǎng)格

? ? UICollectionView *collV;


? ? UILabel*lab,*lab1,*lab2,*lab3,*lab4,*lab5;

? ? UIView*myView,*myView1,*myView2;

}

@end

@implementationOneViewController

- (void)viewDidLoad

{

? ? [super viewDidLoad];

? ? //標題

? ? UIImageView*imgTou = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,60,30)];

? ? imgTou.image= [UIImageimageNamed:@"標題"];

? ? self.navigationItem.titleView = imgTou;


? ? //我的按鈕

? ? self.navigationItem.rightBarButtonItem=[[UIBarButtonItemalloc]initWithImage:[UIImageimageNamed:@"我的按鈕"] style:UIBarButtonSystemItemDonetarget:selfaction:@selector(rightClick)];



? ? //導航窗口顏色

? ? self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];


? ? //界面背景顏色

? ? self.view.backgroundColor = [UIColor whiteColor];





? ? //初始化表格

? ? tbv = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];


? ? tbv.delegate=self;

? ? tbv.dataSource = self;


? ? [self.view addSubview:tbv];


}

-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView

{

? ? return 1;

}

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section

{

? ? return 4;

}

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath

{

? ? staticNSString*str =@"cell";


? ? UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];

? ? if(cell ==nil)

? ? {

? ? ? ? cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];

? ? }



? ? switch(indexPath.row)

? ? {

? ? ? ? case0:

? ? ? ? {

? ? ? ? ? ? tbv.rowHeight=150;


? ? ? ? ? ? arr = @[@"10",@"11",@"12",@"13"];

? ? ? ? ? ? // 創(chuàng)建滾動試圖

? ? ? ? ? ? scrollV= [[UIScrollViewalloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,150)];

? ? ? ? ? ? scrollV.backgroundColor= [UIColorgreenColor];

? ? ? ? ? ? // 設(shè)置滾動的范圍

? ? ? ? ? ? scrollV.contentSize = CGSizeMake(self.view.frame.size.width * 4, 150);

? ? ? ? ? ? // 設(shè)置分頁

? ? ? ? ? ? scrollV.pagingEnabled=YES;

? ? ? ? ? ? // 設(shè)置是否滾動

? ? ? ? ? ? scrollV.scrollEnabled=YES;

? ? ? ? ? ? // 隱藏水平滾動條

? ? ? ? ? ? scrollV.showsHorizontalScrollIndicator = NO;

? ? ? ? ? ? // 取消彈簧效果

? ? ? ? ? ? scrollV.bounces=NO;


? ? ? ? ? ? // 設(shè)置代理

? ? ? ? ? ? scrollV.delegate=self;


? ? ? ? ? ? // 創(chuàng)建圖片框

? ? ? ? ? ? for(inti=0; i<4; i++) {


? ? ? ? ? ? ? ? imgV= [[UIImageViewalloc]initWithFrame:CGRectMake(self.view.frame.size.width* i,0,self.view.frame.size.width,150)];

? ? ? ? ? ? ? ? imgV.image= [UIImageimageNamed:arr[i]];

? ? ? ? ? ? ? ? [scrollVaddSubview:imgV];


? ? ? ? ? ? }


? ? ? ? ? ? // 設(shè)置位置

? ? ? ? ? ? page= [[UIPageControlalloc]initWithFrame:CGRectMake((self.view.frame.size.width-100)/2-50,130,100,20)];

? ? ? ? ? ? // 設(shè)置頁數(shù)

? ? ? ? ? ? page.numberOfPages=4;

? ? ? ? ? ? // 默認點的原色

? ? ? ? ? ? page.pageIndicatorTintColor = [UIColor grayColor];

? ? ? ? ? ? // 選中頁的點的顏色

? ? ? ? ? ? page.currentPageIndicatorTintColor = [UIColor blueColor];


? ? ? ? ? ? // 添加到cell上

? ? ? ? ? ? [cell.contentViewaddSubview:scrollV];

? ? ? ? ? ? [cell.contentViewaddSubview:page];

//? ? ? ? ? ? [self.view addSubview:page];


? ? ? ? }

? ? ? ? ? ? break;


? ? ? ? case1:

? ? ? ? {

? ? ? ? ? ? tbv.rowHeight=240;

? ? ? ? ? ? //圖片數(shù)組

? ? ? ? ? ? arr1 = @[@"",@"",@"",@"",@"",@"",@"",@""];

? ? ? ? ? ? //文字數(shù)組

? ? ? ? ? ? arr2 = @[@"賬戶",@"轉(zhuǎn)賬",@"信用卡還款",@"借錢",@"繳費",@"招乎",@"熱門活動",@"超級網(wǎng)銀"];


? ? ? ? ? ? // 設(shè)置流水布局

? ? ? ? ? ? UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];

? ? ? ? ? ? // 設(shè)置單元格大小

? ? ? ? ? ? layout.itemSize=CGSizeMake(90,90);

? ? ? ? ? ? // 設(shè)置滾動范圍(上下)

? ? ? ? ? ? layout.scrollDirection = UICollectionViewScrollDirectionVertical;

? ? ? ? ? ? // 設(shè)置最小的行間距

? ? ? ? ? ? layout.minimumLineSpacing=30;


? ? ? ? ? ? // 設(shè)置分區(qū)的間距(上左下右)

? ? ? ? ? ? layout.sectionInset=UIEdgeInsetsMake(5,5,5,5);


? ? ? ? ? ? // 創(chuàng)建網(wǎng)格

? ? ? ? ? ? collV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 240) collectionViewLayout:layout];


? ? ? ? ? ? //網(wǎng)格的背景色

? ? ? ? ? ? collV.backgroundColor= [UIColorlightGrayColor];


? ? ? ? ? ? // 注冊cell

? ? ? ? ? ? [collV registerClass:[DemoCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

? ? ? ? ? ? // 設(shè)置代理和數(shù)據(jù)源

? ? ? ? ? ? collV.delegate=self;

? ? ? ? ? ? collV.dataSource=self;


? ? ? ? ? ? [cell.contentViewaddSubview:collV];


? ? ? ? }

? ? ? ? ? ? break;


? ? ? ? case2:

? ? ? ? {

? ? ? ? ? ? tbv.rowHeight=50;


? ? ? ? ? ? UIImageView*xlbV = [[UIImageViewalloc]initWithFrame:CGRectMake(10,10,100,30)];

? ? ? ? ? ? xlbV.image= [UIImageimageNamed:@"xlb"];

? ? ? ? ? ? [cell.contentViewaddSubview:xlbV];


? ? ? ? ? ? lab5= [[UILabelalloc]initWithFrame:CGRectMake(115,10,self.view.frame.size.width-115,30)];

? ? ? ? ? ? lab5.text = @"? 為什么苦口婆心勸你:不要提前還房貸!";

? ? ? ? ? ? lab5.font= [UIFontsystemFontOfSize:15];

? ? ? ? ? ? [cell.contentViewaddSubview:lab5];



? ? ? ? }

? ? ? break;


? ? ? ? case3:

? ? ? ? {

? ? ? ? ? ? tbv.rowHeight=150;

? ? ? ? ? ? lab= [[UILabelalloc]initWithFrame:CGRectMake(20,0,100,50)];

? ? ? ? ? ? //? ? ? ? ? ? lab.backgroundColor = [UIColor greenColor];

? ? ? ? ? ? lab.text=@"為您推薦";

? ? ? ? ? ? lab.font= [UIFontsystemFontOfSize:20];

? ? ? ? ? ? [cell.contentViewaddSubview:lab];


? ? ? ? ? ? lab1= [[UILabelalloc]initWithFrame:CGRectMake(360,0,100,50)];

? ? ? ? ? ? //? ? ? ? ? ? lab1.backgroundColor = [UIColor greenColor];

? ? ? ? ? ? lab1.text=@"更多";

? ? ? ? ? ? lab1.textColor= [UIColorgrayColor];

? ? ? ? ? ? lab1.font= [UIFontsystemFontOfSize:15];

? ? ? ? ? ? [cell.contentViewaddSubview:lab1];


? ? ? ? ? ? myView= [[UIViewalloc]initWithFrame:CGRectMake(0,50,self.view.frame.size.width/3-4,100)];

? ? ? ? ? ? myView.backgroundColor= [UIColorcolorWithRed:175/255.0green:148/255.0blue:111/255.0alpha:1.0];


? ? ? ? ? ? lab2= [[UILabelalloc]initWithFrame:CGRectMake(10,0,100,33)];

? ? ? ? ? ? lab2.text=@"短期理財";

? ? ? ? ? ? lab2.textAlignment = NSTextAlignmentCenter;

? ? ? ? ? ? lab2.textColor= [UIColorwhiteColor];

? ? ? ? ? ? [myViewaddSubview:lab2];


? ? ? ? ? ? lab3= [[UILabelalloc]initWithFrame:CGRectMake(10,33,100,33)];

? ? ? ? ? ? lab3.text=@"4.75%";

? ? ? ? ? ? lab3.textAlignment = NSTextAlignmentCenter;

? ? ? ? ? ? lab3.textColor= [UIColorwhiteColor];

? ? ? ? ? ? [myViewaddSubview:lab3];


? ? ? ? ? ? lab4= [[UILabelalloc]initWithFrame:CGRectMake(0,66,130,33)];

? ? ? ? ? ? lab4.text=@"業(yè)績比較基準";

? ? ? ? ? ? lab4.font= [UIFontsystemFontOfSize:10];

? ? ? ? ? ? lab4.textAlignment = NSTextAlignmentCenter;

? ? ? ? ? ? lab4.textColor= [UIColorwhiteColor];

? ? ? ? ? ? [myViewaddSubview:lab4];



? ? ? ? ? ? myView1= [[UIViewalloc]initWithFrame:CGRectMake(self.view.frame.size.width/3,50,self.view.frame.size.width/3-4,100)];

? ? ? ? ? ? myView1.backgroundColor= [UIColorcolorWithRed:175/255.0green:148/255.0blue:111/255.0alpha:1.0];

? ? ? ? ? ? UIImageView*image = [[UIImageViewalloc]initWithFrame:CGRectMake(0,3,133,100)];

? ? ? ? ? ? image.image= [UIImageimageNamed:@"16"];

? ? ? ? ? ? [myView1addSubview:image];



? ? ? ? ? ? myView2= [[UIViewalloc]initWithFrame:CGRectMake((self.view.frame.size.width/3)*2+3,50,self.view.frame.size.width/3-4,100)];

? ? ? ? ? ? myView2.backgroundColor= [UIColorcolorWithRed:175/255.0green:148/255.0blue:111/255.0alpha:1.0];

? ? ? ? ? ? UIImageView*image1 = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,150,100)];

? ? ? ? ? ? image1.image= [UIImageimageNamed:@"17"];

? ? ? ? ? ? [myView2addSubview:image1];


? ? ? ? ? ? [cell.contentViewaddSubview:myView];

? ? ? ? ? ? [cell.contentViewaddSubview:myView1];

? ? ? ? ? ? [cell.contentViewaddSubview:myView2];


? ? ? ? }


? ? ? ? default:

? ? ? ? ? ? break;

? }



? ? returncell;

}

// 當試圖滾動的時候,調(diào)用

-(void)scrollViewDidScroll:(UIScrollView*)scrollView{


? ? page.currentPage = scrollV.contentOffset.x/self.view.frame.size.width;


}

#pragma mark- 數(shù)據(jù)源方法

// 數(shù)據(jù)源方法

// 設(shè)置分區(qū)

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView

{

? ? return 1;

}

// 設(shè)置單元格個數(shù)

-(NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section

{

? ? return 8;

}

// 設(shè)置單元格內(nèi)容

-(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath{

? ? // 通過可重用標識符查找cell

? ? DemoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];


? ? //網(wǎng)格cell顏色

? ? cell.backgroundColor= [UIColorwhiteColor];



? ? cell.img.image= [UIImageimageNamed:@"18"];

? ? cell.label.text=arr2[indexPath.row];

? ? cell.label.textAlignment = NSTextAlignmentCenter;


? ? return cell;

}

@end

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容