【问题标题】:How to do horizontally autoscroll for colletionViewCell?如何为collectionViewCell进行水平自动滚动?
【发布时间】:2020-05-07 08:33:13
【问题描述】:

我的 App 视图顶部有一个 collectionView,我想在其中显示一些广告, 我为一些想要在集合视图中显示它们的图片制作了一个现成的数组。

现在我正在寻找一种可以让它们自动水平滚动的方法。

类 HomeVC:UIViewController{

override func viewDidLoad() {
    super.viewDidLoad()
    setupCollectionViewCell()
}

@IBOutlet weak var collectionView: UICollectionView!


var array = [ "https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcR8aTilga7j-8GfP4OUXUx1bV3E2EJaFt29QdSBD8OgcLBLCUiG&usqp=CAU?",
             "https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcRVq4AqZIDQ_UPnSd-PxQGkEutZnlu76NbZ4xETWelVnULLJ614&usqp=CAU",
    "https://scontent.ffjr1-2.fna.fbcdn.net/v/t1.0-9/72577_563558293672959_39183751_n.jpg?_nc_cat=101&_nc_sid=dd9801&_nc_ohc=YySniLzpf_sAX-B8YFE&_nc_ht=scontent.ffjr1-2.fna&oh=12da3f0ae2404066684d742b4f785cfc&oe=5ED9FCDA",
             "https://img.particlenews.com/image.php?type=thumbnail_1024x576&url=2zUI4r_0OJWDY4z00",
             "https://content3.jdmagicbox.com/comp/def_content/advertising_agencies/default-advertising-agencies-9.jpg",
             "https://miro.medium.com/max/895/1*2gq5_jgNSYJnLzCqIIYVGA.jpeg"]

}

在我注册的单元格下方,我想通过无限数组循环在其中进行自动滚动。

那你能帮我怎么做吗?

extension HomeVC: UICollectionViewDelegate, UICollectionViewDataSource {

func setupCollectionViewCell () {
    collectionView.delegate = self ; collectionView.dataSource = self
    collectionView.register(UINib(nibName: "homeAdCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "adCell")
}


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return array.count

}


func collectionView(_ collectionView: UICollectionView, layout collectionViewlayout: UICollectionViewLayout, sizeForItemAt IndexPath: IndexPath) -> CGSize {

    print ("Size func called")
    return CGSize(width: 250, height: 75)
}


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "adCell", for: indexPath) as! homeAdCollectionViewCell

    cell.update(SURL: array[indexPath.row])

    return cell
}

}

【问题讨论】:

    标签: xcode loops swift5 collectionview autoscroll


    【解决方案1】:
    1. ViewDidLoad 中设置Timer 并使用collectionView.ScrollToitem 方法滚动到下一项

    2. 如果你想用自动滚动效果无限滚动,你可以检查这个库: https://github.com/WenchaoD/FSPagerView

    【讨论】:

    • 其实我找不到collectionView.ScrollToItem的功能,没有自动补全!!你觉得我搞砸了什么?
    • 我的意思是 yourCollectionViewName.ScrollToItem :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-21
    • 2010-11-03
    • 2019-05-06
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    相关资源
    最近更新 更多