Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // DemoViewController.swift
- // Text Art
- //
- // Created by Bitmatrix110 on 19/07/19.
- // Copyright © 2019 vasundhara. All rights reserved.
- //
- import UIKit
- class CourseCell: UICollectionViewCell {
- @IBOutlet weak var lbl: UILabel!
- }
- class DemoViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
- @IBOutlet weak var col: UICollectionView!
- @IBOutlet weak var viewMain: UIView!
- var pulsatingLayer = CAShapeLayer()
- let colorPulse = UIColor.init(red: 200.0/255.0, green: 200.0/255.0, blue: 200.0/255.0, alpha: 0.5)
- var timerCount: Timer!
- var counter: Int = 0
- var currentIndex: Int = 0
- override func viewDidLoad() {
- super.viewDidLoad()
- viewMain.layer.cornerRadius = 50.0
- let circularPath = UIBezierPath.init(arcCenter: .zero, radius: 50, startAngle: 0, endAngle: 2*CGFloat.pi, clockwise: true)
- pulsatingLayer = createCircleShapeLayer(radius: 50.0, inView: viewMain)
- viewMain.layer.addSublayer(pulsatingLayer)
- animatePulsatingLayer()
- let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
- layout.itemSize = CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height-139.0)
- layout.scrollDirection = .horizontal
- col.collectionViewLayout = layout
- self.col.reloadData()
- DispatchQueue.main.asyncAfter(deadline: .now()+0.2) {
- self.timerCount = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.timerFired), userInfo: nil, repeats: true)
- }
- }
- func createCircleShapeLayer(radius: CGFloat, inView: UIView) -> CAShapeLayer
- {
- let circularPath = UIBezierPath.init(arcCenter: .zero, radius: radius, startAngle: 0, endAngle: 2*CGFloat.pi, clockwise: true)
- let pulsatingLayer = CAShapeLayer()
- pulsatingLayer.path = circularPath.cgPath
- pulsatingLayer.strokeColor = colorPulse.cgColor
- pulsatingLayer.lineWidth = 0
- pulsatingLayer.fillColor = colorPulse.cgColor
- pulsatingLayer.lineCap = CAShapeLayerLineCap.round
- pulsatingLayer.position = CGPoint.init(x: inView.frame.height/2.0, y: inView.frame.height/2.0)
- return pulsatingLayer
- }
- func animatePulsatingLayer()
- {
- let animation = CABasicAnimation.init(keyPath: "transform.scale")
- animation.toValue = 1.4
- animation.duration = 0.5
- animation.timingFunction = CAMediaTimingFunction.init(name: CAMediaTimingFunctionName.easeOut)
- animation.autoreverses = true
- animation.repeatCount = 2 //Float.infinity
- pulsatingLayer.add(animation, forKey: "pulsing")
- }
- @objc func timerFired()
- {
- counter += 1
- if counter == 2
- {
- counter = 0
- if currentIndex > 8
- {
- currentIndex = -1
- }
- let indexPath = IndexPath.init(item: currentIndex+1, section: 0)
- col.scrollToItem(at: indexPath, at: .right, animated: true)
- }
- }
- func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- return 10
- }
- func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CourseCell", for: indexPath) as! CourseCell
- cell.lbl.text = "\(indexPath.item)"
- return cell
- }
- func scrollViewDidScroll(_ scrollView: UIScrollView) {
- let offsetX = col.contentOffset.x
- let quotient = Int(offsetX / UIScreen.main.bounds.width)
- let reminder = offsetX.truncatingRemainder(dividingBy: UIScreen.main.bounds.width)
- if reminder > (UIScreen.main.bounds.width/2.0)
- {
- if self.currentIndex != quotient+1
- {
- self.currentIndex = quotient+1
- print("index : \(quotient+1)")
- }
- }
- else
- {
- if self.currentIndex != quotient
- {
- self.currentIndex = quotient
- print("index : \(quotient)")
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement