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;
}-
各個(gè)長按狀態(tài)的響應(yīng)事件
2.1 長按手勢開始
/// <summary>
/// 長按手勢開始
/// </summary>
void GestureBegan()
{
// 得到當(dāng)前長按的點(diǎn)相對于長按手勢載體的位置
CGPoint location = LongPress.LocationInView(LongPress.View);
// 根據(jù)位置得到IndexPath
NSIndexPath indexPath = TableView.IndexPathForRowAtPoint(location);
// 根據(jù)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()
{
// 得到當(dāng)前長按的點(diǎn)相對于長按手勢載體的位置
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 長按手勢結(jié)束
/// <summary>
/// 長按手勢結(jié)束
/// </summary>
void GestureEnded()
{
UIView.Animate(0.15, new Action(() =>
{
// 此處使用sourceIndex费坊,也就是Changed中變化后的位置茅逮,如果重新取值有可能取到下一個(gè)cell,導(dǎo)致移動的cell沒有恢復(fù)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;
}));
}
開啟滾動事件
void StartScroll()
{
scrollTimer = CADisplayLink.Create(ProcessScroll);
scrollTimer.AddToRunLoop(NSRunLoop.Main, NSRunLoopMode.Common);
}-
滾動事件
/// <summary>
/// 邊緣滾動事件
/// </summary>
void ProcessScroll()
{
// 觸發(fā)長按變化事件
GestureChanged();// 設(shè)置向上移動時(shí)培己,TableView開始滑動的邊界 nfloat minOffsetY = tableView.ContentOffset.Y + edgeScrollRange; // 設(shè)置向下移動時(shí),TableView開始滑動的邊界 nfloat maxOffsetY = tableView.ContentOffset.Y + tableView.Frame.Height - edgeScrollRange; // 得到截圖的當(dāng)前位置 CGPoint touchPoint = snapShotView.Center; // 截圖位置達(dá)到向上的滾動條件实苞,但是沒有可以滾動的內(nèi)容 if (touchPoint.Y < edgeScrollRange && tableView.ContentOffset.Y <= 0) return; // 截圖位置滿足向下的滾動條件剪廉,但是沒有可以滾動的內(nèi)容 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); } }
文章根據(jù) http://www.reibang.com/p/ce382f9bc794 這里的思路來寫窖剑,整體思路一樣,但是實(shí)現(xiàn)上有些不一樣的地方戈稿。