#import "AZRootViewController.h"
@interface AZRootViewController ()<UIScrollViewDelegate>
@end
@implementation AZRootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIScrollView *scrollView=[[UIScrollView alloc]init];
scrollView.frame=self.view.bounds;
[self.view addSubview:scrollView];
for (int i=0; i<5; i++) {
UIImageView* imageView=[[UIImageView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width*i,0,self.view.bounds.size.width,self.view.bounds.size.height)];
imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"10_1%d.jpg",i]];
[scrollView addSubview:imageView];
}
_pageControl=[[UIPageControl alloc] initWithFrame:CGRectMake(10, 300, 300, 10)];
//设置分页总页数
_pageControl.numberOfPages=5;
//当前页数
_pageControl.currentPage=2;//从0开始计数
//实现滑动和分页联动
[_pageControl addTarget:self action:@selector(changePos:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_pageControl];
//设置scrollView的滚动范围
scrollView.contentSize=CGSizeMake(self.view.bounds.size.width*5,self.view.bounds.size.height);
//开启分页
scrollView.pagingEnabled=YES;//只会显示完整的一页
//设置偏移量
//默认显示第三张图片
scrollView.contentOffset=CGPointMake(self.view.frame.size.width*2, 0);
//设置弹簧效果
scrollView.bounces=YES;
scrollView.delegate=self;
//滑动效果开关
scrollView.scrollEnabled=YES;
//显示水平垂直滚动条
scrollView.showsHorizontalScrollIndicator=YES;
scrollView.showsVerticalScrollIndicator=YES;
//滚动条位置
scrollView.scrollIndicatorInsets=UIEdgeInsetsMake(200, 0, 250, 0);
//滑动条类型
scrollView.indicatorStyle=UIScrollViewIndicatorStyleWhite;
//减速
scrollView.decelerationRate=1;
scrollView.tag=100;
//设置额外显示区域
scrollView.contentInset=UIEdgeInsetsMake(0, 100, 0, 0);//类似网易新闻的抽屉
UIView *view=[[UIView alloc] initWithFrame:CGRectMake(-100, 0, 100, self.view.bounds.size.height)];
view.backgroundColor=[UIColor redColor];
[scrollView addSubview:view];
}
//结束减速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//做一些滑动完成后的操作
_pageControl.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width;
}
-(void)changePos:(UIPageControl *)_page
{
UIScrollView *scrollView=(UIScrollView *)[self.view viewWithTag:100];
NSInteger pos=_page.currentPage;
scrollView.contentOffset=CGPointMake(self.view.bounds.size.width*pos, 0);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end