Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. //
  2. // DemoViewController.swift
  3. // Text Art
  4. //
  5. // Created by Bitmatrix110 on 19/07/19.
  6. // Copyright © 2019 vasundhara. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11.  
  12.  
  13. class CourseCell: UICollectionViewCell {
  14.  
  15. @IBOutlet weak var lbl: UILabel!
  16. }
  17.  
  18. class DemoViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
  19.  
  20. @IBOutlet weak var col: UICollectionView!
  21.  
  22. @IBOutlet weak var viewMain: UIView!
  23.  
  24. var pulsatingLayer = CAShapeLayer()
  25.  
  26. let colorPulse = UIColor.init(red: 200.0/255.0, green: 200.0/255.0, blue: 200.0/255.0, alpha: 0.5)
  27.  
  28. var timerCount: Timer!
  29.  
  30. var counter: Int = 0
  31. var currentIndex: Int = 0
  32.  
  33. override func viewDidLoad() {
  34. super.viewDidLoad()
  35.  
  36. viewMain.layer.cornerRadius = 50.0
  37.  
  38. let circularPath = UIBezierPath.init(arcCenter: .zero, radius: 50, startAngle: 0, endAngle: 2*CGFloat.pi, clockwise: true)
  39.  
  40. pulsatingLayer = createCircleShapeLayer(radius: 50.0, inView: viewMain)
  41. viewMain.layer.addSublayer(pulsatingLayer)
  42.  
  43. animatePulsatingLayer()
  44.  
  45.  
  46. let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
  47. layout.itemSize = CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height-139.0)
  48. layout.scrollDirection = .horizontal
  49. col.collectionViewLayout = layout
  50.  
  51. self.col.reloadData()
  52.  
  53. DispatchQueue.main.asyncAfter(deadline: .now()+0.2) {
  54. self.timerCount = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.timerFired), userInfo: nil, repeats: true)
  55. }
  56. }
  57.  
  58. func createCircleShapeLayer(radius: CGFloat, inView: UIView) -> CAShapeLayer
  59. {
  60. let circularPath = UIBezierPath.init(arcCenter: .zero, radius: radius, startAngle: 0, endAngle: 2*CGFloat.pi, clockwise: true)
  61.  
  62. let pulsatingLayer = CAShapeLayer()
  63. pulsatingLayer.path = circularPath.cgPath
  64. pulsatingLayer.strokeColor = colorPulse.cgColor
  65. pulsatingLayer.lineWidth = 0
  66. pulsatingLayer.fillColor = colorPulse.cgColor
  67. pulsatingLayer.lineCap = CAShapeLayerLineCap.round
  68. pulsatingLayer.position = CGPoint.init(x: inView.frame.height/2.0, y: inView.frame.height/2.0)
  69.  
  70. return pulsatingLayer
  71. }
  72.  
  73. func animatePulsatingLayer()
  74. {
  75. let animation = CABasicAnimation.init(keyPath: "transform.scale")
  76. animation.toValue = 1.4
  77. animation.duration = 0.5
  78. animation.timingFunction = CAMediaTimingFunction.init(name: CAMediaTimingFunctionName.easeOut)
  79. animation.autoreverses = true
  80. animation.repeatCount = 2 //Float.infinity
  81.  
  82. pulsatingLayer.add(animation, forKey: "pulsing")
  83. }
  84.  
  85. @objc func timerFired()
  86. {
  87. counter += 1
  88.  
  89. if counter == 2
  90. {
  91. counter = 0
  92.  
  93. if currentIndex > 8
  94. {
  95. currentIndex = -1
  96. }
  97.  
  98. let indexPath = IndexPath.init(item: currentIndex+1, section: 0)
  99. col.scrollToItem(at: indexPath, at: .right, animated: true)
  100. }
  101. }
  102.  
  103. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  104. return 10
  105. }
  106.  
  107. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  108. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CourseCell", for: indexPath) as! CourseCell
  109. cell.lbl.text = "\(indexPath.item)"
  110. return cell
  111. }
  112.  
  113. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  114.  
  115. let offsetX = col.contentOffset.x
  116. let quotient = Int(offsetX / UIScreen.main.bounds.width)
  117. let reminder = offsetX.truncatingRemainder(dividingBy: UIScreen.main.bounds.width)
  118.  
  119.  
  120. if reminder > (UIScreen.main.bounds.width/2.0)
  121. {
  122. if self.currentIndex != quotient+1
  123. {
  124. self.currentIndex = quotient+1
  125.  
  126. print("index : \(quotient+1)")
  127. }
  128. }
  129. else
  130. {
  131. if self.currentIndex != quotient
  132. {
  133. self.currentIndex = quotient
  134.  
  135. print("index : \(quotient)")
  136. }
  137. }
  138.  
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement