创建ViewController类
.h文件里面:
@interface HomeViewController : UIViewController<UIAlertViewDelegate,UIScrollViewDelegate,ChangeTitleDelegate>{
UIButton *btn;
UIScrollView *_scrollerView;
UIPageControl *_page;
}
@end
.m文件里面:
-(void)loadView{
self.view=[[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
self.view.backgroundColor=[UIColor blackColor];
//用在转换时的标题
//self.title=@"edag";
btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame=CGRectMake(100, 100, 100, 30);
[btn addTarget:self
action:@selector(pushAction:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[btn release];
_scrollerView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
_scrollerView.userInteractionEnabled=YES;
_scrollerView.pagingEnabled=YES;
[self.view addSubview:_scrollerView];
[_scrollerView release];
for (int i=0;i<4;i++) {
UIView *aview=[[UIView alloc]initWithFrame:CGRectMake(i*320, 0, 320, 420)];
aview.backgroundColor=[UIColor colorWithRed:(arc4random()%256)/255.0f
green:(arc4random()%256)/255.0f
blue:(arc4random()%256)/255.0f
alpha:1.0f];
[_scrollerView addSubview:aview];
[aview release];
}
//设置滑动的长和宽
_scrollerView.contentSize=CGSizeMake(4*320,480);
_scrollerView.delegate=self;
_page=[[UIPageControl alloc]initWithFrame:CGRectMake(110, 200, 100, 30)];
//设置当前界面
_page.currentPage=0;
//一共有几页
_page.numberOfPages=4;
[_page addTarget:self
action:@selector(pageChange:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_page];
[_page release];
}
-(void)pageChange:(UIPageControl *)sender{
CGFloat offset= sender.currentPage*320;
[_scrollerView setContentOffset:CGPointMake(offset, 0) animated:YES];
}
//只要一滚动就实现
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSLog(@"%s",__func__);
// NSLog(@"_scrollerView.contentOffset==%@",NSStringFromCGPoint(_scrollerView.contentOffset));
// NSLog(@"_scrollerView.bounds==%@",NSStringFromCGRect(_scrollerView.bounds));
int index=scrollView.contentOffset.x/scrollView.frame.size.width;
_page.currentPage=index;
}
//开始滚动时
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
NSLog(@"%s",__func__);
}
//滚动结束
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
NSLog(@"%s",__func__);
}
//开始加速
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
NSLog(@"%s",__func__);
}
//减速
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSLog(@"%s",__func__);
}
//缩小放大等用
- (void)scrollViewDidZoom:(UIScrollView *)scrollView NS_AVAILABLE_IOS(3_2){
NSLog(@"%s",__func__);
}