In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how Swift in iOS uses UICollectionView to achieve unlimited carousel function". The content is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how Swift in iOS uses UICollectionView to achieve unlimited carousel function".
Preface
As a senior (think) iOS programmer, I will often use the broadcast graph. The last time I used UIScrollView to achieve the effect of unlimited rotation, this time in the Swift language, I use UICollectionView to explain the implementation principle of infinite rotation again.
First, the picture above:
UICollectionView- unlimited carousel .gif
The first thing you need to implement is the paging of UICollectionView, which is simple:
CollectionView.isPagingEnabled = true
Next is the principle: you need to add two pictures at both ends of the UICollectionView, the last picture in the first paragraph, and the first picture at the end, and then add each picture in the middle position at one time. This is actually very easy to achieve:
Func collectionView (_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath)-> UICollectionViewCell {let cell = collectionView.dequeueReusableCell (withReuseIdentifier: "ImageCollectionViewCell", for: indexPath) as! ImageCollectionViewCell / / assign a value to the picture (add two pictures at the beginning and end respectively) if (indexPath.row = = 0) {cell.imageName = imageNameList.last} else if (indexPath.row = = self.imageNameList.count + 1) {cell.imageName = imageNameList.first} else {cell.imageName = imageNameList [indexPath.row-1]} return cell}
In this way, when sliding, the effect of infinite rotation can be achieved through offset. When the slide stops, the offset is judged, and when the offset is 0 (the last picture is shown in the view), the method of adjusting the offset is directly adjusted to offset the UICollectionView to the position of the last image. It's the same when sliding to the end.
Func scrollViewDidEndDecelerating (_ scrollView: UIScrollView) {/ when the UIScrollView slides to the first stop, change the offset position of the UIScrollView if (scrollView.contentOffset.x = = 0) {scrollView.contentOffset = CGPoint (x: CGFloat (self.imageNameList.count) * kScreenWidth,y: 0) self.pageControl.currentPage = self.imageNameList.count / / when UIScrollView slides to the last stop Change the offset position of UIScrollView} else if (scrollView.contentOffset.x = = CGFloat (self.imageNameList.count + 1) * kScreenWidth) {scrollView.contentOffset = CGPoint (x: kScreenWidth,y: 0) self.pageControl.currentPage = 0} else {self.pageControl.currentPage = Int (scrollView.contentOffset.x / kScreenWidth)-1}}
In fact, the principle is very simple. I personally think that using UICollectionView to implement unlimited carousel is more practical and easier to maintain than UIScrollView. Next, I'll list all the codes:
Import UIKitlet kScreenWidth = UIScreen.main.bounds.widthclass ViewController: UIViewController {lazy var collectionView: UICollectionView = {let flowLayout = UICollectionViewFlowLayout () flowLayout.minimumLineSpacing = 0 flowLayout.minimumInteritemSpacing = 0 flowLayout.scrollDirection = .roomflowLayout.itemSize = CGSize (width: kScreenWidth, height: 200) let collectionView = UICollectionView (frame: CGRect (x: 0, y: 0, width: UIScreen.main.bounds.width, height: 200) CollectionViewLayout: flowLayout) collectionView.isPagingEnabled = true collectionView.showsHorizontalScrollIndicator = false collectionView.backgroundColor = UIColor.white collectionView.delegate = self collectionView.dataSource = self self.view.addSubview (collectionView) return collectionView} () lazy var pageControl: UIPageControl = {let pageControl = UIPageControl (frame: CGRect (x: 0,150, width: kScreenWidth, height: 50) pageControl.numberOfPages = self.imageNameList.count pageControl.currentPage = 0 pageControl.tintColor = UIColor.black pageControl.pageIndicatorTintColor = UIColor.gray Return pageControl } () lazy var imageNameList: [String] = {let imageList = ["image0", "image1", "image2", "image3"] return imageList} () override func viewDidLoad () {super.viewDidLoad () setupController ()} func setupController () {/ set data collectionView.register (ImageCollectionViewCell.self, forCellWithReuseIdentifier: "ImageCollectionViewCell") collectionView.reloadData () collectionView.scrollToItem (at: IndexPath (row: 1, section: 0), at: .left Animated: false) self.view.addSubview (pageControl)} extension ViewController: UICollectionViewDataSource {func collectionView (_ collectionView: UICollectionView, numberOfItemsInSection section: Int)-> Int {/ this step is just to prevent collapse if (imageNameList.count = = 0) {return 0} return imageNameList.count + 2} func collectionView (_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath)-> UICollectionViewCell {let cell = collectionView.dequeueReusableCell (withReuseIdentifier: "ImageCollectionViewCell", for: indexPath) as! ImageCollectionViewCell / / assign a value to the picture (add two pictures at the beginning and end respectively) if (indexPath.row = = 0) {cell.imageName = imageNameList.last} else if (indexPath.row = = self.imageNameList.count + 1) {cell.imageName = imageNameList.first} else {cell.imageName = imageNameList [indexPath.row-1]} return cell}} extension ViewController: UICollectionViewDelegate {func scrollViewDidEndDecelerating (_ scrollView: UIScrollView) {/ / when UIScrollView stops sliding to the first position Change the offset position of UIScrollView if (scrollView.contentOffset.x = = 0) {scrollView.contentOffset = CGPoint (x: CGFloat (self.imageNameList.count) * kScreenWidth,y: 0) self.pageControl.currentPage = self.imageNameList.count / / when UIScrollView slides to the last bit to stop Change the offset position of UIScrollView} else if (scrollView.contentOffset.x = = CGFloat (self.imageNameList.count + 1) * kScreenWidth) {scrollView.contentOffset = CGPoint (x: kScreenWidth) Y: 0) self.pageControl.currentPage = 0} else {self.pageControl.currentPage = Int (scrollView.contentOffset.x / kScreenWidth)-1} / cellclass ImageCollectionViewCell of the collectionView picture: UICollectionViewCell {/ the picture let imageView = UIImageView () var imageName: String? = "" {didSet {if let name = imageName {imageView.image = UIImage (named: name)} override init (frame: CGRect) {super.init (frame: frame) setupCell () } / / initialize the view func setupCell () {imageView.frame = self.bounds contentView.addSubview (imageView)} required init? (coder aDecoder: NSCoder) {fatalError ("init (coder:) has not been implemented")}} these are all the contents of the article "how Swift in iOS uses UICollectionView to achieve unlimited carousel". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.