美文网首页
网格的用法

网格的用法

作者: 朕i | 来源:发表于2018-07-29 19:47 被阅读0次

//引导

#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

相关文章

  • 网格的用法

    //引导 #import "ViewController.h" #import "MainViewControll...

  • bootstrap网格系统

    基本用法:网格系统用来布局,其实就是列的组合。Bootstrap框架的网格系统中有四种基本的用法。由于Bootst...

  • Unity对项目性能优化的实现

    1.内容的优化 简化模型 最小化模型网格中的顶点和面的数量,避免复杂的网格。 使用纹理贴图代替复杂的网格 考虑用法...

  • Grid网格布局学习

    Grid网格布局学习 引言 本文不对grid布局由来以及优劣做过多的介绍,仅介绍grid网格布局的用法及其效果显示...

  • Flutter基础控件之GridView

    简单介绍 GridView是一个网格布局的Widget。 用法 1、GridView.count 2、GridVi...

  • Blender学习笔记-(012)插件【EdgeFlow】的简单

    这个插件的用法是帮助网格均匀布线,两种模式进行修改具体操作如下: 第一种是【Flow】,对网格拉扯均匀,对模型有一...

  • 2-2 xyz2grd、surface和nearneighbor

    xyz2grd、surface和nearneighbor三个模块的用法参考:grd文件的生成--网格化 nearn...

  • 资料参考

    sass用法指南--阮一峰ES6入门Flex布局教程Javascript标准参考教程css网格布局

  • A019-布局之GridLayout

    GridLayout 网格布局,是Android4.0之后的API才提供的,算是一个相对新的布局容器,它的用法也很...

  • A021-列表容器之GridView

    概述 GridView是Android的另一个列表容器,用法也跟ListView类似,它的布局是一个网格,一行可以...

网友评论

      本文标题:网格的用法

      本文链接:https://www.haomeiwen.com/subject/budtvftx.html