0

J'ai créé cette boucle infinie collectionView avec 13 icônes, mais elle ne fonctionne pas correctement sur les périphériques de résolution 320 x 568. Le problème est de calculer l'image appropriée contentOffset lors du défilement (c'est une animation - ce n'est pas touchable) et ce n'est pas centré après un certain temps. Ces images sont espacées de manière égale car je veux montrer 3 icônes sur l'écran à la fois. Lorsque 3 icônes passent previousContentOffset est x = 319.5 et il devrait être x = 320.0 Des suggestions de solution de contournement pour ce cas particulier? Peut-être itérer par icône unique? Résultats je veux dire après une icône diapositive:iOS - valeur CGPoint inexacte dans collectionView contentOffset

  • previousContentOffset = (CGPoint) (x = 106,5, y = 0),

  • width = (double) 106,66666666666667,

  • currentContentOffset = (CGPoint) (x = 213,16666666666669, y = 0)

// boucle infinie

- (void)setupDataForCollectionView { 

NSArray *originalArray = self.arrayImages; 

NSMutableArray *workingArray = [originalArray mutableCopy]; 

self.dataArray = [workingArray mutableCopy]; 
} 

// Implementation of infinite loop 

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { 

// calculate width for every device 

double contentOffsetWhenFullyScrolledRight = self.collectionViewHorizontal.contentSize.width - self.collectionViewHorizontal.frame.size.width; 

if (scrollView.contentOffset.x == contentOffsetWhenFullyScrolledRight) { 

    NSIndexPath *newIndexPath = [NSIndexPath indexPathForItem:1 inSection:0]; 

    [self.collectionViewHorizontal scrollToItemAtIndexPath:newIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO]; 

} else if (scrollView.contentOffset.x == 0) { 

    NSIndexPath *newIndexPath = [NSIndexPath indexPathForItem:([self.dataArray count] -2) inSection:0]; 

    [self.collectionViewHorizontal scrollToItemAtIndexPath:newIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO]; 
    } 
} 

// TimeInterval for animation 

- (void)timerAction:(NSTimer *)timer { 

//previous icon content offset 
CGPoint previousContentOffSet = self.collectionViewHorizontal.contentOffset; 

//next icon width 
double width = self.collectionViewHorizontal.frame.size.width/3; 
// NSUInteger index = (int)(floor(self.collectionViewHorizontal.contentOffset.x/width) + 1); 

// animate icons scroll view 
CGPoint currentContentOffset = CGPointMake(previousContentOffSet.x + width, previousContentOffSet.y); 

NSIndexPath *indexPath = [self.collectionViewHorizontal indexPathForItemAtPoint:currentContentOffset]; 
if (indexPath.row > self.dataArray.count - 4) { 
    // add elements and reload 
    [self.dataArray addObjectsFromArray:[self.arrayImages copy]]; 
    [self.collectionViewHorizontal reloadData]; 
} 
// sliding animation 
[UIView animateWithDuration:.3 delay:.0 usingSpringWithDamping:0.5 initialSpringVelocity:.0 options:kNilOptions animations:^{ 
    self.collectionViewHorizontal.contentOffset = currentContentOffset; 
} completion:^(BOOL finished) { 
    [self scrollViewDidEndDecelerating:self.collectionViewHorizontal]; 
}]; 
} 

Répondre

0

J'ai implémenté une solution de contournement il y a quelque temps, alors j'ai pensé que je devais partager. J'ai décidé d'itérer avec les index de l'icône courante au lieu de contentOffset. J'ai créé une propriété NSInteger currentIndex et ajouté dans la méthode timer.

- (void)timerAction:(NSTimer *)timer { 

self.currentIndex++; 

//next icon width 
double width = self.collectionViewHorizontal.frame.size.width/3; 

// animate icons scroll view 
CGPoint currentContentOffset = CGPointMake(self.currentIndex * width, 0.0); 

NSIndexPath *indexPath = [self.collectionViewHorizontal indexPathForItemAtPoint:currentContentOffset]; 
if (indexPath.row > (self.dataArray.count /2) { 
    // add elements and reload 
    [self.dataArray addObjectsFromArray:[self.arrayImages copy]]; 
    [self.collectionViewHorizontal reloadData]; 
} 
// sliding animation 
[UIView animateWithDuration:.3 delay:.0 usingSpringWithDamping:0.5 initialSpringVelocity:.0 options:kNilOptions animations:^{ 
    self.collectionViewHorizontal.contentOffset = currentContentOffset; 
} completion:^(BOOL finished) { 
    [self scrollViewDidEndDecelerating:self.collectionViewHorizontal]; 
}]; 
}