【译】UICollectionView 轻松重排

【译】UICollectionView 轻松重排,第1张

概述本文原链:【译】UICollectionView 轻松重排 原文链接:UICollectionViews Now Have Easy Reordering 原本打算总结一下 UICollectionView 的一些用法,看到一篇比较好的文章,所以直接翻译了。翻译得比较生硬,见谅。 我超喜欢UICollectionView。相比UITableView,它容易自定义得多。现在我使用甚至使用 colle

本文原链:【译】UICollectionView 轻松重排
原文链接:UICollectionViews Now Have Easy Reordering

原本打算总结一下 UICollectionVIEw 的一些用法,看到一篇比较好的文章,所以直接翻译了。翻译得比较生硬,见谅。

我超喜欢UICollectionVIEw。相比UItableVIEw,它容易自定义得多。现在我使用甚至使用 collection vIEw 比使用 table vIEw 还要频繁了。在 iOS9 中,它开始支持使用起来很简单的重排。在之前是不可能直接重排的,而且实现起来很麻烦。让我们一起来看看 API。你可以在 @L_301_2@ 上找到对应的 Xcode 项目。

最简单的实现重排是通过使用UICollectionVIEwController。它现在有一个新的属性叫做installsstandardGestureForInteractiveMovement,作用是添加手势(gestures)来重排 cells。这个属性默认值为True,这意味着要使用它我们只需要重写一个方法。

func collectionVIEw(collectionVIEw: UICollectionVIEw,moveItemAtIndexPath sourceIndexPath: NSIndexPath,toIndexPath destinationIndexPath: NSIndexPath) {    // move your data order    // 可以留空}

当前的 collection vIEw 判定 items 可以被移动,因为moveItemAtIndexPath被重写了。

当我们希望在一个简单的UIVIEwController中使用 collection vIEw 时,会麻烦一点。我们也要实现之前提到的UICollectionVIEwDataSource方法,不过我们需要重写installsstandardGestureForInteractiveMovement。不用担心,也很简单。UILongPressGestureRecognizer是一种持续性的手势识别器并且完全支持拖动。

overrIDe func vIEwDIDLoad() {    super.vIEwDIDLoad()            longPressGesture = UILongPressGestureRecognizer(target: self,action: "handleLongGesture:")        self.collectionVIEw.addGestureRecognizer(longPressGesture)}    func handleLongGesture(gesture: UILongPressGestureRecognizer) {        switch(gesture.state) {        case UIGestureRecognizerState.Began:            guard let selectedindexPath = self.collectionVIEw.indexPathForItemAtPoint(gesture.locationInVIEw(self.collectionVIEw)) else {                break            }            collectionVIEw.beginInteractiveMovementForItemAtIndexPath(selectedindexPath)        case UIGestureRecognizerState.Changed:            collectionVIEw.updateInteractiveMovementTargetposition(gesture.locationInVIEw(gesture.vIEw!))        case UIGestureRecognizerState.Ended:            collectionVIEw.endInteractiveMovement()        default:            collectionVIEw.cancelinteractiveMovement()        }    }

我们保存了在 long press gesture 中不活的被选中的 index path 并且基于它是否有值决定允不允许拖动手势生效。然后,我们根据手势状态调用一些新的 collection vIEw 方法。

beginInteractiveMovementForItemAtIndexPath(indexPath: NSIndexPath):开始指定位置 cell 的交互移动。

updateInteractiveMovementTargetposition(targetposition: CGPoint):更新交互移动对象的位置

endInteractiveMovement():在你结束拖动手势之后结束交互移动

cancelinteractiveMovement():取消交互移动

这些让搞定拖动手势非常容易。

效果和标准的UICollectionVIEwController一样。很酷对吧,不过更酷的是我们可以将我们自定义的 collection vIEw layout 应用到重排中去。看看下面在简单的瀑布视图中的交互移动。

嗯,看起来不错,不过如果我们不想在移动的时候改变 cell 大小呢?选中的 cell 大小应该在交互移动时保持一致。这是可以实现的。UICollectionVIEwLayout也有一些其他的方法来负责重排。

func invalIDationContextForInteractivelyMovingItems(targetIndexPaths: [NSIndexPath],withTargetposition targetposition: CGPoint,prevIoUsIndexPaths: [NSIndexPath],prevIoUsposition: CGPoint) -> UICollectionVIEwLayoutInvalIDationContextfunc invalIDationContextForEndingInteractiveMovementOfItemsToFinalindexPaths(indexPaths: [NSIndexPath],movementCancelled: Bool) -> UICollectionVIEwLayoutInvalIDationContext

前一个在目标 indexPath 和之前的 indexPath 之间进行移动时调用。另一个类似,不过是在移动结束之后调用。有了这些我们就可以通过一些小手段达到我们的要求。

internal overrIDe func invalIDationContextForInteractivelyMovingItems(targetIndexPaths: [NSIndexPath],prevIoUsposition: CGPoint) -> UICollectionVIEwLayoutInvalIDationContext {    var context = super.invalIDationContextForInteractivelyMovingItems(targetIndexPaths,withTargetposition: targetposition,prevIoUsIndexPaths: prevIoUsIndexPaths,prevIoUsposition: prevIoUsposition)    self.delegate?.collectionVIEw!(self.collectionVIEw!,moveItemAtIndexPath: prevIoUsIndexPaths[0],toIndexPath: targetIndexPaths[0])    return context}

解决方案非常清晰。获取正在移动的 cell 之前和目标 index path。然后调用UICollectionVIEwDataSource来移动这些 item。

不用怀疑,collection vIEw 重排是一个非常棒的更新。UIKit 工程师干得太棒了!:)

总结

以上是内存溢出为你收集整理的【译】UICollectionView 轻松重排全部内容,希望文章能够帮你解决【译】UICollectionView 轻松重排所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1077535.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存