scrollView 循环滚动

本文介绍了一种在iOS应用中实现无限循环滚动图片的方法。通过在滚动视图两端添加首尾图片,确保了图片可以无缝循环展示。文章详细展示了如何使用UIButton作为图片载体,并通过scrollView代理方法处理滚动结束时的位置重置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思路:比需要创建的滚动图片多添加两个,在第一个图片前面把最后个添加进去,最后面个图片后面添加第一个图片。

1、在第一个显示的图片前面添加最后一张图片

 UIButton *button0 = [UIButton buttonWithType:UIButtonTypeCustom];

    button0.frame = CGRectMake(0, 0, kScreenwidth, CGRectGetHeight(__scrollerView.frame) - 64);

    

    if (modelArray.count > 0) {

        

        AdvertisementModel *model = [modelArray objectAtIndex:modelArray.count - 1];

        //不为空

        if (![model.imagehttp isEqual:[NSNull null]]) {

            

            ImgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:model.imagehttp]];

            UIImage *img = [UIImage imageWithData:ImgData];

            

            [button0 setBackgroundImage:img forState:UIControlStateNormal];

        }else

        {

            [button0 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"pic%d.png",modelArray.count]] forState:UIControlStateNormal];

        }

        

        button0.tag = 99;

        

        [button0 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

        [__scrollerView addSubview:button0];


    }

    

2、添加需要添加的图片

    for (int i = 1; i <= modelArray.count; i++) {

     

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

        button.frame = CGRectMake(i*kScreenwidth, 0, kScreenwidth, CGRectGetHeight(__scrollerView.frame) - 64);

        AdvertisementModel *model = [modelArray objectAtIndex:i - 1];

        

        //不为空

        if (![model.imagehttp isEqual:[NSNull null]]) {

            

            ImgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:model.imagehttp]];

            UIImage *img = [UIImage imageWithData:ImgData];

 

            [button setBackgroundImage:img forState:UIControlStateNormal];

        }else

        {

            [button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"pic%d.png",i]] forState:UIControlStateNormal];

        }

        

        button.tag = 100 + i;

        

        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

        [__scrollerView addSubview:button];

        

    }

    

3、在最后面张图片后面添加第一张图

    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];

    button1.frame = CGRectMake(kScreenwidth * (modelArray.count + 1), 0, kScreenwidth, CGRectGetHeight(__scrollerView.frame) - 64);

    if (modelArray.count > 0) {

        

        AdvertisementModel *model = [modelArray objectAtIndex:0];

        //不为空

        if (![model.imagehttp isEqual:[NSNull null]]) {

            

            ImgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:model.imagehttp]];

            UIImage *img = [UIImage imageWithData:ImgData];

            

            [button1 setBackgroundImage:img forState:UIControlStateNormal];

        }else

        {

            [button1 setBackgroundImage:[UIImage imageNamed:@"pic1.png"] forState:UIControlStateNormal];

        }

        

        button1.tag = 98;

        

        [button1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

        [__scrollerView addSubview:button1];

        

    }

 4、设置偏移量 指向第一张图

    __scrollerView.contentOffset = CGPointMake(kScreenwidth, 0);


//代理方法

-(void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    

    NSInteger page = (NSInteger)scrollView.contentOffset.x /kScreenwidth - 1;

    _pageControl.currentPage =  page;

    

}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    NSInteger page = (NSInteger)scrollView.contentOffset.x /kScreenwidth;


    if (page == 0) {

        [__scrollerView scrollRectToVisible:CGRectMake(kScreenwidth*modelArray.count, 0, kScreenwidth, 256) animated:NO];

    }

    else if (page == modelArray.count + 1)

    {

        [__scrollerView scrollRectToVisible:CGRectMake(kScreenwidth, 0, kScreenwidth, 256) animated:NO];

    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

moxi_wang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值