UIScrollView 简单使用

1、新建storyboard项目 把UIScrollView 拖入view中


2、添加UIScrollView属性 

@property (weak, nonatomic) IBOutlet UIScrollView * scrollView;


 写入代码:

-(void)addImageView

{

    CGFloat w =_scrollView.frame.size.width;

    CGFloat h =_scrollView.frame.size.height;

    for (int i=0; i<kCount; i++)

    {

        UIImageView * imageView=[UIImageView new];

        

        imageView.frame=CGRectMake(i*w, 0, w, h);

        

        NSString * imgName=[NSString stringWithFormat:@"welcome00%d.png",i+1];

        imageView.image =[UIImage imageNamed:imgName];

        

        [_scrollView addSubview:imageView];

        

    }

    

    _scrollView.contentSize=CGSizeMake(kCount*w, 0);

    

    //不现实滚动条

    _scrollView.showsHorizontalScrollIndicator=NO;

    

    //分页设置

    _scrollView.pagingEnabled = YES;

    

    //设置代理

    _scrollView.delegate = self;

    

    [self createPageControlWidth:w height:h];

}

3、添加分页圆点:

添加圆点属性

UIPageControl * _pageControl;

写入代码:


-(void)createPageControlWidth:(CGFloat)w height:(CGFloat)h

{

    //创建圆点

    UIPageControl * pageControl =[UIPageControl new];

    

    //设置圆点宽高

    pageControl.center = CGPointMake(w * 0.5, h-50);

    

    pageControl.bounds=CGRectMake(0, 0, 150, 50);

    

    //显示圆点个数

    pageControl.numberOfPages=kCount;

    

    [self.view addSubview:pageControl];

    

    _pageControl=pageControl;

    

    //禁止圆点按钮点击

    pageControl.enabled = NO;


}


#pragma mark scrollView代理方法

-(void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    int page=scrollView.contentOffset.x / scrollView.frame.size.width;

    

    //设置页码

    _pageControl.currentPage=page;

    if (page==4)

    {

        [self dismissViewControllerAnimated:YES completion:nil];

    }

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值