网格的用法

//引导

#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];


    //设置滚动范围

    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)];


        //设置分页

        scv.pagingEnabled = YES;


        //隐藏水平滚动条

        scv.showsHorizontalScrollIndicator = NO;


        //取消弹簧效果

        scv.bounces=NO;


        //设置滚动视图的代理

        scv.delegate=self;


        //设置图片

        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];

        //跳转下一个界面

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

    }


}

@end


//---记得创建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 ()

{

    //图片数组

    NSArray*arr,*arr1,*arr2;

   //表格

    UITableView *tbv;

    //创建滚动视图

    UIScrollView*scrollV;


    //滚动视图 图片框

    UIImageView *imgV;

    //分页

    UIPageControl *page;


    //创建网格

    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"];

            // 创建滚动试图

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

            scrollV.backgroundColor= [UIColorgreenColor];

            // 设置滚动的范围

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

            // 设置分页

            scrollV.pagingEnabled=YES;

            // 设置是否滚动

            scrollV.scrollEnabled=YES;

            // 隐藏水平滚动条

            scrollV.showsHorizontalScrollIndicator = NO;

            // 取消弹簧效果

            scrollV.bounces=NO;


            // 设置代理

            scrollV.delegate=self;


            // 创建图片框

            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];


            }


            // 设置位置

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

            // 设置页数

            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;

            //图片数组

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

            //文字数组

            arr2 = @[@"账户",@"转账",@"信用卡还款",@"借钱",@"缴费",@"招乎",@"热门活动",@"超级网银"];


            // 设置流水布局

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

            // 设置单元格大小

            layout.itemSize=CGSizeMake(90,90);

            // 设置滚动范围(上下)

            layout.scrollDirection = UICollectionViewScrollDirectionVertical;

            // 设置最小的行间距

            layout.minimumLineSpacing=30;


            // 设置分区的间距(上左下右)

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


            // 创建网格

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


            //网格的背景色

            collV.backgroundColor= [UIColorlightGrayColor];


            // 注册cell

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

            // 设置代理和数据源

            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=@"业绩比较基准";

            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;

}

// 当试图滚动的时候,调用

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


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


}

#pragma mark- 数据源方法

// 数据源方法

// 设置分区

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView

{

    return 1;

}

// 设置单元格个数

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

{

    return 8;

}

// 设置单元格内容

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

    // 通过可重用标识符查找cell

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


    //网格cell颜色

    cell.backgroundColor= [UIColorwhiteColor];



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

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

    cell.label.textAlignment = NSTextAlignmentCenter;


    return cell;

}

@end

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容