iOS开发UI篇—无限轮播(功能完善)…

本文详细介绍了如何在iOS应用中实现一个功能完善的无限轮播图,包括设置定时器来自动切换图片,计算并滚动到下一个显示位置的逻辑,以及在Storyboard中添加页码控件的步骤。同时强调了将定时器添加到runloop的重要性,以确保在用户处理其他任务时仍能正常工作。

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

[self addNSTimer];
}
-(void)addNSTimer
{
      [NSTimer scheduledTimerWithTimeIn terval:1.5 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
}
-(void)nextPage
{
      NSLog(@"%@",[self.collectinView indexPathsForVisibleItem s]);
//      NSIndexPath *currentIndexPath=[[self.collectinView indexPathsForVisibleItem s]lastObject];
}
 打印查看:

  

实现步骤:

(1)添加并设置定时器

(2)设置定时器的调用方法

  1)获取当前正在展示的位置

  2)计算出下一个需要展示的位置

  3)通过动画滚动到下一个位置

  注意点:需要进行判断。

实现代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    //注册cell
//    [self.collectinView registerClass:[YYimageCell class] forCellWithReuseIdentifier:YYCell];
    [self.collectinView registerNib:[UINib nibWithNibName:@"YYcell" bundle:nil] forCellWithReuseIdentifier:YYIDCell];
   
   
     [self.collectinView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:50] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
     [self addNSTimer];
}

-(void)addNSTimer
{
   NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    //添加到runloop中
    [[NSRunLoop mainRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
}
-(void)nextPage
{
//    NSLog(@"%@",[self.collectinView indexPathsForVisibleItems]);
        //1)获取当前正在展示的位置
    NSIndexPath *currentIndexPath=[[self.collectinView indexPathsForVisibleItems]lastObject];
   
       //2)计算出下一个需要展示的位置
    NSInteger nextItem=currentIndexPath.item+1;
    NSInteger nextSection=currentIndexPath.sec

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

子木潇雨

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

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

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

打赏作者

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

抵扣说明:

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

余额充值