ios objective-c uiscrollview uicollectionview paging

ios - UICollectionView con Paging Enable



objective-c uiscrollview (4)

Tengo que crear una vista de cuadrícula como la aplicación iOS de Appstore. Quiero hacer esto con la paginación UICollectionView. También implementé el código pero no pude desplazarme así.

Lo que quiero hacer es que haya una imagen en el centro y en ambos lados (izquierda y derecha), debe mostrar una parte de la imagen anterior y la siguiente. He establecido Frame para UICollectionView es 320 * 320. el tamaño de la celda es 290 * 320. (el espacio mínimo entre celdas es 10) 1

A continuación hay dos enlaces que representan mi requerimiento. Gracias por adelantado.

(Esto es lo que quiero) 2


Si usa la paginación en collectionView, se desplazará por una página. No una celda. Puede deshabilitar la paginación e implementar ScrollViewDelegate

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset { CGFloat pageW = 300; int page = scrollView.contentOffset.x / pageW; CGFloat newOffset =(page + ((velocity.x > 0)? 1 : -1)) * (pageW - 20); CGPoint target = CGPointMake(newOffset, 0); targetContentOffset = ⌖ NSLog(@"end Drag at %f /%i /%f",scrollView.contentOffset.x, page, velocity.x); }

Solo una diferente de la paginación estándar: si arrastra rápidamente, la colección desplazará más de una celda. Y no olvides agregar UIScrollViewDelegate


¿Has intentado configurar la dirección de desplazamiento de tu UICollectionViewFlowLayout en horizontal?

[yourFlowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];

Necesitarás habilitar la paginación en tu vista de colección así:

[yourCollectionView setPagingEnabled:YES];


Tomé la respuesta de Shtefane y la mejoré. Ingrese sus propios valores de cellWidth y cellPadding .

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset { CGFloat cellWidth = self.cellWidth; CGFloat cellPadding = 9; NSInteger page = (scrollView.contentOffset.x - cellWidth / 2) / (cellWidth + cellPadding) + 1; if (velocity.x > 0) page++; if (velocity.x < 0) page--; page = MAX(page,0); CGFloat newOffset = page * (cellWidth + cellPadding); targetContentOffset->x = newOffset; }


Refiérase a la respuesta de Rickster y reescribo con Swift 4:

/* paging */ extension AlbumViewController { /* In case the user scrolls for a long swipe, the scroll view should animate to the nearest page when the scrollview decelerated. */ override func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { scrollToPage(scrollView, withVelocity: CGPoint(x:0, y:0)) } override func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) { scrollToPage(scrollView, withVelocity: velocity) } func scrollToPage(_ scrollView: UIScrollView, withVelocity velocity: CGPoint) { let cellWidth: CGFloat = cellSize let cellPadding: CGFloat = 10 var page: Int = Int((scrollView.contentOffset.x - cellWidth / 2) / (cellWidth + cellPadding) + 1) if velocity.x > 0 { page += 1 } if velocity.x < 0 { page -= 1 } page = max(page, 0) let newOffset: CGFloat = CGFloat(page) * (cellWidth + cellPadding) scrollView.setContentOffset(CGPoint(x:newOffset, y:0), animated: true) } }