private CGPoint?GetSnapScrollPosition(UIScrollView scrollView, nfloat velocity, CGPoint offset) { CarouselCollectionView collectionView = null; if (!_collectionView.TryGetTarget(out collectionView)) { return(null); } var count = _carousel?.Items?.Count; if (!(count >= 0)) { return(null); } var location = velocity >= 0 ? offset : new CGPoint(offset.X + collectionView.Frame.Width, 0); var currentIndex = collectionView.IndexPathForItemAtPoint(location); if (currentIndex == null) { return(null); } var currentRect = collectionView.GetLayoutAttributesForItem(currentIndex); NSIndexPath newIndex = currentIndex; if ((Math.Abs(velocity) > _velocityThreshold && velocity > 0 || offset.X > currentRect.Frame.Left + currentRect.Bounds.Size.Width / 2) && currentIndex.Row < count - 1) { newIndex = NSIndexPath.FromRowSection(currentIndex.Row + 1, 0); } else if ((Math.Abs(velocity) > _velocityThreshold && velocity < 0 || offset.X < currentRect.Frame.Left - currentRect.Bounds.Size.Width / 2) && currentIndex.Row > 0) { newIndex = NSIndexPath.FromRowSection(currentIndex.Row - 1, 0); } UICollectionViewLayoutAttributes newRect = currentRect; if (newIndex != currentIndex) { CurrentIndex = newIndex.Row; newRect = collectionView.GetLayoutAttributesForItem(newIndex); var item = _carousel.Items[CurrentIndex]; _carousel.Current = item; } return(newRect.Frame.Location); }
public void SnapToCurrentIndex(bool animated = true) { if (!(CurrentIndex >= 0 && _carousel?.Items?.Count > 0)) { return; } CarouselCollectionView collectionView = null; if (!_collectionView.TryGetTarget(out collectionView)) { return; } var index = NSIndexPath.FromRowSection(CurrentIndex, 0); var rect = collectionView.GetLayoutAttributesForItem(index); var duration = animated ? 0.2f : 0f; UIView.Animate(duration, () => { collectionView.ContentOffset = rect.Frame.Location; }); }