Advertisement
Guest User

ZKCarousel custom delegate

a guest
Jan 18th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 8.08 KB | None | 0 0
  1. //
  2. //  ZKCarousel.swift
  3. //  Delego
  4. //
  5. //  Created by Zachary Khan on 6/8/17.
  6. //  Copyright © 2017 ZacharyKhan. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. protocol lembuDelegate {
  12.     func getIndexWhenScrolling(currentIndex: Int)
  13. }
  14.  
  15. final public class ZKCarousel: UIView, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource {
  16.    
  17.     var delegate: lembuDelegate?
  18.    
  19.     public var slides : [ZKCarouselSlide] = [] {
  20.         didSet {
  21.             self.collectionView.reloadData()
  22.         }
  23.     }
  24.    
  25.     public lazy var pageControl : UIPageControl = {
  26.         let control = UIPageControl()
  27.         control.numberOfPages = 3
  28.         control.currentPage = 0
  29.         control.hidesForSinglePage = true
  30.         control.pageIndicatorTintColor = .lightGray
  31.         control.currentPageIndicatorTintColor = UIColor(red:0.20, green:0.60, blue:0.86, alpha:1.0)
  32.         return control
  33.     }()
  34.    
  35.     fileprivate lazy var collectionView : UICollectionView = {
  36.         let layout = UICollectionViewFlowLayout()
  37.         layout.scrollDirection = .horizontal
  38.         let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
  39.         cv.delegate = self
  40.         cv.dataSource = self
  41.         cv.isPagingEnabled = true
  42.         cv.register(carouselCollectionViewCell.self, forCellWithReuseIdentifier: "slideCell")
  43.         cv.clipsToBounds = true
  44.         cv.backgroundColor = .clear
  45.         cv.showsHorizontalScrollIndicator = false
  46.         cv.bounces = false
  47.         return cv
  48.     }()
  49.    
  50.     public override init(frame: CGRect) {
  51.         super.init(frame: frame)
  52.         setupCarousel()
  53.     }
  54.    
  55.     required public init?(coder aDecoder: NSCoder) {
  56.         super.init(coder: aDecoder)
  57.         setupCarousel()
  58.     }
  59.    
  60.     private func setupCarousel() {
  61.         self.backgroundColor = .clear
  62.  
  63.         self.addSubview(collectionView)
  64.         self.addConstraintsWithFormat("H:|[v0]|", views: collectionView)
  65.         self.addConstraintsWithFormat("V:|[v0]|", views: collectionView)
  66.        
  67.         self.addSubview(pageControl)
  68.         self.addConstraintsWithFormat("H:|-20-[v0]-20-|", views: pageControl)
  69.         self.addConstraintsWithFormat("V:[v0(25)]-5-|", views: pageControl)
  70.         self.bringSubview(toFront: pageControl)
  71.     }
  72.    
  73.     public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  74.         let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "slideCell", for: indexPath) as! carouselCollectionViewCell
  75.         cell.slide = self.slides[indexPath.item]
  76.         return cell
  77.     }
  78.    
  79.     public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  80.         print(slides.count)
  81.         return self.slides.count
  82.     }
  83.    
  84.     public func numberOfSections(in collectionView: UICollectionView) -> Int {
  85.         return 1
  86.     }
  87.    
  88.     public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  89.         let size = CGSize(width: collectionView.frame.width, height: collectionView.frame.height)
  90.         return size
  91.     }
  92.    
  93.     public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  94.         return 0
  95.     }
  96.    
  97.     public func scrollViewDidScroll(_ scrollView: UIScrollView) {
  98.         let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)
  99.         pageControl.currentPage = Int(pageNumber)
  100.         delegate?.getIndexWhenScrolling(currentIndex: pageControl.currentPage)
  101.     }
  102.    
  103. }
  104.  
  105. fileprivate class carouselCollectionViewCell: UICollectionViewCell {
  106.    
  107.     fileprivate var slide : ZKCarouselSlide? {
  108.         didSet {
  109.             self.parseData(forSlide: slide!)
  110.         }
  111.     }
  112.    
  113.     private lazy var imageView : UIImageView = {
  114.         let iv = UIImageView()
  115.         iv.contentMode = .scaleAspectFill
  116.         iv.backgroundColor = .clear
  117.         iv.clipsToBounds = true
  118.         //iv.addBlackGradientLayer(frame: self.bounds)
  119.         return iv
  120.     }()
  121.    
  122.     private var titleLabel : UILabel = {
  123.         let label = UILabel()
  124.         label.adjustsFontSizeToFitWidth = true
  125.         label.font = UIFont.boldSystemFont(ofSize: 40)
  126.         label.textColor = .white
  127.         label.textAlignment = .center
  128.         return label
  129.     }()
  130.    
  131.     private var descriptionLabel : UILabel = {
  132.         let label = UILabel()
  133.         label.font = UIFont.systemFont(ofSize: 19)
  134.         label.textColor = .white
  135.         label.textAlignment = .center
  136.         label.numberOfLines = 0
  137.         label.backgroundColor = .clear
  138.         label.translatesAutoresizingMaskIntoConstraints = false
  139.         return label
  140.     }()
  141.    
  142.     override init(frame: CGRect) {
  143.         super.init(frame: frame)
  144.         setup()
  145.     }
  146.    
  147.     required init?(coder aDecoder: NSCoder) {
  148.         fatalError("init(coder:) has not been implemented")
  149.     }
  150.    
  151.     private func setup() {
  152.         self.backgroundColor = .clear
  153.         self.clipsToBounds = true
  154.        
  155.         self.addSubview(self.imageView)
  156.         self.addConstraintsWithFormat("H:|[v0]|", views: self.imageView)
  157.         self.addConstraintsWithFormat("V:|[v0]|", views: self.imageView)
  158.        
  159.         self.addSubview(self.descriptionLabel)
  160.         let left = NSLayoutConstraint(item: descriptionLabel, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 15)
  161.         let right = NSLayoutConstraint(item: descriptionLabel, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: -15)
  162.         let bottom = NSLayoutConstraint(item: descriptionLabel, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 0.9, constant: 0)
  163.         let top = NSLayoutConstraint(item: descriptionLabel, attribute: .top, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.25, constant: 0)
  164.         NSLayoutConstraint.activate([left, right, bottom, top])
  165.        
  166.         self.addSubview(self.titleLabel)
  167.         self.addConstraintsWithFormat("H:|-15-[v0]-15-|", views: self.titleLabel)
  168.         self.addConstraintsWithFormat("V:[v0(43)]-[v1]", views: self.titleLabel, self.descriptionLabel)
  169.     }
  170.    
  171.     private func parseData(forSlide slide: ZKCarouselSlide) {
  172.         if let image = slide.slideImage {
  173.             self.imageView.image = image
  174.         }
  175.        
  176.         if let title = slide.slideTitle {
  177.             self.titleLabel.text = title
  178.         }
  179.        
  180.         if let description = slide.slideDescription {
  181.             self.descriptionLabel.text = description
  182.         }
  183.     }
  184. }
  185.  
  186. final public class ZKCarouselSlide : NSObject {
  187.    
  188.     public var slideImage : UIImage?
  189.     public var slideTitle : String?
  190.     public var slideDescription: String?
  191.    
  192.     public init(image: UIImage, title: String, description: String) {
  193.         slideImage = image
  194.         slideTitle = title
  195.         slideDescription = description
  196.     }
  197.    
  198.     override init() {
  199.         super.init()
  200.        
  201.     }
  202.    
  203. }
  204.  
  205. extension UIView {
  206.    
  207.     func addConstraintsWithFormat(_ format: String, views: UIView...) {
  208.        
  209.         var viewsDictionary = [String: UIView]()
  210.         for (index, view) in views.enumerated() {
  211.             let key = "v\(index)"
  212.             viewsDictionary[key] = view
  213.             view.translatesAutoresizingMaskIntoConstraints = false
  214.         }
  215.        
  216.         addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutFormatOptions(), metrics: nil, views: viewsDictionary))
  217.     }
  218.    
  219.    
  220. }
  221.  
  222. extension UIImageView {
  223.     func addBlackGradientLayer(frame: CGRect){
  224.         let gradient = CAGradientLayer()
  225.         gradient.frame = frame
  226.         gradient.colors = [UIColor.clear.cgColor, UIColor.black.withAlphaComponent(0.8).cgColor]
  227.         gradient.locations = [0.0, 0.6]
  228.         self.layer.insertSublayer(gradient, at: 0)
  229.     }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement