创建scrollView
UIScrollView 的尺寸
UIScrollView 的滚动
监听方式
pinch响应
用scrollview 基本离不开page
案例
新建文档
加入文件
在storyboard添加控件 并约束
![Uploading Snip20160407_54_349638.png . . .]
在viewcontroller添加以下代码
在viewDidLoad下添加
_scrollview.bounces = NO;
_scrollview.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
_scrollview.contentOffset = CGPointMake(0, 0);//最远拉伸位置
_scrollview.userInteractionEnabled = YES;
_scrollview.pagingEnabled = YES;
_scrollview.delegate = self;
_scrollview.backgroundColor = [UIColor grayColor];
_scrollview.showsHorizontalScrollIndicator = NO;
_scrollview.showsVerticalScrollIndicator = NO;
_scrollview.contentSize = CGSizeMake(414*3,0);//以6S Plus 宽度做例子
_pageControl.numberOfPages = 3 ;
_pageControl.currentPage = 0 ;
_pageControl.userInteractionEnabled = YES;//pagecontrol能否点击操作
_pageControl.enabled = YES;//点 不能点击
[_pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
self.firstVC = [[FirretVC alloc] init];
[self.firstVC.view setFrame:CGRectMake(0,0,600, 400)];
[self addChildViewController:_firstVC];
self.secondVC = [[secondVC alloc] init];
[self.secondVC.view setFrame:CGRectMake(414, 0,600, 400)];
self.thirdVC = [[thirdVC alloc] init];
[self.thirdVC.view setFrame:CGRectMake(414*2,0,600, 400)];
[self.scrollview addSubview:_firstVC.view];
[self.scrollview addSubview:_secondVC.view];
[self.scrollview addSubview:_thirdVC.view];
添加方法,
滑动分页效果
#pragma scrollView
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSInteger index = scrollView.contentOffset.x/scrollView.frame.size.width;
NSLog(@"%ld",(long)index);
[_pageControl setCurrentPage:index];
}
定义page
#pragma pageControl
-(void)scrollToPage:(int)Page {
CGPoint offset = self.scrollview.contentOffset;
offset.x = 414 * Page;
[UIView animateWithDuration:0.5 animations:^{
self.scrollview.contentOffset = offset;
}];
}
点击亮点移动页面
-(void)changePage:(id)sender{
switch (_pageControl.currentPage) {
case 0 : [self scrollToPage:0];
break;
case 1 : [self scrollToPage:1];
break;
case 2 : [self scrollToPage:2];
break;
default:
break;
}
}