UIScollView(ZHUANG
来源:互联网 发布:java项目介绍ppt 编辑:程序博客网 时间:2024/05/17 09:41
contentSize是scrollview可以滚动的区域,比如frame = (0 ,0 ,320 ,480)contentSize = (320 ,960),代表你的scrollview可以上下滚动,滚动区域为frame大小的两倍。
contentOffset是scrollview当前显示区域顶点相对于frame顶点的偏移量,比如上个例子你拉到最下面,contentoffset就是(0,480),也就是y偏移了480
contentInset是scrollview的contentview的顶点相对于scrollview的位置,例如你的contentInset = (0,100),那么你的contentview就是从scrollview的(0 ,100)开始显示
另外UITableView是UIScrollView的子类,它们在上述属性又有所不同,tabelview的contentsize是由它的下列方法共同实现的
- (NSInteger)numberOfSections;
-(NSInteger)numberOfRowsInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection
- (CGFloat)tableView:(UITableView*)tableView heightForFooterInSection
它会自动计算所有的高度和来做为它的contentsize的height.
例如你在delegate方法
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
}
那么你的tabelview的contentsize就是(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在响应了。
// 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.scrollView scrollRectToVisible: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 * [slideImages count],0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO]; } else if (currentPage==([slideImages count]+1)) { //如果是最后+1,也就是要开始循环的第一个 [self.scrollView scrollRectToVisible: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
- UIScollView(ZHUANG
- zhuang
- (zhuang)hr
- Zhuang miao
- iOS中UIScollView的总结
- bing zhuang tu de shixian
- HDU 2886 Lou 1 Zhuang
- 使用UINavigationController后导致UIScollView尺寸变化
- 使用UINavigationController后导致UIScollView尺寸变化
- 使用UINavigationController后导致UIScollView尺寸变化
- UIScollView容易被忽视的属性
- UIScollView的滚动条总是显示
- 使用UINavigationController后导致UIScollView尺寸变化
- bu hui zhuang shu ru fa
- UIScollview(包含离屏部分)转换为UIImage
- 使用UINavigationController后导致UIScollView尺寸变化(转)
- 关于UIScollView及其子类的在导航控制器
- wo mu qian de zhuang kuang 2007.07.06
- Linux 文件系统
- Linux文本编辑
- maven 配置文件初步了解
- 按F11查询,显示为白色,而不是蓝色
- 保护无线网络的五大技巧
- UIScollView(ZHUANG
- WPF 相关总结
- 简单标签
- 二分查找
- 交互式shell和非交互式shell、登录shell和非登录shell的区别
- 使用C#发送邮件
- Shell和Shell编程
- OWASP TOP10 -2010
- android 属性android:visibility及 view的setVisibility方法值的意思