UIScorllView与UIPageControl

#import "AZRootViewController.h"


@interface AZRootViewController ()<UIScrollViewDelegate>


@end


@implementation AZRootViewController


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

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    UIScrollView *scrollView=[[UIScrollView alloc]init];

    scrollView.frame=self.view.bounds;

    [self.view addSubview:scrollView];

    

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

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

        imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"10_1%d.jpg",i]];

        [scrollView addSubview:imageView];

    }

    

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

    //设置分页总页数

    _pageControl.numberOfPages=5;

    //当前页数

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

    

    //实现滑动和分页联动

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

    

    

    [self.view addSubview:_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=[[UIView alloc] initWithFrame:CGRectMake(-100, 0, 100, self.view.bounds.size.height)];

    view.backgroundColor=[UIColor redColor];

    [scrollView addSubview:view];

 

}


//结束减速

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

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

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

}

-(void)changePos:(UIPageControl *)_page

{

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

    NSInteger pos=_page.currentPage;

    

    

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

}

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}




@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值