Xamarin:UICollection图像重新sorting问题
我正在使用UICollectionView来存储图像,我可以通过重写CanMove
和MoveItem
来重新排列它们。
但是UICollection里面的项目只有在单元大小很大的时候才会重新sorting,就像单元格大小在106左右的高度和宽度一样,如果它们的大小较小,它们可以重新sorting,它们不能被重新sorting。
视图:
public override void ViewDidLoad() { base.ViewDidLoad(); //ImageCv is the name of UiCollectionView var collectionLayout = new PostImageFlowLayout(3, 0.85f); var allCollectionSource = new PostImageColectionSource(ImageCv, (ViewModel as NewPostDetailViewModel)); ImageCv.RegisterNibForCell(PostImageCell.Nib, PostImageCell.Key); ImageCv.RegisterClassForSupplementaryView(typeof(CollectionHeader), UICollectionElementKindSection.Header, new NSString("headerId")); ImageCv.BackgroundColor = UIColor.Clear; ImageCv.Hidden = false; ImageCv.DataSource = allCollectionSource; ImageCv.Delegate = collectionLayout; var longPressGesture = new UILongPressGestureRecognizer(gesture => { // Take action based on state switch (gesture.State) { case UIGestureRecognizerState.Began: var selectedIndexPath = ImageCv.IndexPathForItemAtPoint(gesture.LocationInView(View)); if (selectedIndexPath != null) ImageCv.BeginInteractiveMovementForItem(selectedIndexPath); Debug.WriteLine("Gesture Recognition: Activated"); break; case UIGestureRecognizerState.Changed: ImageCv.UpdateInteractiveMovement(gesture.LocationInView(View)); Debug.WriteLine("Gesture activated: Item location is changed"); break; case UIGestureRecognizerState.Ended: ImageCv.EndInteractiveMovement(); Debug.WriteLine("Gesture activation: complete"); break; default: ImageCv.CancelInteractiveMovement(); Debug.WriteLine("Gesture activation: Terminate"); break; } }); // Add the custom recognizer to the collection view ImageCv.AddGestureRecognizer(longPressGesture); }
使用System的UICollectionViewDelegateFlowLayout; 使用System.Windows.Input; 使用CoreGraphics; 使用UIKit;
namespace Sources.CollectionSources { public class PostImageFlowLayout : UICollectionViewDelegateFlowLayout { private float headerHeight; private int noOfItems; private bool isLoading; public PostImageFlowLayout(int noOfItems, float headerHeight = 0f) { this.noOfItems = noOfItems; this.headerHeight = headerHeight; } public override CGSize GetSizeForItem(UICollectionView collectionView, UICollectionViewLayout layout, Foundation.NSIndexPath indexPath) { return GetPostCellSize(); } public override CGSize GetReferenceSizeForHeader(UICollectionView collectionView, UICollectionViewLayout layout, nint section) { return new CGSize(collectionView.Frame.Width, headerHeight); } public override UIEdgeInsets GetInsetForSection(UICollectionView collectionView, UICollectionViewLayout layout, nint section) { return new UIEdgeInsets(0, 0, 0, 0); } private CGSize GetPostCellSize() { var relativeWidth = (UIScreen.MainScreen.Bounds.Width - 2) / this.noOfItems; return new CGSize(relativeWidth, relativeWidth); //return new CGSize(55, 55); } } }
资源
public class PostImageColectionSource : MvxCollectionViewSource { private NewPostDetailViewModel newPostDetailViewModel; private string type; static NSString animalCellId = new NSString("PostImageCell"); static NSString headerId = new NSString("Header"); List<IAnimal> animals; protected override NSString DefaultCellIdentifier { get { return PostImageCell.Key; } } public override System.Collections.IEnumerable ItemsSource { get { return base.ItemsSource; } set { base.ItemsSource = value; CollectionView.ReloadData(); } } public PostImageColectionSource(UICollectionView collectionView, NewPostDetailViewModel newPostDetailViewModel) : base(collectionView) { this.newPostDetailViewModel = newPostDetailViewModel; animals = new List<IAnimal>(); for (int i = 0; i < 20; i++) { animals.Add(new Monkey(i)); } } public override nint NumberOfSections(UICollectionView collectionView) { return 1; } public override nint GetItemsCount(UICollectionView collectionView, nint section) { return 5;// animals.Count; } public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath) { var cell = (PostImageCell)collectionView.DequeueReusableCell(animalCellId, indexPath); var animal = animals[indexPath.Row]; cell.Result(indexPath.Row); return cell; } public override bool CanMoveItem(UICollectionView collectionView, NSIndexPath indexPath) { Debug.WriteLine("Ready to move images"); //System.Diagnostics.Debug.WriteLine("Checking if it can move the item"); return true; } public override void MoveItem(UICollectionView collectionView, NSIndexPath sourceIndexPath, NSIndexPath destinationIndexPath) { //base.MoveItem(collectionView, sourceIndexPath, destinationIndexPath); Debug.WriteLine("Start moving images to reorder"); var item = animals[(int)sourceIndexPath.Item]; animals.RemoveAt((int)sourceIndexPath.Item); animals.Insert((int)destinationIndexPath.Item, item); } }
当PostImageFlowLayout中的GetPostCellSize的宽度和高度大约为100时, PostImageColectionSource
中的CanMove
和MoveItem
被调用,并且项目正在被重新sorting。 但是,如果GetPostCellSize
宽度和高度在50或70左右,即使手势被激活, PostImageColectionSource
中的CanMove
和MoveItem
也不会被调用,因此无法移动。
任何人都可以希望我重新sortingUICollectionView中的图像,当单元格的大小是像70左右的宽度和高度。
谢谢。
我正在迅速和客观地标记C,因为这个问题一般与IOS相关,而不是xamarin特定的
这里的主要问题是,您需要将集合视图传递给gesture.LocationInView(View)调用,而不是主视图。 在ViewDidLoad
的ViewDidLoad中更改:
var selectedIndexPath = ImageCv.IndexPathForItemAtPoint(gesture.LocationInView(View));
和
ImageCv.UpdateInteractiveMovement(gesture.LocationInView(View));
至
var selectedIndexPath = ImageCv.IndexPathForItemAtPoint(gesture.LocationInView(ImageCv)); // <-- pass in ImageCV instead of View. (where ImageCV is the collection view)
和
ImageCv.UpdateInteractiveMovement(gesture.LocationInView(ImageCv)); // <-- pass in ImageCV instead of View.
另外需要注意的是, PostImageColectionSource
最终来自UICollectionViewSource
,它是UICollectionViewDelegate
和UICollectionViewDataSource
在一个类中的组合,但是被分配到集合视图的DataSource
属性。 所有这一切的意思是,虽然你可以在PostImageColectionSource
实现UICollectionViewDelegate
方法, UICollectionViewDelegate
由于集合视图的Delegate
属性被设置为PostImageFlowLayout
,所以最终从UICollectionViewDelegate
通过UICollectionViewDelegateFlowLayout
派生,委托方法不会被调用。
- 使用UISearchBarsearch表格视图
- CollectionViewController.reloadData()与来自Parse Server的数据
- 苹果推送通知与定期蜂鸣声
- Autoresizingmask立即覆盖最初设置的框架(创build一个不同的和错误的布局)
- Cocoa-Touch:如何查看两个NSDate是否在同一天?
- 以编程方式检测iPad是否具有Retina显示屏?
- Swift – 如何将自定义字段添加到Google Maps SDK中的标记?
- 将静态库目标转换为Xcode项目中的框架目标
- 使用AppDelegate的Storyboard以编程方式实例化View Controller