一、基本知识

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. linux中软链接打包、计算以及同步

    目录test中存在软连接: 1.打包,参数h(将实际文件进行打包): tar zcvfPh test.tar.gz   test 2.计算大小,参数L(计算的是实际文件的大小): du -sL  te ...

  2. nodejs生成UID(唯一标识符)——node-uuid模块

    unique identifier 惟一标识符        -->> uid 在项目开发中我们常需要给某些数据定义一个唯一标识符,便于寻找,关联. node-uuid模块很好的提供了这个 ...

  3. NHibernate系列文章九:NHibernate对象二级缓存上

    摘要 NHibernate的二级缓存由SessionFactory管理,由所有Session共享. NHibernate缓存读取顺序: 首先从一级缓存中读取,如果一级缓存对象存在,则读取一级缓存对象并 ...

  4. 文件读写方法1.FileInputStream和FileOutputStream

    package fileTest; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundEx ...

  5. Log4Net日志的配置

    <configuration>  <configSections>    <section name="log4net" type="log ...

  6. OpenSSL命令系列

    1.1 ssl命令系列前言 openssl命令的格式是"openssl command command-options args",command部分有很多种命令,这些命令需要依赖 ...

  7. information_schema系列二(列,列权限,事件,存储引擎)

    这个系列的文章主要是为了能够让自己了解MySQL5.7的一些系统表,统一做一下备注和使用,也希望分享出来让大家能够有一点点的受益. 1:COLUMNS 老规矩.查一下这个表,看一下记录,由于这个是看表 ...

  8. 十步图解CSS的position

    CSS的positon,我想做为一个Web制作者来说都有碰到过,但至于对其是否真正的了解呢?那我就不也说了,至少我自己并不非常的了解其内核的运行.今天在Learn CSS Positioning in ...

  9. form 上传 html 代码

    $("#fm").form('submit',{ url: url, onSubmit: function(){ //进行表单验证 $scope.editor.sync(); va ...

  10. removeClass() 方法

    删除元素的class类别用removeClass() 方法,与addClass()方法对应.具体使用如下: 如果要删除 p 标记是 cls0 的类别,可以使用如下的代码: $("p" ...