UIScollView(ZHUANG

来源:互联网 发布:java项目介绍ppt 编辑:程序博客网 时间:2024/05/17 09:41

contentSizescrollview可以滚动的区域,比如frame = (0 ,0 ,320 ,480)contentSize = (320 ,960),代表你的scrollview可以上下滚动,滚动区域为frame大小的两倍。

contentOffsetscrollview当前显示区域顶点相对于frame顶点的偏移量,比如上个例子你拉到最下面,contentoffset就是(0,480),也就是y偏移了480

contentInsetscrollviewcontentview的顶点相对于scrollview的位置,例如你的contentInset = (0,100),那么你的contentview就是从scrollview(0 ,100)开始显示

 

另外UITableViewUIScrollView的子类,它们在上述属性又有所不同,tabelviewcontentsize是由它的下列方法共同实现的

- (NSInteger)numberOfSections;

-(NSInteger)numberOfRowsInSection:(NSInteger)section;

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section;

- (CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section;

它会自动计算所有的高度和来做为它的contentsizeheight.

 

例如你在delegate方法

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {

    return100;

}

那么你的tabelviewcontentsize就是(320, 4400)


 

UIScrollView分页的实现

UIScrollView的经典例子就是safari的编辑模式了

UIScrollView可以垂直,水平滚动。

如果属性pageEnabled =YES,则设置其为分页模式,那么没滚动一次就是一页,垂直方向上的一页就是UIScrollView的高度,水平方向就是其宽度。

我们可以设置contentSize,这就是UIScrollView可以滚动的最大区域了。

UIScrollView没有datasource的概念,因为这就是一个view,具体view中的内容摆放完全随意,只是每次都会滚动固定的页面大小。

比如我们有5个页面,我们需要自己计算每个页面的位置,将其水平放在UIScrollView上。

contentSize则设置为5*每个页面的宽度,每个页面的高度。

如果contentSize的高度或这宽度正好与UIScrollView的高度或宽度相等,则在该方向上不能滚动。

具体例子可以查看官方文档的例子,PageControl。

注意:UIScrollView不会带有分页的指示符号,需要利用UIPageControl控制。

 

但是要想做到safari的效果,我们需要一个小技巧,因为在safari中,当前页时可以看到前后两页的部分内容。

注意:分页的宽度不能修改,所以我们只能缩小UIScrollView的宽度,让其不要占满整个屏幕,同时设置clipsToBouds属性为NO,这样超出范围的视图也会显示,这样就能看到前后两页的内容,当然需要设置前后两页的alpha值为0.5,做出一个透明效果。

在UIScrollView的滚动事件中,判断位置,当一个页面出现的区域超过1半时,设置alpha为1,小于1半时设置alpha为0.5

 

现在还有一个问题:UIScrollView的滑动事件响应区域变小了,因为UIScrollView本身变小了,其余我们看到的页面在区域之外,因此我们需要重写UIScrollView所在的parentView的hittest事件,在其他区域的事件返回值为UIScrollView对象,这样用户感觉整个屏幕都是UIScrollView在响应了。



  循环滚动一个UIScrollView

//  testScrollViewViewController.m //  testScrollView //  Created by cash on 11-7-4. //  Copyright 2011年 xbiii3s@gmail.com. All rights reserved. #import "testScrollViewViewController.h" @implementation testScrollViewViewController @synthesize scrollView, slideImages; #define WIDTH_OF_SCROLL_PAGE 320#define HEIGHT_OF_SCROLL_PAGE 460 #define WIDTH_OF_IMAGE 320 #define HEIGHT_OF_IMAGE 460#define LEFT_EDGE_OFSET 0 - (void)viewDidLoad {     scrollView = [[UIScrollView alloc] init];     CGRect scrollFrame;     scrollFrame.origin.x = 0;     scrollFrame.origin.y = 0;       scrollFrame.size.width = WIDTH_OF_SCROLL_PAGE;     scrollFrame.size.height = HEIGHT_OF_SCROLL_PAGE;     scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];     scrollView.bounces = YES;     scrollView.pagingEnabled = YES;     scrollView.delegate = self;     scrollView.userInteractionEnabled = YES;     slideImages = [[NSMutableArray alloc] init];     [slideImages addObject:@"IMG_0116.PNG"];     [slideImages addObject:@"IMG_0118.PNG"];     [slideImages addObject:@"IMG_0119.PNG"];     [slideImages addObject:@"main_bg.png"];     //add the last image first     UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:([slideImages count]-1)]]]     imageView.frame = CGRectMake(LEFT_EDGE_OFSET, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);     [scrollView addSubview:imageView];     [imageView release];     for (int i = 0;i<[slideImages count];i++) {         //loop this bit         UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImageimageNamed:[slideImages objectAtIndex:i]]];         imageView.frame = CGRectMake((WIDTH_OF_IMAGE * i) + LEFT_EDGE_OFSET + 320, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);         [scrollView addSubview:imageView];         [imageView release];     }     //add the first image at the end     imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImagesobjectAtIndex:0]]];     imageView.frame = CGRectMake((WIDTH_OF_IMAGE * ([slideImages count] + 1)) + LEFT_EDGE_OFSET, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);     [scrollView addSubview:imageView];     [imageView release];     [scrollView setContentSize:CGSizeMake(WIDTH_OF_SCROLL_PAGE * ([slideImages count] + 2), HEIGHT_OF_IMAGE)];     [scrollView setContentOffset:CGPointMake(0, 0)];     [self.view addSubview:scrollView];     [self.scrollViewscrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];      [super viewDidLoad]; } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {     int currentPage = floor((self.scrollView.contentOffset.x - self.scrollView.frame.size.width / ([slideImages count]+2)) / self.scrollView.frame.size.width) + 1;     if (currentPage==0) {         //go last but 1 page         [self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE * [slideImagescount],0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];     } else if (currentPage==([slideImages count]+1)) { //如果是最后+1,也就是要开始循环的第一个         [self.scrollViewscrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];     }
} - (void)didReceiveMemoryWarning {     // Releases the view if it doesn't have a superview.     [super didReceiveMemoryWarning];     // Release any cached data, images, etc that aren't in use. }- (void)viewDidUnload {     // Release any retained subviews of the main view.     // e.g. self.myOutlet = nil; } - (void)dealloc {     [scrollView release];     [slideImages release];     [super dealloc]; } @end
原创粉丝点击