private static void OnRecognizing(LongPressGestureRecognizer recognizer, UITableView tableView, ViewCell cell)
            {
                NSIndexPath indexPath = tableView.IndexPathForRowAtPoint(recognizer.LocationInView(tableView));

                switch (recognizer.State)
                {
                case UIGestureRecognizerState.Began:
                    if (indexPath != null)
                    {
                        // Remember the source row
                        recognizer.sourceIndexPath      = indexPath;
                        recognizer.destinationIndexPath = indexPath;
                        cell.View.BackgroundColor       = Color.Gray;
                    }
                    break;

                case UIGestureRecognizerState.Changed:
                    if ((recognizer.destinationIndexPath != null) && (indexPath != null) && (recognizer.destinationIndexPath != indexPath))
                    {
                        // Dragged to a new row location, so show it to the user with animation
                        tableView.MoveRow(recognizer.destinationIndexPath, indexPath);
                        recognizer.destinationIndexPath = indexPath;
                    }
                    break;

                case UIGestureRecognizerState.Cancelled:
                case UIGestureRecognizerState.Failed:
                    recognizer.sourceIndexPath = null;
                    cell.View.BackgroundColor  = Color.Transparent;
                    break;

                case UIGestureRecognizerState.Recognized:
                    // Move the data source finally
                    if ((recognizer.sourceIndexPath != null) && (recognizer.destinationIndexPath != null) && (recognizer.sourceIndexPath != recognizer.destinationIndexPath))
                    {
                        // Reset the move because otherwise the underneath control will get out of sync with
                        // the Xamarin.Forms element. The next line will drive the real change from ItemsSource
                        tableView.MoveRow(recognizer.destinationIndexPath, recognizer.sourceIndexPath);
                        tableView.Source.MoveRow(tableView, recognizer.sourceIndexPath, recognizer.destinationIndexPath);
                    }

                    recognizer.sourceIndexPath      = null;
                    recognizer.destinationIndexPath = null;
                    cell.View.BackgroundColor       = Color.Transparent;
                    break;
                }
            }
            private static void OnRecognizing(LongPressGestureRecognizer recognizer, UITableView tableView, ViewCell cell)
            {
                NSIndexPath indexPath = tableView.IndexPathForRowAtPoint(recognizer.LocationInView(tableView));

                switch (recognizer.State)
                {
                case UIGestureRecognizerState.Began:
                    tableView.ScrollEnabled = false;
                    if (indexPath != null)
                    {
                        // Remember the source row
                        sourceIndexPath      = indexPath;
                        destinationIndexPath = indexPath;
                        var selectedCell = tableView.CellAt(indexPath);
                        UIGraphics.BeginImageContext(selectedCell.ContentView.Bounds.Size);
                        selectedCell.ContentView.Layer.RenderInContext(UIGraphics.GetCurrentContext());
                        UIImage img = UIGraphics.GetImageFromCurrentImageContext();
                        UIGraphics.EndImageContext();

                        UIImageView iv = new UIImageView(img);
                        dragAndDropView       = new UIView();
                        dragAndDropView.Frame = iv.Frame;
                        dragAndDropView.Add(iv);
                        //dragAndDropView.BackgroundColor = UIColor.Blue;
                        sourceTableView = tableView;

                        UIApplication.SharedApplication.KeyWindow.Add(dragAndDropView);

                        dragAndDropView.Center = recognizer.LocationInView(UIApplication.SharedApplication.KeyWindow);

                        dragAndDropView.AddGestureRecognizer(selectedCell.GestureRecognizers [0]);
                    }

                    break;

                case UIGestureRecognizerState.Changed:
                    dragAndDropView.Center = recognizer.LocationInView(UIApplication.SharedApplication.KeyWindow);
                    destinationIndexPath   = indexPath;
                    break;

                case UIGestureRecognizerState.Cancelled:
                case UIGestureRecognizerState.Failed:
                    sourceIndexPath           = null;
                    cell.View.BackgroundColor = Color.Transparent;
                    break;

                case UIGestureRecognizerState.Ended:

                    if (dragAndDropView == null)
                    {
                        return;
                    }

                    dragAndDropView.RemoveFromSuperview();
                    dragAndDropView = null;


                    UIView view = UIApplication.SharedApplication.KeyWindow;

                    UIView viewHit = view.HitTest(recognizer.LocationInView(view), null);

                    int removeLocation = (int)sourceIndexPath.Item;
                    int insertLocation = destinationIndexPath != null ? (int)destinationIndexPath.Item : -1;

                    UITableView destinationTableView = viewHit as UITableView;

                    if (viewHit is UITableView)
                    {
                        destinationTableView = viewHit as UITableView;
                    }
                    else
                    {
                        while (!(viewHit is UITableViewCell) && viewHit != null)
                        {
                            viewHit = viewHit.Superview;
                        }

                        UIView tempView = viewHit?.Superview;

                        while (!(tempView is UITableView) && tempView != null)
                        {
                            tempView = tempView.Superview;
                        }

                        if (tempView != null)
                        {
                            destinationTableView = tempView as UITableView;
                            insertLocation       = (int)destinationTableView.IndexPathForCell((UITableViewCell)viewHit).Item;
                        }
                    }

                    if (destinationTableView != null)
                    {
                        if (DragAndDropListViewRenderer.ListMap.ContainsKey(tableView.Tag.ToString()) && DragAndDropListViewRenderer.ListMap.ContainsKey(destinationTableView.Tag.ToString()))
                        {
                            var sourceList      = (IList)DragAndDropListViewRenderer.ListMap [tableView.Tag.ToString()].Item2;
                            var destinationList = (IList)DragAndDropListViewRenderer.ListMap [destinationTableView.Tag.ToString()].Item2;
                            if (!tableView.Tag.Equals(destinationTableView.Tag) || removeLocation != insertLocation)
                            {
                                if (sourceList.Contains(cell.BindingContext))
                                {
                                    sourceList.Remove(cell.BindingContext);

                                    if (insertLocation != -1)
                                    {
                                        destinationList.Insert(insertLocation, cell.BindingContext);
                                    }
                                    else
                                    {
                                        destinationList.Add(cell.BindingContext);
                                    }
                                }
                                tableView.ReloadData();
                                destinationTableView.ReloadData();
                            }
                        }
                    }


                    tableView.ScrollEnabled = true;

                    break;
                }
            }
        protected override void HandleLongPressGesture()
        {
            switch (LongPressGestureRecognizer.State)
            {
            case UIGestureRecognizerState.Began:
            {
                // we try to grab the current seelected item and draw a floating copy of it on the Drag Surface
                // the copy is just an image of it using RasterizedImage
                var currentIndexPath = CollectionView.IndexPathForItemAtPoint(LongPressGestureRecognizer.LocationInView(CollectionView));

                SelectedItemIndexPath = currentIndexPath;
                if (SelectedItemIndexPath == null)
                {
                    return;
                }

                if (!DataSource.CanMoveItemAtIndexPath(SelectedItemIndexPath))
                {
                    return;
                }

                var collectionViewCell = CollectionView.CellForItem(SelectedItemIndexPath);

                var        tpoint = DragSurface.ConvertPointFromView(collectionViewCell.Frame.Location, CollectionView);
                RectangleF frame  = new RectangleF(tpoint.X, tpoint.Y, collectionViewCell.Frame.Size.Width, collectionViewCell.Frame.Size.Height);
                CurrentView = new UIView(frame);

                collectionViewCell.Highlighted = true;
                var highlightedImageView = new UIImageView(RastertizedImage(collectionViewCell));
                highlightedImageView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
                highlightedImageView.Alpha            = 1.0f;

                collectionViewCell.Highlighted = false;
                var imageView = new UIImageView(RastertizedImage(collectionViewCell));
                imageView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
                imageView.Alpha            = 0.0f;

                CurrentView.AddSubview(imageView);
                CurrentView.AddSubview(highlightedImageView);
                DragSurface.AddSubview(CurrentView);         // add this to the top level view so that we can drag outside
                CurrentViewCenter = CurrentView.Center;

                OnWillBeginDraggingItem(SelectedItemIndexPath);

                // we animate the drawing out of the floating copy
                UIView.Animate(0.3, 0.0, UIViewAnimationOptions.BeginFromCurrentState,
                               () =>
                    {
                        CurrentView.Transform      = CGAffineTransform.MakeScale(1.1f, 1.1f);
                        highlightedImageView.Alpha = 0.0f;
                        imageView.Alpha            = 1.0f;
                    },
                               () =>
                    {
                        highlightedImageView.RemoveFromSuperview();
                        OnDidBegingDraggingItem(SelectedItemIndexPath);
                    });
                InvalidateLayout();
            }
            break;

            case UIGestureRecognizerState.Cancelled:
            case UIGestureRecognizerState.Ended:
            {
                var currentIndexPath = SelectedItemIndexPath;
                if (currentIndexPath == null || CurrentView == null)
                {
                    return;
                }
                SelectedItemIndexPath = null;
                CurrentViewCenter     = PointF.Empty;

                OnWillEndDraggingItem(currentIndexPath);

                UIView.Animate(0.3, 0.0, UIViewAnimationOptions.BeginFromCurrentState,
                               () =>
                    {
                        var layoutAttributes  = this.LayoutAttributesForItem(currentIndexPath);
                        CurrentView.Transform = CGAffineTransform.MakeScale(1.0f, 1.0f);
                        CurrentView.Center    = CollectionView.ConvertPointToView(layoutAttributes.Center, DragSurface);
                    },
                               () =>
                    {
                        CurrentView.RemoveFromSuperview();
                        CurrentView = null;
                        InvalidateLayout();

                        OnDidEndDraggingItem(currentIndexPath);
                    });
            }
            break;
            }
        }
Esempio n. 4
0
        protected override void HandleLongPressGesture()
        {
            switch (LongPressGestureRecognizer.State)
            {
            case UIGestureRecognizerState.Began:
            {
                // we get the current item and draw a floating copy on it on the Collection View, so that it cannot be taken outside the bounds of the collection view
                // we get the copy usign RasterizedImage
                var currentIndexPath = CollectionView.IndexPathForItemAtPoint(LongPressGestureRecognizer.LocationInView(CollectionView));
                SelectedItemIndexPath = currentIndexPath;
                if (SelectedItemIndexPath == null)
                {
                    return;
                }

                if (!DataSource.CanMoveItemAtIndexPath(SelectedItemIndexPath))
                {
                    return;
                }

                var collectionViewCell = CollectionView.CellForItem(SelectedItemIndexPath);
                CurrentView = new UIView(collectionViewCell.Frame);

                collectionViewCell.Highlighted = true;
                var highlightedImageView = new UIImageView(RastertizedImage(collectionViewCell));
                highlightedImageView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
                highlightedImageView.Alpha            = 1.0f;

                collectionViewCell.Highlighted = false;
                var imageView = new UIImageView(RastertizedImage(collectionViewCell));
                imageView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
                imageView.Alpha            = 0.0f;

                CurrentView.AddSubview(imageView);
                CurrentView.AddSubview(highlightedImageView);
                CollectionView.AddSubview(CurrentView);
                CurrentViewCenter = CurrentView.Center;

                OnWillBeginDraggingItem(SelectedItemIndexPath);

                // animate the floating copy into existence
                UIView.Animate(0.3, 0.0, UIViewAnimationOptions.BeginFromCurrentState,
                               () =>
                    {
                        CurrentView.Transform      = CGAffineTransform.MakeScale(1.1f, 1.1f);
                        highlightedImageView.Alpha = 0.0f;
                        imageView.Alpha            = 1.0f;
                    },
                               () =>
                    {
                        highlightedImageView.RemoveFromSuperview();
                        OnDidBegingDraggingItem(SelectedItemIndexPath);
                    });
                InvalidateLayout();
            }
            break;

            case UIGestureRecognizerState.Cancelled:
            case UIGestureRecognizerState.Ended:
            {
                var currentIndexPath = SelectedItemIndexPath;
                if (currentIndexPath == null)
                {
                    return;
                }
                SelectedItemIndexPath = null;
                CurrentViewCenter     = PointF.Empty;

                OnWillEndDraggingItem(currentIndexPath);

                UIView.Animate(0.3, 0.0, UIViewAnimationOptions.BeginFromCurrentState,
                               () =>
                    {
                        var layoutAttributes  = LayoutAttributesForItem(currentIndexPath);
                        CurrentView.Transform = CGAffineTransform.MakeScale(1.0f, 1.0f);
                        CurrentView.Center    = layoutAttributes.Center;
                    },
                               () =>
                    {
                        CurrentView.RemoveFromSuperview();
                        CurrentView = null;
                        InvalidateLayout();

                        OnDidEndDraggingItem(currentIndexPath);
                    });
            }
            break;
            }
        }
Esempio n. 5
0
 private static void OnRecognizing(LongPressGestureRecognizer recognizer, UITableView tableView, ViewCell cell)
 {
     NSIndexPath indexPath = tableView.IndexPathForRowAtPoint(recognizer.LocationInView(tableView));
     switch (recognizer.State)
     {
         case UIGestureRecognizerState.Began:
             if (indexPath != null)
             {
                 // Remember the source row
                 recognizer.sourceIndexPath = indexPath;
                 recognizer.destinationIndexPath = indexPath;
                 cell.View.BackgroundColor = Color.Gray;
             }
             break;
         case UIGestureRecognizerState.Changed:
             if (recognizer.destinationIndexPath != null && indexPath != null && recognizer.destinationIndexPath != indexPath)
             {
                 // Dragged to a new row location, so show it to the user with animation
                 tableView.MoveRow(recognizer.destinationIndexPath, indexPath);
                 recognizer.destinationIndexPath = indexPath;
             }
             break;
         case UIGestureRecognizerState.Cancelled:
         case UIGestureRecognizerState.Failed:
             recognizer.sourceIndexPath = null;
             cell.View.BackgroundColor = Color.Transparent;
             break;
         case UIGestureRecognizerState.Recognized:
             // Move the data source finally
             if (recognizer.sourceIndexPath != null && recognizer.destinationIndexPath != null && recognizer.sourceIndexPath != recognizer.destinationIndexPath)
             {
                 // Reset the move because otherwise the underneath control will get out of sync with
                 // the Xamarin.Forms element. The next line will drive the real change from ItemsSource
                 tableView.MoveRow(recognizer.destinationIndexPath, recognizer.sourceIndexPath);
                 tableView.Source.MoveRow(tableView, recognizer.sourceIndexPath, recognizer.destinationIndexPath);
             }
             recognizer.sourceIndexPath = null;
             recognizer.destinationIndexPath = null;
             cell.View.BackgroundColor = Color.Transparent;
             break;
     }
 }
            private static void OnRecognizing(LongPressGestureRecognizer recognizer, UITableView tableView, ViewCell cell)
            {
                try
                {
                    NSIndexPath indexPath = tableView.IndexPathForRowAtPoint(recognizer.LocationInView(tableView));

                    Items = AppData.PropertyModel.SelectedAction.Action.Paragraphs;
                    if (indexPath != null)
                    {
                        if (VisitActionDetailsPage.CurrentInstance.IsEditable)
                        {
                            if (!VisitActionDetailsPage.CurrentInstance.SingleTypeCode.Contains(Items[(int)indexPath.LongRow].ParagraphType))
                            {
                                switch (recognizer.State)
                                {
                                case UIGestureRecognizerState.Began:
                                    tableView.ScrollEnabled = false;
                                    if (indexPath != null)
                                    {
                                        // Remember the source row
                                        sourceIndexPath      = indexPath;
                                        destinationIndexPath = indexPath;
                                        var selectedCell = tableView.CellAt(indexPath);
                                        UIGraphics.BeginImageContext(selectedCell.ContentView.Bounds.Size);
                                        selectedCell.ContentView.Layer.RenderInContext(UIGraphics.GetCurrentContext());
                                        UIImage img = UIGraphics.GetImageFromCurrentImageContext();
                                        UIGraphics.EndImageContext();


                                        UIImageView iv = new UIImageView(img);
                                        dragAndDropView       = new UIView();
                                        dragAndDropView.Frame = iv.Frame;
                                        dragAndDropView.Add(iv);
                                        //dragAndDropView.BackgroundColor = UIColor.Blue;
                                        sourceTableView = tableView;

                                        UIApplication.SharedApplication.KeyWindow.Add(dragAndDropView);

                                        dragAndDropView.Center = recognizer.LocationInView(UIApplication.SharedApplication.KeyWindow);

                                        dragAndDropView.AddGestureRecognizer(selectedCell.GestureRecognizers[0]);
                                    }

                                    break;

                                case UIGestureRecognizerState.Changed:
                                    dragAndDropView.Center = recognizer.LocationInView(UIApplication.SharedApplication.KeyWindow);
                                    destinationIndexPath   = indexPath;
                                    break;

                                case UIGestureRecognizerState.Cancelled:
                                case UIGestureRecognizerState.Failed:
                                    sourceIndexPath           = null;
                                    cell.View.BackgroundColor = Color.Transparent;
                                    break;

                                case UIGestureRecognizerState.Ended:

                                    if (dragAndDropView == null)
                                    {
                                        return;
                                    }

                                    dragAndDropView.RemoveFromSuperview();
                                    dragAndDropView = null;

                                    UIView view = UIApplication.SharedApplication.KeyWindow;

                                    UIView viewHit = view.HitTest(recognizer.LocationInView(view), null);

                                    int removeLocation = (int)sourceIndexPath.Item;
                                    int insertLocation = destinationIndexPath != null ? (int)destinationIndexPath.Item : -1;

                                    while (!(viewHit is UITableViewCell) && viewHit != null)
                                    {
                                        viewHit = viewHit.Superview;
                                    }

                                    UIView tempView = viewHit?.Superview;

                                    while (!(tempView is UITableView) && tempView != null)
                                    {
                                        tempView = tempView.Superview;
                                    }

                                    insertLocation = (int)tableView.IndexPathForCell((UITableViewCell)viewHit).Item;

                                    if (CustomTableViewRenderer.ListMap.ContainsKey(tableView.Tag.ToString()))
                                    {
                                        var sourceList = (IList)CustomTableViewRenderer.ListMap[tableView.Tag.ToString()].Item2;

                                        if (!tableView.Tag.Equals(tableView.Tag) || removeLocation != insertLocation)
                                        {
                                            if (Items[removeLocation].ParagraphType == Items[insertLocation].ParagraphType)
                                            {
                                                if (sourceList.Contains(cell.BindingContext))
                                                {
                                                    sourceList.Remove(cell.BindingContext);

                                                    if (insertLocation != -1)
                                                    {
                                                        sourceList.Insert(insertLocation, cell.BindingContext);
                                                    }
                                                    else
                                                    {
                                                        sourceList.Add(cell.BindingContext);
                                                    }
                                                }
                                                tableView.ReloadData();
                                            }
                                        }
                                    }

                                    VisitActionDetailsPage.CurrentInstance.RefreshList();
                                    tableView.ScrollEnabled = true;

                                    break;
                                }
                            }
                            else
                            {
                                dragAndDropView.RemoveFromSuperview();
                                VisitActionDetailsPage.CurrentInstance.RefreshList();
                            }
                        }
                    }
                    else
                    {
                        dragAndDropView.RemoveFromSuperview();
                        VisitActionDetailsPage.CurrentInstance.RefreshList();
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("==={0}===", ex.Message);
                }
            }