Горизонтальная автоматическая прокрутка UICollectionviewcell

Я пытаюсь реализовать горизонтальную автоматическую прокрутку ячейки UICollectionView. Прокрутка происходит от первого индекса ко второму индексу, после чего она останавливается, и NSTimer становится недействительным. Ниже приведен код, который я реализовал. Что я делаю неправильно? Представление коллекции находится внутри tableviewcell. UITableview --- UITableViewCell -- Раздел 0 -- Коллекция пользовательского интерфейса

 - (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];
}

person Sandeep Maganti    schedule 05.12.2018    source источник


Ответы (1)


Для NSTimer установите repeats:NO на TRUE Не добавлять и не удалять таймер в делегатах прокрутки.

person pkc456    schedule 05.12.2018
comment
Работал братан. Автоматическая прокрутка подходит для всех индексных путей, но когда она достигает последнего индексного пути, она не сбрасывает индексный путь и не прокручивает обратно до 0 индекса. - person Sandeep Maganti; 05.12.2018
comment
@SandeepMaganti Когда текущий индекс равен количеству ячеек, сбросьте индекс до 0. - person pkc456; 05.12.2018