UIScrollView滚动视图

1. 基本功能

直接在ViewController.m中编辑
创建一个滚动视图。其中的内容可以进行滚屏查看:

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Heiti SC Light'; color: #008400}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}span.s1 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s2 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures}span.s4 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s5 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s6 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s7 {font-variant-ligatures: no-common-ligatures; color: #272ad8}

    //创建一个滚动视图,可以对视图内内容进行滚屏查看功能
    UIScrollView* sv=[[UIScrollView alloc]init];
    sv.frame=CGRectMake(0, 0, 320, 576);

设置滚屏视图的属性:
画布大小就是你需要滚动显示的范围

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Heiti SC Light'; color: #008400}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400}span.s1 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s2 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures}span.s4 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s5 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s6 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s7 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s8 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s9 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}

    //是否按照整页来滚动视图
    sv.pagingEnabled=YES;
    //是否可以开启滚动效果
    sv.scrollEnabled=YES;
    //设置画布的大小,画布显示在滚动视图内。一般大于frame
    sv.contentSize=CGSizeMake(320*5, 576);
    //是否边缘弹动
    sv.bounces=YES;
    sv.alwaysBounceHorizontal=YES;
    sv.alwaysBounceVertical=YES;
    //是否显示滚动条
    sv.showsHorizontalScrollIndicator=YES;
    sv.showsVerticalScrollIndicator=YES;
    //是否通过点击屏幕让滚动视图响应事件
   // sv.userInteractionEnabled=NO;
    sv.backgroundColor=[UIColor yellowColor];

给滚动视图添加子视图,即要显示的内容:

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s3 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s4 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s5 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s6 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}

    
    for (int i=0; i<3; i++) {
        NSString* strName=[NSString stringWithFormat:@"%d.jpg",i+1];
        
        UIImage* image=[UIImage imageNamed:strName];
        
        UIImageView* iView=[[UIImageView alloc]initWithImage:image];
        
        iView.frame=CGRectMake(320*i,0, 320, 576);
        
        [sv addSubview:iView];

注意:图片必须转成UIImage格式,再转成UIImageView格式,才能成为子视图被添加。

2. 高级功能

你可能感兴趣的:(UIScrollView滚动视图)