Guest User

Untitled

a guest
Feb 24th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. import UIKit
  2.  
  3. // MARK: PhotoSliderView
  4.  
  5. class PhotoSliderView: UIView {
  6.  
  7. // MARK: Outlets
  8.  
  9. @IBOutlet var contentView: UIView!
  10. @IBOutlet var scrollView: UIScrollView!
  11. @IBOutlet var pageControl: UIPageControl!
  12.  
  13. // MARK: Configure Methods
  14.  
  15. func configure(with images: [UIImage]) {
  16.  
  17. // Get the scrollView width and height
  18. let scrollViewWidth: CGFloat = scrollView.frame.width
  19. let scrollViewHeight: CGFloat = scrollView.frame.height
  20.  
  21. // Loop through all of the images and add them all to the scrollView
  22. for (index, image) in images.enumerated() {
  23. let imageView = UIImageView(frame: CGRect(x: scrollViewWidth * CGFloat(index),
  24. y: 0,
  25. width: scrollViewWidth,
  26. height: scrollViewHeight))
  27. imageView.image = image
  28. imageView.contentMode = .scaleAspectFill
  29. imageView.clipsToBounds = true
  30. scrollView.addSubview(imageView)
  31. }
  32.  
  33. // Set the scrollView contentSize
  34. scrollView.contentSize = CGSize(width: scrollView.frame.width * CGFloat(images.count),
  35. height: scrollView.frame.height)
  36.  
  37. // Ensure that the pageControl knows the number of pages
  38. pageControl.numberOfPages = images.count
  39. }
  40.  
  41. // MARK: Init Methods
  42.  
  43. override init(frame: CGRect) {
  44. super.init(frame: frame)
  45. commonInit()
  46. }
  47.  
  48. required init?(coder aDecoder: NSCoder) {
  49. super.init(coder: aDecoder)
  50. commonInit()
  51. }
  52.  
  53. private func commonInit() {
  54. Bundle.main.loadNibNamed(String(describing: PhotoSliderView.self), owner: self, options: nil)
  55. addSubview(contentView)
  56. contentView.frame = self.bounds
  57. contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  58. }
  59.  
  60. // MARK: Helper Methods
  61.  
  62. @IBAction func pageControlTap(_ sender: Any?) {
  63. guard let pageControl: UIPageControl = sender as? UIPageControl else {
  64. return
  65. }
  66.  
  67. scrollToIndex(index: pageControl.currentPage)
  68. }
  69.  
  70. private func scrollToIndex(index: Int) {
  71. let pageWidth: CGFloat = scrollView.frame.width
  72. let slideToX: CGFloat = CGFloat(index) * pageWidth
  73.  
  74. scrollView.scrollRectToVisible(CGRect(x: slideToX, y:0, width:pageWidth, height:scrollView.frame.height), animated: true)
  75. }
  76. }
  77.  
  78. // MARK: UIScrollViewDelegate
  79.  
  80. extension PhotoSliderView: UIScrollViewDelegate {
  81. func scrollViewDidEndDecelerating(_ scrollView: UIScrollView){
  82. let pageWidth:CGFloat = scrollView.frame.width
  83. let currentPage:CGFloat = floor((scrollView.contentOffset.x-pageWidth/2)/pageWidth)+1
  84. pageControl.currentPage = Int(currentPage)
  85. }
  86. }
Add Comment
Please, Sign In to add comment