重新排序 CollectionView 不工作在 cdoe 后面购买添加视图
Reorder CollectionView is not working buy adding view in cdoe behind
我尝试添加我的 UICollectionView,但 MoveItem 不工作
我的移动事件没有调用。我不确定,但我尝试在故事情节中添加它可以工作,但是当我通过在空 UIview 中添加视图来尝试在代码后面时。
这是我的 UICollectionViewDataSource
public override bool CanMoveItem(UICollectionView collectionView, NSIndexPath indexPath) {
// We can always move items
Console.WriteLine("Is Move");
return true;
}
public override void MoveItem(UICollectionView collectionView, NSIndexPath sourceIndexPath, NSIndexPath destinationIndexPath) {
// Reorder our list of items
Console.WriteLine("Item move");
var item = Numbers[(int) sourceIndexPath.Item];
Numbers.RemoveAt((int) sourceIndexPath.Item);
Numbers.Insert((int) destinationIndexPath.Item, item);
}
我的 UICollectionView class:
#region Computed Properties
public WaterfallCollectionSource Source {
get {
return (WaterfallCollectionSource) DataSource;
}
}#
endregion
# region Constructors
public WaterfallCollectionView(IntPtr handle): base(handle) {}#
endregion
# region Constructors
public WaterfallCollectionView(CGRect frame, UICollectionViewLayout layout): base(frame, layout) {}#
endregion
public override void AwakeFromNib() {
base.AwakeFromNib();
// Initialize
DataSource = new WaterfallCollectionSource(this);
Delegate = new WaterfallCollectionDelegate(this);
}
我的 UIViewController
WaterfallCollectionView Waterfall;
public ViewController(IntPtr handle): base(handle) {}
public override void ViewDidLoad() {
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
var flowLayout = new UICollectionViewFlowLayout() {
ItemSize = new CGSize((float) UIScreen.MainScreen.Bounds.Size.Width - 20.0 f, 50.0 f),
};
var uICollectionView = new CGRect(0, 0, (int) UIScreen.MainScreen.Bounds.Size.Width, (int) UIScreen.MainScreen.Bounds.Size.Height);
Waterfall = new WaterfallCollectionView(uICollectionView, flowLayout);
Waterfall.DataSource = new WaterfallCollectionSource(Waterfall);
Waterfall.Delegate = new WaterfallCollectionDelegate(Waterfall);
MainView.AddSubview(Waterfall);
var log = new UILongPressGestureRecognizer();
}
欢迎来到 SO!
来自共享代码,没有看到为 uICollectionView
添加 UILongPressGestureRecognizer
。 MoveItem
方法不能自动调用,需要UILongPressGestureRecognizer
.
调用
例如,为CollectionView添加长按方法如下:
UILongPressGestureRecognizer uILongPressGesture = new UILongPressGestureRecognizer(longPressGesture);
collectionView.AddGestureRecognizer(uILongPressGesture );
那么实现的longPressGesture
方法就是:
private void longPressGesture(UILongPressGestureRecognizer longPressGesture)
{
switch (longPressGesture.State)
{
case UIGestureRecognizerState.Began:
{
Console.WriteLine("long press start");
CGPoint touchPoint = longPressGesture.LocationInView(collectionView);
NSIndexPath selectedIndexPath = collectionView.IndexPathForItemAtPoint(touchPoint);
if (null != selectedIndexPath)
{
collectionView.BeginInteractiveMovementForItem(selectedIndexPath);
}
}
break;
case UIGestureRecognizerState.Changed:
{
Console.WriteLine("long press changed");
CGPoint touchPoint = longPressGesture.LocationInView(collectionView);
collectionView.UpdateInteractiveMovement(touchPoint);
}
break;
case UIGestureRecognizerState.Ended:
{
Console.WriteLine("long press end");
collectionView.EndInteractiveMovement();
break;
}
default:
{
collectionView.CancelInteractiveMovement();
break;
}
}
}
效果:
我尝试添加我的 UICollectionView,但 MoveItem 不工作 我的移动事件没有调用。我不确定,但我尝试在故事情节中添加它可以工作,但是当我通过在空 UIview 中添加视图来尝试在代码后面时。
这是我的 UICollectionViewDataSource
public override bool CanMoveItem(UICollectionView collectionView, NSIndexPath indexPath) {
// We can always move items
Console.WriteLine("Is Move");
return true;
}
public override void MoveItem(UICollectionView collectionView, NSIndexPath sourceIndexPath, NSIndexPath destinationIndexPath) {
// Reorder our list of items
Console.WriteLine("Item move");
var item = Numbers[(int) sourceIndexPath.Item];
Numbers.RemoveAt((int) sourceIndexPath.Item);
Numbers.Insert((int) destinationIndexPath.Item, item);
}
我的 UICollectionView class:
#region Computed Properties
public WaterfallCollectionSource Source {
get {
return (WaterfallCollectionSource) DataSource;
}
}#
endregion
# region Constructors
public WaterfallCollectionView(IntPtr handle): base(handle) {}#
endregion
# region Constructors
public WaterfallCollectionView(CGRect frame, UICollectionViewLayout layout): base(frame, layout) {}#
endregion
public override void AwakeFromNib() {
base.AwakeFromNib();
// Initialize
DataSource = new WaterfallCollectionSource(this);
Delegate = new WaterfallCollectionDelegate(this);
}
我的 UIViewController
WaterfallCollectionView Waterfall;
public ViewController(IntPtr handle): base(handle) {}
public override void ViewDidLoad() {
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
var flowLayout = new UICollectionViewFlowLayout() {
ItemSize = new CGSize((float) UIScreen.MainScreen.Bounds.Size.Width - 20.0 f, 50.0 f),
};
var uICollectionView = new CGRect(0, 0, (int) UIScreen.MainScreen.Bounds.Size.Width, (int) UIScreen.MainScreen.Bounds.Size.Height);
Waterfall = new WaterfallCollectionView(uICollectionView, flowLayout);
Waterfall.DataSource = new WaterfallCollectionSource(Waterfall);
Waterfall.Delegate = new WaterfallCollectionDelegate(Waterfall);
MainView.AddSubview(Waterfall);
var log = new UILongPressGestureRecognizer();
}
欢迎来到 SO!
来自共享代码,没有看到为 uICollectionView
添加 UILongPressGestureRecognizer
。 MoveItem
方法不能自动调用,需要UILongPressGestureRecognizer
.
例如,为CollectionView添加长按方法如下:
UILongPressGestureRecognizer uILongPressGesture = new UILongPressGestureRecognizer(longPressGesture);
collectionView.AddGestureRecognizer(uILongPressGesture );
那么实现的longPressGesture
方法就是:
private void longPressGesture(UILongPressGestureRecognizer longPressGesture)
{
switch (longPressGesture.State)
{
case UIGestureRecognizerState.Began:
{
Console.WriteLine("long press start");
CGPoint touchPoint = longPressGesture.LocationInView(collectionView);
NSIndexPath selectedIndexPath = collectionView.IndexPathForItemAtPoint(touchPoint);
if (null != selectedIndexPath)
{
collectionView.BeginInteractiveMovementForItem(selectedIndexPath);
}
}
break;
case UIGestureRecognizerState.Changed:
{
Console.WriteLine("long press changed");
CGPoint touchPoint = longPressGesture.LocationInView(collectionView);
collectionView.UpdateInteractiveMovement(touchPoint);
}
break;
case UIGestureRecognizerState.Ended:
{
Console.WriteLine("long press end");
collectionView.EndInteractiveMovement();
break;
}
default:
{
collectionView.CancelInteractiveMovement();
break;
}
}
}
效果: