UIScrollView,UIPageControl的结合使用

创建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__);

}    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值