UIScrollView (API+循环滚动)

来源:互联网 发布:东方财富 数据接口 编辑:程序博客网 时间:2024/04/29 18:03


详细

-(id)initWithFrameRect:(CGRect)rect ImageArray:(NSArray *)imgArr TitleArray:(NSArray *)titArr delegate:(id<JNScrollerViewDelegate>)delegate;{        if ((self=[super initWithFrame:rect]))    {        [self buildUI:rect ImageArray:imgArr TitleArray:titArr delegate:delegate];    }        return self;}-(void)buildUI:(CGRect)rect ImageArray:(NSArray *)imgArr TitleArray:(NSArray *)titArr delegate:(id<JNScrollerViewDelegate>)delegate;{    self.delegate = delegate;        self.userInteractionEnabled=YES;    titleArray=titArr;    NSMutableArray *tempArray=[NSMutableArray arrayWithArray:imgArr];    [tempArray insertObject:[imgArr objectAtIndex:([imgArr count]-1)] atIndex:0];    [tempArray addObject:[imgArr objectAtIndex:0]];    imageArray=[NSArray arrayWithArray:imgArr];    viewSize=rect;    NSUInteger pageCount=[imageArray count];        //初始化    scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, viewSize.size.width, viewSize.size.height)];    scrollView.delegate = self;    [self addSubview:scrollView];    //是否分页    scrollView.pagingEnabled = YES;        //设置滑动最大范围    scrollView.contentSize = CGSizeMake(viewSize.size.width * pageCount, viewSize.size.height);        //设置水平拖动条是否显示    scrollView.showsHorizontalScrollIndicator = NO;        //设置垂直拖动条是否显示    scrollView.showsVerticalScrollIndicator = NO;        //类似弹簧效果,超出显示内容    scrollView.scrollsToTop = YES;            /**     *  ContentOffset  图片的相对坐标(偏移量),案例是循环滚动,所以第一张作为循环的缓冲区图片,第二张为初始显示的首张图片     *     *  @param viewSize.size.width 第二张的x坐标     *  @param 0                   第二张的y坐标     *  输入结果(iPhone 6 模拟器) :  scrollView.contentOffset =(375,0)     */    //设置初始显示位置,第一张图片x,y坐标    [scrollView setContentOffset:CGPointMake(viewSize.size.width, 0)];            /**     *  contentInset 类似于下拉刷新露出的 "等待区域"     *  UIEdgeInsetsMake(上, 左, 下, 右);  设置对应区域偏移量     *     */    scrollView.contentInset = UIEdgeInsetsMake(0, 20, 0, 0); //超出显示内容回弹时候,显示出偏移20点的左边区域    scrollView.backgroundColor = [UIColor redColor];        //填充图片,支持 urlString 和 imageName    for (int i=0; i<pageCount; i++)    {        NSString *imgURL=[imageArray objectAtIndex:i];        UIImageView *imgView=[[UIImageView alloc] init];        if ([imgURL hasPrefix:@"http://"])        {                     NSURL *newImgUrl = [NSURL URLWithString:imgURL];            //网络图片,异步加载,可以用三方带缓存图片库            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{                                UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:newImgUrl]];                                dispatch_async(dispatch_get_main_queue(), ^{                    [imgView setImage:img];                });            });        }        else if (imgURL)        {                        UIImage *img=[UIImage imageNamed:[imageArray objectAtIndex:i]];            [imgView setImage:img];        }                [imgView setFrame:CGRectMake(viewSize.size.width*i, 0,viewSize.size.width, viewSize.size.height)];        imgView.tag=i;                //添加单击手势        UITapGestureRecognizer *Tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagePressed:)];        [Tap setNumberOfTapsRequired:1];        [Tap setNumberOfTouchesRequired:1];        imgView.userInteractionEnabled=YES;        [imgView addGestureRecognizer:Tap];        [scrollView addSubview:imgView];    }                    //================================================================        //说明文字层    UIView *noteView=[[UIView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height-33,self.bounds.size.width,33)];    float pageControlWidth=(pageCount-2)*10.0f+40.f;    float pagecontrolHeight=20.0f;        //当前页数控制器    pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake((self.frame.size.width-pageControlWidth),6, pageControlWidth, pagecontrolHeight)];    [noteView addSubview:pageControl];    //当前下标    pageControl.currentPage=0;        //总页数    pageControl.numberOfPages=(pageCount-2);        //当前页数圆点的颜色    pageControl.currentPageIndicatorTintColor = [UIColor redColor];        //背景颜色    pageControl.pageIndicatorTintColor = [UIColor blueColor];            noteTitle=[[UILabel alloc] initWithFrame:CGRectMake(5, 6, self.frame.size.width-pageControlWidth-15, 20)];    [noteTitle setText:[titleArray objectAtIndex:0]];    [noteTitle setBackgroundColor:[UIColor clearColor]];    [noteTitle setFont:[UIFont systemFontOfSize:13]];        [self addSubview:noteView];        UIColor *noteViewColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:0.5];        //文字+页数显示控件    if (titleArray.count==0)    {//        noteViewColor = [UIColor clearColor];        pageControl.frame = CGRectMake((self.frame.size.width-CGRectGetWidth(pageControl.frame))*.5f, 6, pageControlWidth, pagecontrolHeight);    }    //无文字层    else    {        [noteView addSubview:noteTitle];    }    [noteView setBackgroundColor:noteViewColor];                }-(void)setAnimaTion:(BOOL)animaTion_tager time:(int)time{    animaTion = animaTion_tager;    if (animaTion) {        timer = [NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(changeimage) userInfo:nil repeats:YES];        [timer fire];    }else{        [timer invalidate];    }}-(void)changeimage{    [UIView animateWithDuration:0.5f                          delay:0                        options:UIViewAnimationOptionCurveEaseInOut                     animations:^{                         scrollView.contentOffset=CGPointMake(scrollView.contentOffset.x+ scrollView.frame.size.width, scrollView.contentOffset.y);                     } completion:^(BOOL finished) {                         [self scrollViewDidScroll:scrollView];                         [self scrollViewDidEndDecelerating:scrollView];                     }];}//当手动拨到第四个时 会同时触发DidEnd,- (void)scrollViewDidScroll:(UIScrollView *)sender{    CGFloat pageWidth = scrollView.frame.size.width;    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;    currentPageIndex=page;    pageControl.currentPage = page-1;    NSLog(@"didScroll%d",currentPageIndex);    //控件pageControl 显示第几个//    pageControl.currentPage=(page-1);//    NSInteger titleIndex=page-1;//    if (titleIndex==[titleArray count]) {//        titleIndex=0;//    }//    if (titleIndex<0) {//        titleIndex=[titleArray count]-1;//    }//    [noteTitle setText:[titleArray objectAtIndex:titleIndex]];    }- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView{    if (currentPageIndex==0) {        //往左 变 3        [_scrollView setContentOffset:CGPointMake(([imageArray count]-2)*viewSize.size.width, 0)];    }    if (currentPageIndex==([imageArray count]-1)) {        //往右 变 1        [_scrollView setContentOffset:CGPointMake(viewSize.size.width, 0)];            }    }



以下转自: http://blog.csdn.net/houseq/article/details/11575817?utm_source=tuicool&utm_medium=referral

--前言:UIScrollView使用非常广,本文研究UIScrollView各属性和方法,明白它们的意义、作用。在后面的一篇文章有整理UIScrollView一些常见用法以及一些效果的实现思路。--参考文章:http://www.cocoachina.com/iphonedev/sdk/2010/1224/2503.html  &&  http://zjqzy03080312.blog.163.com/blog/static/18574280720121121105928687  &&  http://blog.csdn.net/wzzvictory/article/details/9264335  --官方查阅文档  https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIScrollView_Class/Reference/UIScrollView.html --介绍:UIScrollView用于在一个小范围里显示很大的内容的控件。通过用户平滑、手捏手势,在这个小区域里查看不同内容。是UITableView和UITextView的父类。它是视图,但是比较特殊,可以看成把它看成2层的结构。上面是它的frame层,跟一般试图一样,是它的可见区域,下面层是contentView,可以滑动。 ============常用属性============--CGPoint contentOffSet:contentView的偏移值(正常contentOffSet.x/y值都为正数,但有时(bounces = YES)拽出边界了或者contentInset设置了,还是会出现负数,伴随着contentView滑动,总在改变);--CGSize contentSize:contentView的大小;--UIEdgeInsets contentInset:contentView四周的扩展大小,相当改变ContentView的大小,但不是改变contentSize(UIEdgeInsets是个结构体,里面的数值可能是负数,所以扩展的结果可能是contentView被蚕食了一部分。但不管contentView变大了还是变小了,contentOffSet的值还是相比原来大小的偏移);--id<UIScrollerViewDelegate> delegate:它的代理;--BOOL directionalLockEnabled:默认是NO,可以在垂直和水平方向同时运动。当值是YES时,假如一开始是垂直或者是水平运动,那么接下来会锁定另外一个方向的滚动。假如一开始是对角方向滚动,则不会禁止某个方向(测试锁定不了,难道是contentsize跟frame高度或宽度一样时用的)--BOOL bounces:默认是 yes,就是滚动超过边界会反弹有反弹回来的效果。假如是 NO,那么滚动到达边界会立刻停止--UIScrollViewIndicatorStyle  indicatorStyle:设定滚动条的样式;--BOOL alwaysBounceVertical:默认no,控制垂直方向遇到边框是否反弹(但bounces为NO时,它为yes,也不反弹。/if YES and bounces is YES, even if content is smaller than bounds, allow drag vertically,测试不好使);--BOOL alwaysBounceHorizontal:默认no,控制水平方向遇到边框是否反弹(但bounces为NO时,它为yes,也不反弹);--BOOL pagingEnabled:default NO,contentView是否整页翻动;--BOOL scrollEnabled:default YES,contentView是否能滚动;--BOOL showsHorizontalScrollIndicator:default YES,是否显示水平方向的滚动条;--BOOL showsVerticalScrollIndicator:default YES  是否显示垂直方向的滚动条;--UIEdgeInsets scrollIndicatorInsets:滚动条在scrollerView中的位置的扩展;--float  decelerationRate:手指放开后的减速率(测试发现设定这个值没用);===========================================交互相关============--Scrolling with no scroll bars is a bit complex. on touch down, we don't know if the user will want to scroll or track a subview like a control. on touch down, we start a timer and also look at any movement. if the time elapses without sufficient change in position, we start sending events to  the hit view in the content subview. if the user then drags far enough, we switch back to dragging and cancel any tracking in the subview.  the methods below are called by the scroll view and give subclasses override points to add in custom behavior.  you can remove the delay in delivery of touchesBegan:withEvent: to subviews by setting delaysContentTouches to NO.--UIScrollView的事件响应顺序跟一般的不同,一般的见参考文档, UIScrollView的工作原理,当手指touch的时候,UIScrollView会拦截Event,会等待一段时间,在这段时间内,如果没有手指没有移动,当时间结束时,UIScrollView会发送tracking events到子视图上。在时间结束前,手指发生了移动,那么UIScrollView就会进行移动,从而取笑发送tracking顺序说明:当手指touch的时候,如果scrollView上面有可交互的视图,track,->或滑动或点击   --BOOL tracking:(readonly)returns YES if user has touched. may not yet have started dragging,即用户按上了,不管滑没滑 --BOOL dragging:(readonly)returns YES if user has started scrolling. this may require some time and or distance to move to initiate dragging,用户在手指在scrolll时,松开了即为no了 --BOOL decelerating:(readonly) returns YES if user isn't dragging (touch up) but scroll view is still moving  --BOOL delaysContentTouches:就是手指点击到类的时候,如果它为yes,则按之前讲的处理,如果为no,并且点在了“可交互的视图”,立马调用touchesShouldBegin --BOOL canCancelContentTouches:是if touches have already been delivered to a subview of the scroll view之后发生的事,如果no,即使touch move,也不scroll了,反之如果是yes,tracking后,手指移动,会调用touchesShouldCancelInContentView方法;--备注:注意顺序,所以当delaysContentTouches为no,canCancelContentTouches为no的时候,touchesShouldBegin方法的返回值不同,滑动在“可交互的视图”的效果也不同,一个滑的动,一个滑不动(注意是touch move了之后的事情)   ============相关方法============ // override points for subclasses to control delivery of touch events to subviews of the scroll view // called before touches are delivered to a subview of the scroll view. if it returns NO the touches will not be delivered to the subview - (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view;// default returns YES--调用地方:方法在UIScrollView的子类中被重写,被调用,--调用时候:如果delaysContentTouches为yes,则手指点击的时候,会捕获到Event,等待一段时间,在这段时间内,如果没有手指没有移动,并且手指点击在了一个“可交互的视图”则调用touchesShouldBegin,否则类scroll。如果delaysContentTouches为no,只要手指点击了,并且点在一个“可交互的视图”,立刻调用touchesShouldBegin方法。--备注:当点击子类中的“可交互的视图”上时,如果返回值为no,点击事件不会传递给子视图了。(例如上面加了一个button,如果它返回no,点击button,没有效果)。yes反之; // called before scrolling begins if touches have already been delivered to a subview of the scroll view. if it returns NO the touches will continue to be delivered to the subview and scrolling will not occur// not called if canCancelContentTouches is NO. default returns YES if view isn't a UIControl- (BOOL)touchesShouldCancelInContentView:(UIView *)view; --调用时候,上面已讲, --返回值说明:yes,发生滚动,touch不在传递给子视图,no,不滚动,touch传递给子视图========================总结: 用户touch--》tracking真--》如果点击一个可交互视图上,发生后面的事,如果没有,一般情况--》步骤1:根据delaysContentTouches的值,判断什么时候步骤2(立刻还是判断一定条件);步骤2:调用touchesShouldBegin方法,如果返回值为yes,touch事件传给子视图;如果为no,touch事件不传给子视图,进入状态n。(跳转) 步骤3:touch move的,如果canCancelContentTouches为no,不调用touchesShouldCancelInContentView方法,类也不scroll,进入状态m,如果canCancelContentTouches为yes,调用touchesShouldCancelInContentView,根据touchesShouldCancelInContentView方法的返回值,如果是yes,进入状态s。如果是no,进入状态m。   状态n:tracking为真,如果touch move的就scroll,touch松开,进入状态e 状态s:touch move中,类scroll,如果touch松开,进入状态e 状态m:touch move中,类不scroll,如果touch松开,进入状态e 状态e:结束  ===========================================缩放相关============  --float minimumZoomScale:缩放的最小比例; --float maximumZoomScale:缩放的最大比例; --float zoomScale:缩放的比例(在minimumZoomScale和maximumZoomScale之间变化);如果设置的话,写在 [self.view addSubview:_scrollView];之后才能好使   --BOOL bouncesZoom :控制缩放到边界的时是否会反弹; --BOOL zooming(readonly):判断控件的大小是否正在缩放; --BOOL zoomBouncing(readonly):判断控件是否缩放到了最大/最小;  --备注:如果scollView设置了缩放,但又设置contentszie等,刚开始没缩放的时候能scroll,但一但缩放过,就只能缩放,不能scroll了   // When the user taps the status bar, the scroll view beneath the touch which is closest to the status bar will be scrolled to top, but only if its `scrollsToTop` property is YES, its delegate does not return NO from `shouldScrollViewScrollToTop`, and it is not already at the top.// On iPhone, we execute this gesture only if there's one on-screen scroll view with `scrollsToTop` == YES. If more than one is found, none will be scrolled.     --BOOL scrollsToTop:设置单击状态栏控件是否滚动到顶部


以下转自:http://www.jianshu.com/p/bcaf5cdfaa7e

iOS开发学习笔记-UIScrollView的用法

字数2677 阅读5197 评论5 

UIScrollView是用来在屏幕上显示那些在有限区域内放不下的内容。例如,在手机屏幕上显示内容丰富的网页或者很大的图片。在这种情况下,需要用户对屏幕内容进行拖动或缩放来查看屏幕或窗口区域外的内容。

所以,ScrollView应该首先有一个窗口,用来显示内容,其次,还要有内容本身。这里的这个显示窗口就是UIScrollView,这个窗口可以是整个手机屏幕,也可以只是手机屏幕的一部分区域(屏幕其他部分可以显示些别的东西)。内容则是你需要查看的图片或者网页等信息视图。通常,其大小会超过这个屏幕,正因为如此,我们才要使用UIScrollView来查看。

使用方法

建立scrollView

先来看如何使用UIScrollView在小窗口中显示一副大图。

UIScrollView-001.jpg
UIScrollView-001.jpg

假定ViewConroller已经设置了属性@property UIScrollView * scrollView;

- (void)viewDidLoad{[super viewDidLoad];//1. 建立UIScrollView窗口,我们只打算用手机的上半屏显示图像,(这一步也可以在storyboard里完成)self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320 , 300)];//2.建立内容视图UIImageView * view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tesla.jpg"]];//3.将内容视图作为scrollView的子视图[self.scrollView addSubview: view];//4.当然了,还得把scrollView添加到视图结构中[self.view addSubview: self.scrollView];}

运行一下看看,发现窗口正常显示了图片的左上角。但是根本拖不动,看不了图片的其他部分。这是怎么回事呢?因为我们没有为scrollView设置要显示的内容大小。而scrollView的contenSize大小默认是0。

内容展示:contentSize contentOffset contentInset

contentSize描述了有多大范围的内容需要使用scrollView的窗口来显示,其默认值为CGSizeZero,也就是一个宽和高都是0的范围。

当contentSize小于当前scrollView的大小时,意味着用户要显示的内容在窗口范围内是可以全部显示的,这个时候,通常内容视图是拖不动的(有内容没有显示出来才要拖嘛,都显示得出来)。之所以说是“通常”,是因为通过某些设置,还是可以拖得动的,后边回弹机制里会解释。所以要让视图可以拖动,我们得设置一个contentSize。在前面这个简单的这个例子里,我们当然就设置成imageView的大小就好了啊。所以在第2步之后添加一句:

self.scrollView.contentSize = view.bounds.size;

这样,就可以显示图片的全部内容。
但是如果你只想在窗口显示图片的的一部分,也是可以的,就把contentSize设置得小一点就好了。除了contentSize,还有contentOffsetcontentInset也可以结合起来使用。需要注意的是,contentSize的范围是以scrollView的位置为基准的。所以,如果内容视图的frame.origin不是(0,0),则需要仔细计算内容视图能被显示的范围。

  • contentOffset: 描述了内容视图相对于scrollView窗口的位置(当然是向上向左的偏移量咯)。默认值是CGPointZero,也就是(0,0)。当视图被拖动时,系统会不断修改该值。也可以通过setContentOffset:animated:方法让图片到达某个指定的位置。

  • scrollRectToVisible:animated:setContentOffset:animated:类似,只不过是将scrollView坐标系内的一块指定区域移到scrollView的窗口中,如果这部分已经存在于窗口中,则什么也不做。

  • contentInset: 表示scrollView的内边距,也就是内容视图边缘和scrollView的边缘的留空距离,默认值是UIEdgeInsetsZero,也就是没间距。这个属性用的不多,通常在需要刷新内容时才用得到。

好了,图片现在可以拖动了。为了更清楚的进行描述,我们换一张图片。大小为550*350的网格。

我们发现,当将图片拖到边缘的时候,图片还是可以继续被拖动的,会显示contenSize之外的内容。

  • 如果contentSize设置为图片的大小,拖动到边缘后仍然可以继续拖动,视图显示出一定的弹性,显示出空白,一旦松手,内容视图会回弹,空白不会显示在scrollView窗口中。
  • 如果contentSize设置得比图片的大小还小,拖动到contentSize指定的大小后仍然可以继续拖动,显示内容视图的更多部分,但一旦松手,内容视图也会回弹,超过contentSize的那部分不会显示在scrollView窗口中。
  • 如果contentSize设置得比图片的大小更大,拖动到图片的边缘后仍然可以继续拖动,显示空白,直到超过contentSize的范围,才会产生弹性,松手后,视图回弹,静止状态下我们可以看到scrollView可以显示出contentSize范围内图片范围外的空白。

也就是说scrollView窗口在手指释放的时候后的静止状态下,只会显示contentSize范围内的内容。

UIScrollView-002.jpg
UIScrollView-002.jpg

scrollView的这种回弹机制,是可以设置的,相关的属性为:bouncesalwaysBounceHorizontalalwaysBounceVerticaldecelerationRate

在拖动的过程中,我们还发现水平方向和垂直方向还显示出状态条,状态条的显示也是可以设置的。相关属性为:indicatorStyle showsHorizontalScrollIndicator showsVerticalScrollIndicator scrollIndicatorInsets flashScrollIndicators

UIScrollView-003.jpg
UIScrollView-003.jpg

回弹机制:bounces alwaysBounceHorizontal alwaysBounceVertical

  • bounces:描述的当scrollview的显示超过内容区域的边缘以及返回时,是否有弹性,默认值为YES。值为YES的时候,意味着到达contentSize所描绘的的边界的时候,拖动会产生弹性。值为No的时候,拖动到达边界时,会立即停止。所以,如果在上面的例子当中,将bounces设置为NO时,窗口中是不会显示contentSize范围外的内容的。
  • alwaysBounceHorizontal:默认值为NO,如果该值设为YES,并且bounces也设置为YES,那么,即使设置的contentSize比scrollView的size小,那么也是可以拖动的。
  • alwaysBounceVertical:默认值为NO,如果该值设为YES,并且bounces也设置为YES,那么,即使设置的contentSize比scrollView的size小,那么也是可以拖动的。

状态条显示indicatorStyle showsHorizontalScrollIndicator showsVerticalScrollIndicator scrollIndicatorInsets flashScrollIndicators

根据我们的实际需要,我们可以对状态条进行各种设置。

  • indicatorStyle: 状态条的风格,默认值为UIScrollViewIndicatorStyleDefault。除此之外,还有UIScrollViewIndicatorStyleBlack, UIScrollViewIndicatorStyleWhite两种其他风格。可以用来和环境配色。
  • showsHorizontalScrollIndicator : 当处于跟踪状态(tracking)时是否显示水平状态条,默认值为YES。下一节说明什么是跟踪状态。
  • showsVerticalScrollIndicator : 当处于跟踪状态(tracking)时是否显示垂直状态条,默认值为YES。
  • scrollIndicatorInsets : 状态条和scrollView边距的距离(暂时还没想明白为什么要有这个)。
  • flashScrollIndicators: 短暂的显示一下状态条,当你将scrollView调整到最上面时,需要调用一下该方法。

状态跟踪

之前提到过跟踪状态(tracking)。相关的属性有三个:tracking dragging decelerate,这三个属性表明了当前视图的滚动状态。

  • tracking: 只读,一旦用户开始触摸视图(也许还没有开始拖动),该属性值为YES
  • dragging: 只读,当用户开始拖动(手指已经在屏幕上滑动一段距离了),该属性值为YES
  • decelerate: 只读,当用户松开手指,但视图仍在滚动时,该值返回YES
  • zooming: 只读,用户是否正在进行缩放手势。
  • zoomBouncing:只读,当缩放超过最大或者最小范围的时候,回弹到最大最小范围的过程中,该值返回YES。

缩放

上一节我们看到了zooming属性,scrollView除了支持拖动之外,还支持缩放。

  • maximumZoomScale: 最大放大比例,默认值为1,不得小于minimumZoomScale
  • minimumZoomScale: 最小放大比例,默认值为1,不得大于maxmumZoomScale
  • bouncesZoom: 描述在缩放超过缩放比例时,是否bounce,默认值为YES。如果值为NO,则达到最大或最小缩放比例时会立即停止缩放。否则,产生弹簧效果。
  • zoomScale: 当前的缩放比例。系统会根据缩放过程调整此值。
  • setZoomScale:animated:: 程序设置缩放大小。
  • zoomToRect:animated: 将内容视图缩放到指定的Rect中。
  • panGestureRecognizer
  • pinchGestureRecognizer

其他设置

  • delegate: scrollView的委托对象,该委托对象必须实现UIScrollViewDelegate协议,这些方法会在合适的时候被调用。
  • scrollEnabled:视图是否可被拖动,默认值为YES。一旦该值设置为NO,则scrollView不再接受触屏事件,会直接传递响应链。
  • scrollToTop:是否启动“滚动至顶端”手势,默认值为YES。当用户使用了“滚动至顶端”手势(轻击状态栏)时,系统会要求离状态栏最近的scrollView滚动到顶端,如果scrollToTop设置为NO,则该scrollView的delegate的scrollViewShouldScrollToTop:方法会返回NO,不会滚动到顶端。否则,则会滚动到顶端。滚动到顶端之后,会给delegate发送scrollViewDidScrollToTop:消息。需要注意的是,在iphone上,如果有多个scrollview的scrollToTop参数设置为YES的时候,“滚动至顶端”手势会失效。
  • delaysContentTouches:是否推迟触屏手势处理,默认值为YES。设置为YES的时候,系统在确定是否发生scroll事件之后,才会处理触屏手势,否则,则会立即调用touchesShouldBegin:withEvent:inContentView:方法。
  • directionalLockEnabled:是否锁定某个特定方向的滚动,默认值为NO。设置为YES时,一旦用户向水平或竖直方向拽动时,另一个方向的滚动则被锁定了。但是如果首次拽动是斜着的,那么则不会锁定方向。
  • keyboardDismissMode: 当拖动发生时,键盘的消失模式,默认值是不消失。
  • pagingEnabled:是否使用分页机制,默认值为NO。当设置为YES时,会按照scrollView的宽度对内容视图进行分页。
  • decelerationRate: 手指滑动后抬起,页面的减速速率。可以使用UIScrollViewDecelerationRateNormalUIScrollViewDecelerationRateFast常量值来设置合理的减速速率。

特殊的ScrollView

除了这些基本的ScrollView,我们常常使用的新闻客户端或者待办事项软件,都会有列表视图,也就是UITableView,也是一种ScrollView,它继承了UIScrollView的特性,又有自己的独特性。除了列表视图,还有瀑布流视图UICollectionView,也类似,未来再详细剖析。


0 0
原创粉丝点击