UIScorllView与UIPageControl

来源:互联网 发布:dw制作淘宝导航条 编辑:程序博客网 时间:2024/05/16 09:01

#import "AZRootViewController.h"


@interfaceAZRootViewController ()<UIScrollViewDelegate>


@end


@implementation AZRootViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

   self = [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];

   if (self) {

        // Custom initialization

    }

    returnself;

}


- (void)viewDidLoad

{

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    

    UIScrollView *scrollView=[[UIScrollViewalloc]init];

    scrollView.frame=self.view.bounds;

    [self.viewaddSubview:scrollView];

    

   for (int i=0; i<5; i++) {

        UIImageView* imageView=[[UIImageViewalloc] initWithFrame:CGRectMake(self.view.bounds.size.width*i,0,self.view.bounds.size.width,self.view.bounds.size.height)];

        imageView.image=[UIImageimageNamed:[NSStringstringWithFormat:@"10_1%d.jpg",i]];

        [scrollViewaddSubview:imageView];

    }

    

    _pageControl=[[UIPageControlalloc] initWithFrame:CGRectMake(10,300,300,10)];

    //设置分页总页数

    _pageControl.numberOfPages=5;

    //当前页数

    _pageControl.currentPage=2;//0开始计数

    

    //实现滑动和分页联动

    [_pageControl addTarget:selfaction:@selector(changePos:)forControlEvents:UIControlEventTouchUpInside];

    

    

    [self.viewaddSubview:_pageControl];

    

    

    //设置scrollView的滚动范围

    scrollView.contentSize=CGSizeMake(self.view.bounds.size.width*5,self.view.bounds.size.height);

    

    //开启分页

    scrollView.pagingEnabled=YES;//只会显示完整的一页

    

    //设置偏移量

    //默认显示第三张图片

    scrollView.contentOffset=CGPointMake(self.view.frame.size.width*2,0);

    

    

    //设置弹簧效果

    scrollView.bounces=YES;

    

    

    scrollView.delegate=self;

    //滑动效果开关

    scrollView.scrollEnabled=YES;

    

    //显示水平垂直滚动条

    scrollView.showsHorizontalScrollIndicator=YES;

    scrollView.showsVerticalScrollIndicator=YES;

    

    //滚动条位置

    scrollView.scrollIndicatorInsets=UIEdgeInsetsMake(200,0,250,0);

    

    //滑动条类型

    scrollView.indicatorStyle=UIScrollViewIndicatorStyleWhite;

    

   //减速

    scrollView.decelerationRate=1;

    scrollView.tag=100;

    

    //设置额外显示区域

    scrollView.contentInset=UIEdgeInsetsMake(0,100, 0, 0);//类似网易新闻的抽屉

    UIView *view=[[UIViewalloc] initWithFrame:CGRectMake(-100,0,100,self.view.bounds.size.height)];

    view.backgroundColor=[UIColorredColor];

    [scrollViewaddSubview:view];

 

}


//结束减速

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    //做一些滑动完成后的操作

    _pageControl.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width;

}

-(void)changePos:(UIPageControl *)_page

{

    UIScrollView *scrollView=(UIScrollView *)[self.viewviewWithTag:100];

    NSInteger pos=_page.currentPage;

    

    

    scrollView.contentOffset=CGPointMake(self.view.bounds.size.width*pos,0);

}

- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}




@end


0 0