一、基本知识

1、初始化 UIScrollView
#import "ViewController.h"
#define WIDTH[[UIScreen mainScreen]bounds].size.width
#define HEIGHT[[UIScreen mainScreen]bounds].size.height
@interface ViewController ()<UIScrollViewDelegate>
@end
UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(10, 20, WIDTH-20, HEIGHT-30)];
2、设置属性
UIScrollView *scroll = [[UIScrollViewalloc]initWithFrame:CGRectMake(10, 20, WIDTH-20, HEIGHT-30)];
scroll.backgroundColor = [UIColor greenColor];
    scroll.contentSize = CGSizeMake((WIDTH-20)*10, HEIGHT+30);;//设置滚动范围,如果想要左右滑动,则必须保证此宽大于scroll的frame对应的宽,如果想要上下滑动必须保证此高大于scroll的frame对应的高
    //scroll.scrollEnabled = YES;//是否允许滚动
    scroll.bounces = YES;//是否有弹簧效果
    //scroll.contentOffset =CGPointMake(40, 80);//设置scrollview滚动到某个位置
    NSLog(@"%@",NSStringFromCGPoint(scroll.contentOffset));//获取scrollview当前滚动的位置
                                     
    scroll.pagingEnabled = YES;//是否允许整页滚动,如果想要左右整页滑动,要保证contentsize的宽是scrollview frame宽的整数倍,如果要上下整页滑动,要保证contentsize 的高是frame高的整数倍,当下一页露出范围小于整页的一半时,滚回到当前页,当超出一半时,滚动到下一页
    //scroll.showsHorizontalScrollIndicator = NO;//是否显示水平滚动条
    //scroll.showsVerticalScrollIndicator = NO;//是否显示垂直滚动条
    //scroll.scrollsToTop = YES;//点状态栏时,是否允许scorllView滚动到顶部
    //scroll.zooming = YES;//是否允许缩放
    scroll.zoomScale = 2;
3、 [self.view addSubview:scroll];
4、 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 60, 30)];
    label.backgroundColor = [UIColor redColor];
    [scroll addSubview:label];
   
    for (int i=1; i<4; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i]];
        //    float w = image.size.width;
        //    float h =image.size.height;
        UIImageView *imageview = [[UIImageView alloc]initWithImage:image];
        imageview.frame = CGRectMake(30+i*375, 60, 300, 550);
        [scroll addSubview:imageview];
    }
 
 二、例子
#import "ViewController.h"
@interface ViewController ()<UIScrollViewDelegate>
{
    UISegmentedControl *segment;
    UIScrollView *scrollView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 30, 300, 400)];
    scrollView.backgroundColor = [UIColor whiteColor];
    scrollView.contentSize = CGSizeMake(900, 400);
    scrollView.delegate = self;
    scrollView.pagingEnabled = YES;
    scrollView.scrollEnabled = NO;
    [self.view addSubview:scrollView];
   
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 400)];
    view.backgroundColor = [UIColor grayColor];
    [scrollView addSubview:view];
   
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(300, 0, 300, 400)];
    view1.backgroundColor = [UIColor brownColor];
    [scrollView addSubview:view1];

UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(600, 0, 300, 400)];
    view2.backgroundColor = [UIColor purpleColor];
    [scrollView addSubview:view2];
   
    segment = [[UISegmentedControl alloc] initWithItems:@[@"1",@"2",@"3"]];
    segment.frame = CGRectMake(10, 450,200, 40);
    segment.selectedSegmentIndex = 0;
    [segment addTarget:self action:@selector(segmentChange) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:segment];
   
}
-(void)segmentChange{
    CGPoint p = {segment.selectedSegmentIndex*300,0};
    [scrollView setContentOffset:p animated:YES];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
   // NSLog(@"滑动时调用");
}

-(void)scrollViewDidScrollToTop:(UIScrollView *)scrollView{
    //点击状态栏调用(scrolltotop = yes)
   // NSLog(@"到顶了");
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    //手指放到scrollview上开始滑动时调用
   // NSLog(@"调用");
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    //停止拖拽
   // NSLog(@"--->停止");
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //停止减速--scrollview不动了
  //  NSLog(@"不动了");
   
    CGPoint p = scrollView.contentOffset;
    float w = p.x;

int index = w/scrollView.frame.size.width;
    NSLog(@"当前:%d页",index);
   
    segment.selectedSegmentIndex = index;
   

}
 
 

UIScrollView滚动视图的更多相关文章

  1. 改良UIScrollView滚动视图

    #define HEIGHT  self.view.frame.size.height #define WIDTH    self.view.frame.size.width @interface V ...

  2. UIScrollView 滚动视图—IOS开发

    转自:http://blog.csdn.net/iukey/article/details/7319314 UIScrollView 类负责所有基于 UIKit 的滚动操作. 一.创建 CGRect  ...

  3. OCUI界面设计:滚动视图与分页控件初探

    滚动视图(UIScrollView) 简单介绍 1.UIScrollView滚动视图能够排列并显示超出自身显示范围的内容. 2.UIScrollView内部整合了多种手势来达到丰富的界面展示效果. 3 ...

  4. [转]IOS 学习笔记(8) 滚动视图(UIScrollView)的使用方法

    下面介绍pageControl结合ScrollView实现连续滑动翻页的效果,ScrollView我们在应用开发中经常用到,以g这种翻页效果还是很好看的,如下图所示: 通过这个例子,我们重点学习UIS ...

  5. Swift - 滚动视图(UIScrollView)的用法

    1,当图片尺寸超过屏幕时,使用UIScrollView可以实现滚动条视图,即手指触摸滚动屏幕方便浏览整个页面. 1 2 3 4 5 6 var scrollView=UIScrollView() sc ...

  6. UIScrollView(滚动视图)

    (1)常用属性: 1)@property(nonatomic)CGPointcontentOffset; 这个属性⽤用来表⽰示UIScrollView滚动的位置 2)@property(nonatom ...

  7. iOS学习笔记——滚动视图(scrollView)

    滚动视图:在根视图中添加UIScrollViewDelegate协议,声明一些对象属性 @interface BoViewController : UIViewController<UIScro ...

  8. iOS关于菜单滚动视图实现

    菜单滚动视图也是在项目开发过程中比较常用到的功能,先直接看效果图 实现的效果如下: 当菜单个数的总长度超过一个屏宽度就计算每一个的文字宽度,若没有则只进行一个屏平分,点击菜单项时,滚动的视图位置会随着 ...

  9. swift:创建滚动视图的图片轮播器

    用swift创建图片轮播器和用OC创建的方式是一样的,都主要用到UIScrollView和UIImageview这两个控件,有几张图片,就将滚动视图的内容区域大小设置为每一张图片的大小乘以张数即可.然 ...

随机推荐

  1. google快捷键,通过浏览器本身来查看

    今天玩google浏览器时发现一个不需要访问google就能查看其快捷键的方式,再此记录一下,以备后用. 1:打开Google浏览器 2:打开开发者工具,有三种方法 2-1:按F12快捷键 2-2:按 ...

  2. Excel—“撤销工作表保护密码”的破解并获取原始密码

    您是否遇到过这样的情况:您用Excel编制的报表.表格.程序等,在单元格中设置了公式.函数等,为了防止其他人修改您的设置或者防止您自己无意中修改,您可能会使用Excel的工作表保护功能,但时间久了保护 ...

  3. PHP&MySQL 语法

    PHP操作MySQL数据库 第一步:连接数据库 $dbhost = 'localhost:3306'; //mysql服务器主机地址 $dbuser = 'guest'; //mysql用户名 $db ...

  4. 转:RTC搭建android下三层应用程序访问服务器MsSql-服务器端

    原文:http://www.cnblogs.com/delphi007/p/3346061.html 前几天通过Ro搭建webservice,然后在android下调用,虽然已近成功,但是返回的数据库 ...

  5. 用tcc遇到的一个大坑

    在centos6.5 x86_64服务器上编译安装完tcc, 版本0.9.25(在github上clone的),似乎一切正常 但当用tcc来编译"hello, world"程序时, ...

  6. Python 2.7_Second_try_爬取阳光电影网_获取电影下载地址并写入文件 20161207

    1.昨天文章http://www.cnblogs.com/Mr-Cxy/p/6139705.html 是获取电影网站主菜单 然后获取每个菜单下的电影url  2.今天是对电影url 进行再次解析获取下 ...

  7. Linux搭建smtp服务器+laravel5.2发邮件配置

    /** * 这里主要是想通过自己搭建smtp服务器,配置laravel5.2框架,实现邮箱发邮件功能, * 主要内容是搭建smtp服务器,laravel5.2发邮件顺手提一下 */ /** * 1.l ...

  8. 2.Mybatis入门程序(单表的增删改成)

    这里讲的单表的增删改查,是由mapper代理的增删改查,先来看看步骤: 1.jar包的导入 2.配置全局的配置文件 3.建立接口 4.编写mapper.xml 5.测试 工程结构:这个你们自己可以调整 ...

  9. eclipse4.x 启动之后, "Initializing Java Tooling" 卡住问题解决

    eclipse4.x 启动之后, "Initializing Java Tooling(1%)",其他操作均被阻塞,导致无法正常工作, 解决方案: 删除当前工作目录下的worksp ...

  10. 解决Linux c语言运行时候“段错误 (核心已转储)”问题-采用gdb 解决

    编译没有警告,没有错误,运行就打印 段错误 (核心已转储) 网上找了一下,都是各种问题,都推荐用gdb 调试解决,咱也来趁机学习gdb一下.   gcc+gdb)输入命令行 运行 sudo apt-g ...