NSIndexPath GetIndexPathForCenteredItem(UICollectionView collectionView) { var centerPoint = new CGPoint(collectionView.Center.X + collectionView.ContentOffset.X, collectionView.Center.Y + collectionView.ContentOffset.Y); var centerIndexPath = collectionView.IndexPathForItemAtPoint(centerPoint); return(centerIndexPath); }
private void SnapToCenter(UICollectionView collectionView) { if (collectionView == null || !_weakElement.TryGetTarget(out var element)) { return; } var firstIndex = NSIndexPath.FromItemSection(0, 0); if (IsCellFullyVisible(collectionView, firstIndex)) { // Check if first item is fully visible, if true don't snap. collectionView.ScrollToItem(firstIndex, UICollectionViewScrollPosition.CenteredHorizontally, true); return; } var lastIndex = NSIndexPath.FromItemSection(collectionView.NumberOfItemsInSection(0) - 1, 0); if (IsCellFullyVisible(collectionView, lastIndex)) { // Check if last item is fully visible, if true don't snap. collectionView.ScrollToItem(lastIndex, UICollectionViewScrollPosition.CenteredHorizontally, true); return; } var collectionViewCenter = collectionView.Center; var contentOffset = collectionView.ContentOffset; var center = new CGPoint( collectionViewCenter.X + contentOffset.X + (nfloat)element.CollectionPadding.Left - (nfloat)element.CollectionPadding.Right, collectionViewCenter.Y + contentOffset.Y + (nfloat)element.CollectionPadding.Top - (nfloat)element.CollectionPadding.Bottom); var indexPath = collectionView.IndexPathForItemAtPoint(center); if (indexPath == null) { // Point is right between two cells: picking one var indexes = collectionView.IndexPathsForVisibleItems.OrderBy(i => i.Item).ToArray(); if (indexes.Length > 0) { int middleIndex = (indexes.Count() - 1) / 2; var candidateIndexPath = indexes[middleIndex]; if (candidateIndexPath.Row < element.CurrentIndex) { indexPath = candidateIndexPath; } else { indexPath = indexes[middleIndex + 1 > indexes.Length ? middleIndex : middleIndex + 1]; } } } collectionView.ScrollToItem(indexPath, UICollectionViewScrollPosition.CenteredHorizontally, true); }
public CalendarItem?CalendarItemAtPoint(CGPoint point) { var indexPath = collectionView.IndexPathForItemAtPoint(point); if (indexPath != null && indexPath.Item < calendarItems.Count) { return(calendarItems[(int)indexPath.Item]); } return(null); }
private NSIndexPath IndexPathForCenterCellFromCollectionView(UICollectionView collectionView) { var point = collectionView.ConvertPointFromView(collectionView.Center, collectionView.Superview); var indexPath = collectionView.IndexPathForItemAtPoint(point); if (indexPath != null) { return(indexPath); } else { return(collectionView.IndexPathsForVisibleItems.First()); } }
private void GestureAction(UITapGestureRecognizer tap) { try { var touchLocation = tap.LocationOfTouch(0, CollectionResult); var indexP = CollectionResult.IndexPathForItemAtPoint(touchLocation); var index = (int)indexP.Item; if (index >= 0 && index < ResultItems.Count) { RemoveItem(index); } } catch (Exception ex) { Console.WriteLine(ex.StackTrace); } }
public static int GetCenteredIndex(this UICollectionView collectionView) { var centerItemIndex = -1; var indexPathsForVisibleItems = collectionView.IndexPathsForVisibleItems.OrderBy(x => x.Row).ToList(); if (indexPathsForVisibleItems.Count == 0) { return(-1); } var firstVisibleItemIndex = (int)indexPathsForVisibleItems.First().Item; var centerPoint = new CGPoint(collectionView.Center.X + collectionView.ContentOffset.X, collectionView.Center.Y + collectionView.ContentOffset.Y); var centerIndexPath = collectionView.IndexPathForItemAtPoint(centerPoint); centerItemIndex = centerIndexPath?.Row ?? firstVisibleItemIndex; return(centerItemIndex); }
public override void ViewDidLoad() { base.ViewDidLoad(); //Calculate Cell Size for screen resolution CGSize CellSize = GetSize(); /* * Initialize the collectionview layout */ UICollectionViewFlowLayout layout = new UICollectionViewFlowLayout { SectionInset = new UIEdgeInsets(20, 20, 20, 20), MinimumInteritemSpacing = 1, MinimumLineSpacing = 10, ItemSize = CellSize //new SizeF(110, 110) }; /* * Initialize the CollectionViewSource and UICollectionView */ CollectionViewSource = new CollectionViewImageStackSource(); CollectionViewSource.ImageViewSize = new SizeF((float)CellSize.Width, (float)CellSize.Height); CollectionView = new UICollectionView(UIScreen.MainScreen.Bounds, layout); CollectionView.BackgroundColor = AppColors.LIGHT_TEAL; var longPressGesture = new UILongPressGestureRecognizer(gesture => { // Take action based on state switch (gesture.State) { case UIGestureRecognizerState.Began: var selectedIndexPath = CollectionView.IndexPathForItemAtPoint(gesture.LocationInView(View)); if (selectedIndexPath != null) { CollectionView.BeginInteractiveMovementForItem(selectedIndexPath); } break; case UIGestureRecognizerState.Changed: CollectionView.UpdateInteractiveMovement(gesture.LocationInView(View)); break; case UIGestureRecognizerState.Ended: CollectionView.EndInteractiveMovement(); break; default: CollectionView.CancelInteractiveMovement(); break; } }); // Add the custom recognizer to the collection view CollectionView.AddGestureRecognizer(longPressGesture); CollectionView.ShowsHorizontalScrollIndicator = true; CollectionView.RegisterClassForCell(typeof(UserCell2), UserCell2.CellID); CollectionView.ShowsHorizontalScrollIndicator = true; CollectionView.Source = CollectionViewSource; }
private void UpdateCurrentIndex(UICollectionView collectionView) { if (collectionView == null || !_weakElement.TryGetTarget(out var element)) { return; } nint newIndex; if (element.SnapStyle == SnapStyle.Center) { var lastIndex = NSIndexPath.FromItemSection(collectionView.NumberOfItemsInSection(0) - 1, 0); if (IsCellFullyVisible(collectionView, lastIndex)) { newIndex = lastIndex.Item; } else if (IsFirstCellFullyVisible(collectionView)) { newIndex = 0; } else { var collectionViewCenter = collectionView.Center; var contentOffset = collectionView.ContentOffset; var center = new CGPoint( collectionViewCenter.X + contentOffset.X + (nfloat)element.CollectionPadding.Left - (nfloat)element.CollectionPadding.Right, collectionViewCenter.Y + contentOffset.Y + (nfloat)element.CollectionPadding.Top - (nfloat)element.CollectionPadding.Bottom); var centerPath = collectionView.IndexPathForItemAtPoint(center); if (centerPath == null) { InternalLogger.Warn( "Failed to find a NSIndexPath in SnapStyle center context: UpdateCurrentIndex returns nothing"); return; } newIndex = centerPath.Item; } } else { var firstCellBounds = new CGRect { X = collectionView.ContentOffset.X, Y = collectionView.ContentOffset.Y, Size = new CGSize(element.ItemWidth, element.ItemHeight), }; var firstCellCenter = new CGPoint(firstCellBounds.GetMidX(), firstCellBounds.GetMidY()); var indexPath = collectionView.IndexPathForItemAtPoint(firstCellCenter); if (indexPath == null) { return; } newIndex = indexPath.Row; } InternalLogger.Info($"UpdateCurrentIndex => {newIndex}"); element.CurrentIndex = (int)newIndex; }