iOS scrollView geekband

创建scrollView


iOS scrollView geekband_第1张图片

UIScrollView 的尺寸


iOS scrollView geekband_第2张图片

UIScrollView 的滚动


iOS scrollView geekband_第3张图片

监听方式


iOS scrollView geekband_第4张图片

pinch响应


iOS scrollView geekband_第5张图片

用scrollview 基本离不开page


iOS scrollView geekband_第6张图片

案例
新建文档
加入文件


iOS scrollView geekband_第7张图片

在storyboard添加控件 并约束
![Uploading Snip20160407_54_349638.png . . .]


iOS scrollView geekband_第8张图片

在viewcontroller添加以下代码


iOS scrollView geekband_第9张图片

在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;
}
}
iOS scrollView geekband_第10张图片

你可能感兴趣的:(iOS scrollView geekband)