简体   繁体   English

带有集合视图 POD 的 Swift Carousel

[英]Swift Carousel with collection views POD

So I'm trying to achieve something like this:所以我试图实现这样的目标:

在此处输入图片说明

A carousel where I can swipe left and right, but I'm not sure how can I achieve this, I have a collection view all setup with horizontal scroll right now.我可以左右滑动的旋转木马,但我不确定如何实现这一点,我现在有一个带有水平滚动的集合视图设置。

Is there any other way to do it?有没有其他方法可以做到? Is there a POD or something that I can use?是否有 POD 或我可以使用的东西? it doesn't need to use collection views I just tested using this.它不需要使用我刚刚使用它测试过的集合视图。

Any help will be appreciated任何帮助将不胜感激

A great pod that I've used is called "CollectionViewPagingLayout" it's very simple我用过的一个很棒的 pod 叫做“CollectionViewPagingLayout”,它非常简单

  1. install the pod here ( https://github.com/amirdew/CollectionViewPagingLayout#installation )在此处安装 pod ( https://github.com/amirdew/CollectionViewPagingLayout#installation )
  2. create a custom cell class.创建自定义单元格类。 Import CollectionViewPagingLayout and conform to transformable view.导入 CollectionViewPagingLayout 并符合可转换视图。 Then conform to the protocol stub (transform) and customize the carousel however you want like so:然后符合协议存根(转换)并根据需要自定义轮播:
import UIKit
import CollectionViewPagingLayout

class MyCustomCell: UICollectionViewCell, TransformableView {

  func transform(progress: CGFloat) {
     //customize it however you want
  }
}
  1. if you don't want to customize your own you can use 3 of the pre-set options he offers.如果您不想自定义您可以使用他提供的 3 个预设选项。 Just change "TransformableView" to either "StackTransformView", "SnaphotTransformView", or "ScaleTransformView".只需将“TransformableView”更改为“StackTransformView”、“SnaphotTransformView”或“ScaleTransformView”。 Then replace the transform func with one of his pre-set variables like so:然后用他的预设变量之一替换转换函数,如下所示:
import UIKit
import CollectionViewPagingLayout

class MyCustomCell: UICollectionViewCell, ScaleTransformView {

    var scaleOptions = ScaleTransformViewOptions(
        minScale: 0.6,
        scaleRatio: 0.4,
        translationRatio: CGPoint(x: 0.66, y: 0.2),
        maxTranslationRatio: CGPoint(x: 2, y: 0),
    )
}
  1. Now go to your ViewController (with the collectionView) and import collectionViewpaginglayout.现在转到您的 ViewController(带有 collectionView)并导入 collectionViewpaginglayout。 Then create a layout constant that conforms to the CollectionViewPagingLayout.然后创建一个符合 CollectionViewPagingLayout 的布局常量。 Finally, under view did load, connect your collectionView to the layout class like so:最后,在 view did load 下,将您的 collectionView 连接到布局类,如下所示:
import UIKit
import CollectionViewPagingLayout

class viewController: UIViewController {

//Outlets
@IBOutlet var myCollectionView: UICollectionView!

//Variables
let layout = CollectionViewPagingLayout()

//View did load
override func viewDidLoad() {
        super.viewDidLoad()
     //collectionViewPagingLayout setup
      layout.delegate = self
      myCollectionView.collectionViewLayout = layout
      myCollectionView.isPagingEnabled = true
}

}
  1. Set up your collectionView how you normally do with the delegates and such.设置您的 collectionView 您通常如何处理委托等。 And you're all set.你已经准备好了。 You can use the layout delegate method it offers to do things like change the page or count which number page you are in. like so:您可以使用它提供的布局委托方法来执行诸如更改页面或计算您所在的页面数量之类的操作。如下所示:
extension ViewController: CollectionViewPagingLayoutDelegate {
    
    func onCurrentPageChanged(layout: CollectionViewPagingLayout, currentPage: Int) {
       print(currentPage)
    }
}
  • Note 1 - when setting up your custom collectionView cell (in storyboard or nib), add the constrains as center horizontally in container and center vertically in container or else it won't show the 2 other cells peeking.注意 1 - 在设置自定义 collectionView 单元格(在故事板或笔尖中)时,将约束添加为容器中水平居中,容器中垂直居中,否则不会显示其他 2 个单元格偷看。
  • Note 2 - You can set the number of visible items to an int so that it doesn't load all the cells at once (this is better explained in the github page).注意 2 - 您可以将可见项目的数量设置为 int,这样它就不会一次加载所有单元格(这在 github 页面中有更好的解释)。 ex: layout.numberOfVisibleItems = 3 you would put this within viewdidload()例如: layout.numberOfVisibleItems = 3你会把它放在 viewdidload() 中

In Swift 3 .斯威夫特 3 中

extension UICollectionView {

var centerPoint : CGPoint {

    get {

        return CGPoint(x: self.center.x + self.contentOffset.x, y: self.center.y + self.contentOffset.y);
    }
}

var centerCellIndexPath: IndexPath? {

    if let centerIndexPath = self.indexPathForItem(at: self.centerPoint) {

        return centerIndexPath
    }

    return nil
 }
}

To make Curosel with Item cells:要使用项目单元格制作Curosel

func curosel() {

    if myCollectionView.centerCellIndexPath != nil {

        var index = (myCollectionView.centerCellIndexPath?.row)!

        if  myCollectionView.centerCellIndexPath! == IndexPath(item: 0, section: 0) {

            myCollectionView.cellForItem(at: myCollectionView.centerCellIndexPath!)?.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)

            index += 1

            myCollectionView.cellForItem(at: IndexPath(item: index, section: 0))?.transform = CGAffineTransform.identity

        } else {

            myCollectionView.cellForItem(at: myCollectionView.centerCellIndexPath!)?.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
            index -= 1
            myCollectionView.cellForItem(at: IndexPath(item: index, section: 0))?.transform = CGAffineTransform.identity
            index += 2
            myCollectionView.cellForItem(at: IndexPath(item: index, section: 0))?.transform = CGAffineTransform.identity
        }
        myCollectionView.scrollToItem(at:IndexPath(item: index-1, section: 0), at: .centeredHorizontally, animated: true)
     }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM