self.sv = [[UIScrollView alloc]initWithFrame:self.view.Frame];
self.sv.backgroundColor = [UIColor redColor];
[self.view addSubview:self.sv];
[_sv release];
滚动原因:空间大小限制了可显示的范围 如果想要显示更多的范围需要滚动
滚动条件:内容大小 》frame.size 能不能滚动由内容大小决定
self.sv.contentSize = CGSizeMake(375*6,667);
添加图片:
for(NSInteger i = 1;i <= 6;i++){
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(375*(i - 1),0,375,667)];
NSString *name = [NSString stringWithFormat:@"S%ld.jpg",i];
imageView.image = [UIImage imageNamed:name];
[self.sv addSubview:imageView];
[mageView releas];
}
滚到哪由偏移量决定:只要视图滚动 偏移量就会改变 反之 只要改变偏移量 视图就会滚动
sv.contentOffset = CGPiontMake(100,100);
内边距:
sv.contentInset = UIEdgeInsetsMake(100,200,300,400);
整页翻动:以当前滚动视图的宽度为一页
self.sv.pagingEnabled = YES;
边缘弹动效果:
self.sv.bounces = YES;
滚到顶部:每个滚动视图都默认为YES 一个页面中存在多个滚动视图时仅开启需要滚动的视图 其他滚动视图设置为NO;
self.sv.scrollsToTop = YES;
是否允许滚动
self.sv.scrollEnabled = YES;
滚动条是否隐藏
水平:self.sv.showsHorizontalScrollIndicator = NO;
垂直:self.sv.showsVerticalScrollIndicator = NO;
*********UIPageControl*****
UIPageControl *page = [[UIPageControl alloc]initWithFrame:CGRectMake(0,0,130,30)];
page.backgroundColor = [UIColor blackColor];
添加父视图:加在self.view保证视图滚动时 page依然存在
[self.view addSubview:page];
[page release];
page.numberOfPages = 6;
page.center = CGPointMake(self.view.center.x,667-50);
[page addTarget:self action:@selector(page:)forControlEvents:UIControlEventVventValueChanged];
page.tag = 1000;
视图的滚动分为两个阶段:
1.拖拽(手指在屏幕上)
2.减速(手指离开屏幕)
self.sv.delegate = self;
滚动视图功能为:滚动 缩放
最小比例
self.sv.minimumZomScale = 0.5;
最大比例
self.sv.maximumZoomScale = 2;
}
页码控制器 修改小圆点要放在结束减速的位置
-(void)scrollViewEndDecelerating:(UIScrollView *)scrollView{
UIPageControl *pc = [self.view viewWithTag:1000];
pc.currentPage = self.sv.contentOffset.x/375;
}
通过动画滚动:
-(void)page:(UIPageControl *)page{
[UIView animateWithDuration:1animations:^{
self.sv.contentOffset = CGPointMake(375*page.currentPage,0);
}];
}