Xamarin - UITableView 長按拖動Cell

  1. UITableView 添加長按事件
    LongPress = new UILongPressGestureRecognizer(new Action(() => { Cell_OnMove(); }));
    tableView.AddGestureRecognizer(LongPress);
    其中Cell_OnMove中代碼如下:
    switch (LongPress.State)
    {
    case UIGestureRecognizerState.Began:
    GestureBegan();
    break;
    case UIGestureRecognizerState.Changed:
    //ProcessScroll();
    break;
    case UIGestureRecognizerState.Ended:
    GestureEnded();
    break;
    }

  2. 各個長按狀態(tài)的響應事件
    2.1 長按手勢開始
    /// <summary>
    /// 長按手勢開始
    /// </summary>
    void GestureBegan()
    {
    // 得到當前長按的點相對于長按手勢載體的位置
    CGPoint location = LongPress.LocationInView(LongPress.View);
    // 根據位置得到IndexPath
    NSIndexPath indexPath = TableView.IndexPathForRowAtPoint(location);
    // 根據IndexPath得到Cell
    var cell = TableView.CellAt(indexPath) as OptionEditCell;
    // 記錄長按開始的位置
    sourceIndex = NSIndexPath.FromRowSection(indexPath.Row, 0);
    // 得到cell的移動截圖
    snapShotView = GetMoveCellImage(cell);
    // 開啟邊緣滑動
    StartScroll();
    CGPoint center = cell.Center;
    snapShotView.Center = center;
    snapShotView.Alpha = 0;
    TableView.AddSubview(snapShotView);
    UIView.Animate(0.1, new Action(() =>
    {
    center.Y = location.Y;
    snapShotView.Center = center;
    snapShotView.Transform = CGAffineTransform.MakeScale(1f, 1f);
    snapShotView.Alpha = 1f;
    cell.Alpha = 0;
    }));
    }
    2.2 長按手勢變化
    /// <summary>
    /// 長按手勢變化
    /// </summary>
    void GestureChanged()
    {
    // 得到當前長按的點相對于長按手勢載體的位置
    CGPoint location = LongPress.LocationInView(LongPress.View);

         // 移動位置超出上邊界則等于上邊界
         if (location.Y <= 0) location.Y = 0;
         // 移動位置超出下邊界則等于下邊界
         if (location.Y >= TableView.ContentSize.Height - 1) location.Y = TableView.ContentSize.Height - 1;
    
         NSIndexPath indexPath = TableView.IndexPathForRowAtPoint(location);
         var cell = TableView.CellAt(indexPath) as OptionEditCell;
         if (cell == null) return;
         CGPoint cen = snapShotView.Center;
         cen.Y = location.Y;
         snapShotView.Center = cen;
         cell.Alpha = 0;
         if (indexPath != null && !indexPath.Equals(sourceIndex))
         {
             // 移動行,是界面有滑動填充的效果
             Source_OnItemMoved(sourceIndex.Row, indexPath.Row);
             // 記錄cell的新位置
             sourceIndex = indexPath;
         }
     }
    

2.3 長按手勢結束
/// <summary>
/// 長按手勢結束
/// </summary>
void GestureEnded()
{
UIView.Animate(0.15, new Action(() =>
{
// 此處使用sourceIndex,也就是Changed中變化后的位置,如果重新取值有可能取到下一個cell,導致移動的cell沒有恢復alpha值
var sourceCell = TableView.CellAt(sourceIndex) as OptionEditCell;
sourceCell.Alpha = 1;
snapShotView.Center = sourceCell.Center;
snapShotView.Transform = CGAffineTransform.MakeIdentity();
snapShotView.Alpha = 0;

            // 停止scrollTimer
            scrollTimer.RemoveFromRunLoop(NSRunLoop.Main, NSRunLoopMode.Common);
        }), new Action(() =>
        {
            // 移除snapShotView
            snapShotView.RemoveFromSuperview();
            snapShotView = null;
        }));
    }
  1. 開啟滾動事件
    void StartScroll()
    {
    scrollTimer = CADisplayLink.Create(ProcessScroll);
    scrollTimer.AddToRunLoop(NSRunLoop.Main, NSRunLoopMode.Common);
    }

  2. 滾動事件
    /// <summary>
    /// 邊緣滾動事件
    /// </summary>
    void ProcessScroll()
    {
    // 觸發(fā)長按變化事件
    GestureChanged();

         // 設置向上移動時,TableView開始滑動的邊界
         nfloat minOffsetY = tableView.ContentOffset.Y + edgeScrollRange;
         // 設置向下移動時,TableView開始滑動的邊界
         nfloat maxOffsetY = tableView.ContentOffset.Y + tableView.Frame.Height - edgeScrollRange;
         // 得到截圖的當前位置
         CGPoint touchPoint = snapShotView.Center;
    
         // 截圖位置達到向上的滾動條件,但是沒有可以滾動的內容
         if (touchPoint.Y < edgeScrollRange && tableView.ContentOffset.Y <= 0) return;
         // 截圖位置滿足向下的滾動條件,但是沒有可以滾動的內容
         if ((touchPoint.Y > tableView.ContentSize.Height - edgeScrollRange) &&
            (tableView.ContentOffset.Y >= tableView.ContentSize.Height - tableView.Frame.Height)) return;
    
         nfloat maxMoveDistance = 10f;
         if (touchPoint.Y < minOffsetY)
         {
             nfloat moveDistance = (minOffsetY - touchPoint.Y) / edgeScrollRange * maxMoveDistance;
             // 確保停止后,TableView頂部不出現(xiàn)空白
             if (tableView.ContentOffset.Y - moveDistance <= 0)
                 moveDistance = tableView.ContentOffset.Y;
             tableView.SetContentOffset(new CGPoint(tableView.ContentOffset.X, tableView.ContentOffset.Y - moveDistance), false);
             snapShotView.Center = new CGPoint(snapShotView.Center.X, snapShotView.Center.Y - moveDistance);
         }
         else if(touchPoint.Y > maxOffsetY)
         {
             nfloat moveDistance = (touchPoint.Y - maxOffsetY) / edgeScrollRange * maxMoveDistance;
             // 確保停止后,TableView底部不出現(xiàn)空白
             if (tableView.ContentOffset.Y + moveDistance >= tableView.ContentSize.Height - tableView.Frame.Height)
                 moveDistance = tableView.ContentSize.Height - tableView.Frame.Height - tableView.ContentOffset.Y;
             tableView.SetContentOffset(new CGPoint(tableView.ContentOffset.X, tableView.ContentOffset.Y + moveDistance), false);
             snapShotView.Center = new CGPoint(snapShotView.Center.X, snapShotView.Center.Y + moveDistance);
         }
     }
    

文章根據 http://www.lxweimin.com/p/ce382f9bc794 這里的思路來寫,整體思路一樣,但是實現(xiàn)上有些不一樣的地方。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容