UIScrollView --->UIView//創建UIScrollViewtestScrollView=[[UIScrollView alloc]init];testScrollView.frame=CGRectMake(0, 0, 320, 200);testScrollView.backgr...
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;
}