UIPageControl和UIScrollView的联合使用


UIPageControl和UIScrollView的联合使用 
1、首先声明一个UIPageControl
UIPageControl *_pageControl;


2、对UIPageControl进行初始化,并设置总共页数
//初始化_pageControl
    _pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(0, 158, (ViewWidth-150)/2, 15)];
    [_headView addSubview:_pageControl];
//设置总共页数
_pageControl.numberOfPages=_ADArray.count;


3、UIscrollView拖拽完之后对当前页码赋值


#pragma mark - scrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{ NSLog(@"UIsrollView滚动中");
  //计算当前所在页       _pageControl.currentPage=_scrollViewShop.contentOffset.x/ViewWidth;
    }
}


4、添加一个计时器
//添加计时器,每3秒钟执行以下nextImage事件
-(void)addTimer

{

    //加上[self removeTimer];轮播时间隔时间一致,否则有快有慢
    [self removeTimer];

    timerShop=[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    [timerShop fire];
}


5、完成跳至下一个页面的事件
//跳至下一页
-(void)nextImage
{
//以下代码使用UIPageControlShop
    int curePage=(int)_pageControl.currentPage;
    if (curePage==(totalCount-1)) {
        curePage=0;
        _pageControl.currentPage=0;
    }
    else{
        curePage++;
    }
    CGFloat Xshop=curePage*_scrollViewShop.frame.size.width;
    _scrollViewShop.contentOffset=CGPointMake(Xshop, 0);



注意:1、在使用_scrollViewShop.contentOffset前必须将scrollview的滚动范围_scrollViewShop.contentSize设置好,否则没法滚动,有几张图片就设置几张图片*图片宽度
//设置scrollview的滚动范围
    CGFloat contentW=totalCount*imageW;

    _scrollViewShop.contentSize=CGSizeMake(contentW, 0);

   2、记得在程序在开始要[self  addTimer];

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值