【问题标题】:Horizontal auto scroll UICollectionviewcell水平自动滚动 UICollectionviewcell
【发布时间】:2019-05-06 23:14:06
【问题描述】:

我正在尝试实现 UICollectionView 单元格的水平自动滚动。滚动发生在从第一个索引到第二个索引之后它停止并且 NSTimer 无效。下面是我实现的代码。我究竟做错了什么? 集合视图位于 tableviewcell 内。 UITableview --- UITableViewCell -- Section 0 -- UI collectionview

 - (void)addTimer
{

    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(nextPage) userInfo:nil repeats:NO];
    //    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(self.recCollectionView.frame.size.width, self.recCollectionView.frame.size.height);
}

- (void)nextPage
{
    @try
    {
        // 1.back to the middle of sections
        NSIndexPath *currentIndexPathReset = [self resetIndexPath];

        // 2.next position
        NSInteger nextItem = currentIndexPathReset.item + 1;
        NSInteger nextSection = currentIndexPathReset.section;
        if (nextItem == [[[self.schemeDataDictionary objectForKey:@"data"]objectForKey:delegate.tableString] count]) {
            nextItem = 0;
            nextSection++;
        }

        NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:nextItem inSection:nextSection];

        // 3.scroll to next position
        [self.recCollectionView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    }
    @catch (NSException * e) {
        NSLog(@"Exception: %@", e);
    }
    @finally {
        NSLog(@"finally");
    }
}
- (NSIndexPath *)resetIndexPath
{
    @try
    {
        // currentIndexPath
        NSIndexPath *currentIndexPath = [[self.recCollectionView indexPathsForVisibleItems] lastObject];
        // back to the middle of sections
        NSIndexPath *currentIndexPathReset = [NSIndexPath indexPathForItem:currentIndexPath.item inSection:0/2];
        [self.recCollectionView scrollToItemAtIndexPath:currentIndexPathReset atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
        return currentIndexPathReset;
    }
    @catch (NSException * e) {
        NSLog(@"Exception: %@", e);
    }
    @finally {
        NSLog(@"finally");
    }   
}
- (void)removeTimer
{
    [self.timer invalidate];
    //self.timer = nil;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self removeTimer];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self addTimer];
}

【问题讨论】:

    标签: ios objective-c uicollectionview uicollectionviewcell autoscroll


    【解决方案1】:

    对于NSTimer,将repeats:NO 设置为TRUE 不要在滚动委托中添加和删除计时器。

    【讨论】:

    • 工作的兄弟。所有索引路径都会自动滚动,但是当它到达最后一个索引路径时,它不会重置索引路径并滚动回 0 索引。
    • @SandeepMaganti 当前索引等于单元格计数时,将索引重置为 0。
    猜你喜欢
    • 2022-12-21
    • 2017-06-17
    • 1970-01-01
    • 2015-10-04
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多