有关UIScrollView 和 UIPageControll 结合使用

定义:

pageControl = [[[UIPageControl alloc]initWithFrame:CGRectMake(300, 200, 100, 30)]autorelease];//定义初始化  

[pageControl setNumberOfPages:9];//设置总页数  

[pageControl setCurrentPage:0];//设置当前页

 [pageControl addTarget:self action:@selector(clickpagecontrol) forControlEvents:UIControlEventValueChanged];//添加事件  

[self.view addSubview:pageControl];//这个就不解释啦

 

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView1

//减速停止的时候执行,如果没有这个方法慢慢拖动的时候,会导致  offsetofScrollView.x / scroll.frame.size.width 计算不准确  ,

{

CGPoint offsetofScrollView = scrollView1.contentOffset;

[pageControl setCurrentPage:offsetofScrollView.x / scroll.frame.size.width];

}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate

//停止拖拽的时候开始执行,如果没有这个方法 你会发现当你快速滑动UIScrollview的时候并没有触发scrollViewDidEndDecelerating:(UIScrollView *)scrollView1事件

{

CGPoint offsetofScrollView = scroll.contentOffset;

[pageControl setCurrentPage:offsetofScrollView.x / scroll.frame.size.width];

}

-(void)clickpagecontrol

{//点击UIPageControl 出发的事件  

[scroll scrollRectToVisible:CGRectMake(pageControl.currentPage*scroll.frame.size.width, 0, scroll.frame.size.width , scroll.frame.size.height) animated:YES];

}

posted @ 2012-05-04 23:15  高笑228  阅读(242)  评论(0编辑  收藏  举报