UIScrollView --->UIView
//创建UIScrollView
testScrollView=[[UIScrollView alloc]init];
testScrollView.frame=CGRectMake(0, 0, 320, 200);
testScrollView.backgroundColor=[UIColor cyanColor];
//设置UIScrollView的容量
testScrollView.contentSize =CGSizeMake(320*5, 200 );
//隐藏水平指示条
testScrollView.showsHorizontalScrollIndicator =NO;
//隐藏垂直指示条
testScrollView.showsVerticalScrollIndicator = NO;
//是否允许分页 YES为允许 默认为NO
testScrollView.pagingEnabled =YES;
//设置边缘动画效果 NO 为无动画 默认为Yes
testScrollView.bounces =YES;
//设置代理
testScrollView.delegate =self;
//显示ScrollView
[self.view addSubview:testScrollView];
//创建一个UIPageControl,图片下面的计数点
UIPageControl *page =[[UIPageControl alloc] initWithFrame:CGRectMake(110, 130, 100, 100)];
// page.backgroundColor =[UIColor redColor];
//设置当前page的页数
page.numberOfPages =5;
//设置当前page显示哪一页
page.currentPage =0;
//设置当前未选中页的颜色
page.pageIndicatorTintColor =[UIColor yellowColor];
//设置选中的页的颜色
page.currentPageIndicatorTintColor=[UIColor redColor];
//对UIPageControl进行方法绑定
[page addTarget:self action:@selector(movePageControl:) forControlEvents:UIControlEventValueChanged];
//创建定时器
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(move) userInfo:nil repeats:YES];
}
//静态变量 只会执行一次
static int count=-1;
//实现方法
-(void)move
{
if (page.currentPage==4||page.currentPage==0) {
count=-count;
}
page.currentPage=page.currentPage+count;
//设置每一次跳转的偏移量
testScrollView.contentOffset=CGPointMake(page.currentPage*320, 0);
}
//找到偏移量方法
-(void)movePageControl:(UIPageControl *)pageControl
{
//设置偏移量无动画效果
//testScrollView.contentOffset=CGPointMake(320 * page.currentPage, 0);
//设置scrollView偏移量并且使用动画效果
[testScrollView setContentOffset:CGPointMake(pageControl.currentPage*320, 0)];
NSLog(@"%d",pageControl.currentPage);
}
#pragma mark-
#pragma mark-UIScrollViewDelegete
//图片正在移动时调用
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSLog(@"图片正在移动时调用");
}
//图片移动开始时会调用一次
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
NSLog(@"图片移动开始时会调用一次");
}
//当松开scrollView时会调用一次
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
NSLog(@"当松开scrollView时会调用一次");
}
//scrollView滑动结束调用一次
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(@"scrollView滑动结束调用一次");
NSLog(@"%f",scrollView.contentOffset.x);
page.currentPage= scrollView.contentOffset.x/320;
}