UICollectionView“滑动".在iOS7应用管理器中? [英] The UICollectionView "swipe-away" in iOS7 app manager?

查看:90
本文介绍了UICollectionView“滑动".在iOS7应用管理器中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在任何2014+版本的iPhone或iPad上,双击主屏幕按钮以查看应用程序管理器"

On any 2014+ iPhone or iPad, double-click the home button to see the "app manager"

这是一个左右UICollectionView但它具有滑动"手势..向上滑动.怎么做?从UICollectionView中删除"单元格不是那么容易.

This is a left-right UICollectionView BUT it has a "swipe-away" gesture .. swipe up. How is it done? It's not so easy to "remove" a cell from a UICollectionView.

针对Google员工的脚注..针对剥离",撕裂"这一普遍问题,是集合视图中的一个单元格,这是一个整洁的解释:https://stackoverflow.com/a/24339705/294884 希望对您有所帮助.

Footnote for googlers .. for the general problem of "peeling off", "tearing away", one cell from a collection view, here's a full tidy explanation: https://stackoverflow.com/a/24339705/294884 Hope it helps someone.

推荐答案

比对您的问题的建议要简单得多.

It can be much simpler than the comments on your question are suggesting.

您的单元格应包含一个视图(您将要拖动的东西),然后向该视图添加UIPanGestureRecognizer.

Your cell should contain a view (the thing that you're going to drag off) and you add a UIPanGestureRecognizer to that view.

在手势的动作方法中,向上或向下移动视图,当视图距离足够远而您想要删除它时,只需对其进行动画处理即可.这里有很多有关此部分的问题.

In the gesture's action method, you move the view up or down, and when it gets far enough off that you want to delete it, you just animate it off. There are plenty of questions here dealing with this part.

这在您的收藏中留下了空白,现在您需要四处移动.事实证明,这很简单:

This leaves a gap in your collection and now you need to move things around. It turns out this is quite simple:

[_collectionView performBatchUpdates:^{
   [_collectionView deleteItemsAtIndexPaths:@[indexPath]];
} completion:^(BOOL finished) {
     // you might want to remove the data from the data source here so the view doesn't come back to life when the collection view is reloaded.
}];

已删除单元格右边的东西滑过,我们都很好.

The stuff to the right of the removed cell slides over and we're all good.

要克服的另一个问题:确保手势识别器和集合视图的结合在一起.值得庆幸的是,这也不是很棘手.

Another problem to get over: making sure your gesture recognizer and the collection view's one play nice together. Thankfully, that's not too tricky either.

[_collectionView.panGestureRecognizer requireGestureRecognizerToFail:pgr]; //where pgr is the recognizer you made for dragging the view off

这意味着要使集合视图的平移手势起作用,您的视图必须失败.因此,您需要进行设置,以使其仅在上下左右平移时才起作用,并且让collection视图仍然可以左右左右平移.在手势识别器的代表中,实现以下方法,该方法仅检查您是否在x轴或y轴上移动得更多.

This means in order for the collection view's pan gesture to do its thing, your one has to fail. So you'll want to set yours up so that it only works when panning up and down, and let the collection view still do its thing for left to right pans. In your gesture recognizers's delegate, implement the following method which simply checks if you're moving more on the x-axis or y-axis.

-(BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer
{
    CGPoint translation =[gestureRecognizer translationInView:self.view];

    return(translation.x * translation.x > translation.y * translation.y);
}

这篇关于UICollectionView“滑动".在iOS7应用管理器中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆