0

我们有一个使用自定义 MvxCollectionViewSource 作为源的 UICollectionView。选择项目时,我们想滚动到项目,做一个简短的动画,然后做一个操作。在源中,ItemSelected 被覆盖如下,其中为 ScrollAnimationEnded 设置了一个事件。

public override void ItemSelected(UICollectionView collectionView, NSIndexPath indexPath)
        {
            var item = GetItemAt(indexPath) as SlideMenuViewItemModel;

            var cell = collectionView.VisibleCells[0];
            var index = collectionView.IndexPathForCell(cell);

            if (!Equals(index, indexPath))
            {
                collectionView.ScrollAnimationEnded += HandleAnimationDone;

                collectionView.ScrollToItem(indexPath, UICollectionViewScrollPosition.CenteredHorizontally, true);   
            }
            else
            {
                var layout = (SlideMenuLayout)collectionView.CollectionViewLayout;
                layout.SelectedIndexPath = indexPath;
                collectionView.PerformBatchUpdates(delegate { }, delegate
                {
                    item.OnClick.Execute(null);
                    layout.SelectedIndexPath = null;

                });

            }
        }

        private void HandleAnimationDone(object sender, EventArgs args)
        {
            CollectionView.ScrollAnimationEnded -= HandleAnimationDone;
            var layout = (SlideMenuLayout)CollectionView.CollectionViewLayout;

            CollectionView.PerformBatchUpdates(delegate { }, delegate
            {
            //    item.OnClick.Execute(null);
                layout.SelectedIndexPath = null;
            });
        }

问题是它只在第一次工作。选择项目并执行滚动并触发事件。但是,当尝试进行下一个选择时,永远不会调用 ItemSelected。如果我删除行 collectionView.ScrollAnimationEnded += HandleAnimationDone 它会继续工作。所以似乎连接事件会破坏一些东西,或者我需要调用一些东西来重置。

我希望你有一个建议:)

问候

4

1 回答 1

0

我还没有找到解决方案,因为似乎设置事件会切断其他东西。但是我做了一个解决方法,而不是使用 ScrollToItem 和设置动画结束事件,我做了一个自定义滚动动画。下面显示了我是如何做到的。

public override void ItemSelected(UICollectionView collectionView, NSIndexPath indexPath)
    {
        var item = GetItemAt(indexPath) as SlideMenuViewItemModel;

        var cell = GetCell (collectionView, indexPath);

        CurrentIndex = indexPath;
        UIView.Animate (0.5, 0, UIViewAnimationOptions.CurveEaseIn,
                        () => {
            // do any animation
        },
        () => {
            //do anything after the animation
        }
        );
    }
于 2013-10-30T07:08:41.003 回答