swift_06_Carousel Effect
知识点
1.UICollectionView的使用
2.横向滚动
完整代码
ViewController.swift
import UIKit class ViewController: UIViewController { fileprivate var interests = Interest.createInterests() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. initView() } fileprivate struct My { static let CellIdentifier = "InterestCell" static let CellPadding: CGFloat = 16.0 } func initView(){ var collectionView : UICollectionView? let layout = UICollectionViewFlowLayout() layout.sectionInset = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5); layout.minimumInteritemSpacing = 5; // this number could be anything <=5. Need it here because the default is 10. layout.minimumLineSpacing = 4.0 //设置行间距 layout.itemSize = CGSize(width: 100, height: 44) layout.scrollDirection = .horizontal//横向滚动 collectionView = UICollectionView(frame:CGRect(x: 16, y: 100, width: UIScreen.main.bounds.width-16, height: 450), collectionViewLayout: layout) //注册一个cell collectionView!.register(InterestCollectionViewCell.self, forCellWithReuseIdentifier:My.CellIdentifier) collectionView?.delegate = self; collectionView?.dataSource = self; collectionView?.backgroundColor = UIColor.gray collectionView?.showsHorizontalScrollIndicator = true collectionView?.showsVerticalScrollIndicator = false collectionView?.isPagingEnabled = true self.view.addSubview(collectionView!) } } extension ViewController : UICollectionViewDataSource { func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return interests.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: My.CellIdentifier, for: indexPath) as! InterestCollectionViewCell cell.interest = self.interests[indexPath.item] return cell } } extension ViewController : UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: UIScreen.main.bounds.width - 2 * My.CellPadding, height: 450) } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { return My.CellPadding } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { return UIEdgeInsets(top: 0, left: My.CellPadding/2, bottom: 0, right: My.CellPadding) } } extension ViewController : UIScrollViewDelegate { func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) { let originPoint = targetContentOffset.pointee; var index = Int(originPoint.x / UIScreen.main.bounds.width) let offset = Int(originPoint.x) % Int(UIScreen.main.bounds.width) index += (offset > Int(UIScreen.main.bounds.width/2) ? 1 : 0) targetContentOffset.pointee = CGPoint(x: index * Int(UIScreen.main.bounds.width) , y: 0) } }
数据相关Interest.swift
import UIKit class Interest { // MARK: - Public API var title = "" var description = "" var numberOfMembers = 0 var numberOfPosts = 0 var featuredImage: UIImage! init(title: String, description: String, featuredImage: UIImage!) { self.title = title self.description = description self.featuredImage = featuredImage numberOfMembers = 1 numberOfPosts = 1 } // MARK: - Private // dummy data static func createInterests() -> [Interest] { return [ Interest(title: "Hello there, i miss u.", description: "We love backpack and adventures! We walked to Antartica yesterday, and camped with some cute pinguines, and talked about this wonderful app idea. 🐧⛺️✨", featuredImage: UIImage(named: "hello")!), Interest(title: "🐳🐳🐳🐳🐳", description: "We love romantic stories. We walked to Antartica yesterday, and camped with some cute pinguines, and talked about this wonderful app idea. 🐧⛺️✨", featuredImage: UIImage(named: "dudu")!), Interest(title: "Training like this, #bodyline", description: "Create beautiful apps. We walked to Antartica yesterday, and camped with some cute pinguines, and talked about this wonderful app idea. 🐧⛺️✨", featuredImage: UIImage(named: "bodyline")!), Interest(title: "I'm hungry, indeed.", description: "Cars and aircrafts and boats and sky. We walked to Antartica yesterday, and camped with some cute pinguines, and talked about this wonderful app idea. 🐧⛺️✨", featuredImage: UIImage(named: "wave")!), Interest(title: "Dark Varder, #emoji", description: "Meet life with full presence. We walked to Antartica yesterday, and camped with some cute pinguines, and talked about this wonderful app idea. 🐧⛺️✨", featuredImage: UIImage(named: "darkvarder")!), Interest(title: "I have no idea, bitch", description: "Get up to date with breaking-news. We walked to Antartica yesterday, and camped with some cute pinguines, and talked about this wonderful app idea. 🐧⛺️✨", featuredImage: UIImage(named: "hhhhh")!), ] } }
自定义cell之 InterestCollectionViewCell.swift
import UIKit class InterestCollectionViewCell: UICollectionViewCell { var interest: Interest! { didSet { updateUI() } } var titleLabel:UILabel?//title var featuredImageView: UIImageView! override init(frame: CGRect) { super.init(frame: frame) initView() } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } func initView(){ titleLabel = UILabel(frame: CGRect(x: 0, y: 300, width: frame.width, height: 44)) titleLabel?.layer.cornerRadius = 4 titleLabel?.layer.borderWidth = 0.5 titleLabel?.textAlignment = NSTextAlignment.center titleLabel?.layoutMargins = UIEdgeInsets(top:0, left:0, bottom:0, right:0) self .addSubview(titleLabel!) featuredImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: frame.width, height: 280)) self .addSubview(featuredImageView!) } fileprivate func updateUI() { titleLabel?.text = interest.title featuredImageView?.image = interest.featuredImage } }
浙公网安备 33010602011771号