Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.24 KB | None | 0 0
  1. //
  2. //  CustomSegmentedControl.swift
  3. //  SuperBoss
  4. //
  5. //  Created by STENIO FREITAS on 19/08/17.
  6. //  Copyright © 2017 Stenio Wagner. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class CustomSegmentedControl: UIControl {
  12.  
  13.     var selectorTextColor: UIColor = .white
  14.     var selectorColor: UIColor = .darkGray
  15.     var selector: UIView!
  16.    
  17.     var borderColor: UIColor = .yellow
  18.     var borderWidth: CGFloat = 1
  19.     var textColor: UIColor = .lightGray
  20.    
  21.     let buttonTitles = ["Podcasts", "Vídeos"]
  22.     var buttons = [UIButton]()
  23.  
  24.     override func draw(_ rect: CGRect) {
  25.         layer.cornerRadius = frame.height / 2
  26.     }
  27.    
  28.     func updateViews() {
  29.         subviews.forEach { $0.removeFromSuperview() }
  30.  
  31.         buttons.removeAll()
  32.  
  33.         setupButtons(buttonTitles)
  34.        
  35.         buttons[0].setTitleColor(selectorTextColor, for: .normal)
  36.        
  37.         setupSelector(numberOfTitles: buttonTitles.count)
  38.        
  39.         setupStackView()
  40.     }
  41.    
  42.     func setupButtons(_ buttonTitles: [String]) {
  43.         for buttonTitle in buttonTitles {
  44.             let button = UIButton(type: .system)
  45.            
  46.             button.setTitle(buttonTitle, for: .normal)
  47.             button.setTitleColor(textColor, for: .normal)
  48.             button.addTarget(self, action: #selector(onSegmentedControlChanged(buttonClicked:)), for: .touchUpInside)
  49.            
  50.             buttons.append(button)
  51.         }
  52.     }
  53.    
  54.     func setupSelector(numberOfTitles: Int) {
  55.         let selectorWidth = frame.width / CGFloat(numberOfTitles)
  56.        
  57.         selector = UIView(frame: CGRect(x: 0, y: 0, width: Int(selectorWidth), height: Int(frame.height)))
  58.         selector.layer.cornerRadius = frame.height / 2
  59.         selector.backgroundColor = selectorColor
  60.        
  61.         addSubview(selector)
  62.     }
  63.    
  64.     func setupStackView() {
  65.         let stackView = UIStackView(arrangedSubviews: buttons)
  66.        
  67.         stackView.axis = .horizontal
  68.         stackView.alignment = .fill
  69.         stackView.distribution = .fillProportionally
  70.        
  71.         addSubview(stackView)
  72.        
  73.         stackView.translatesAutoresizingMaskIntoConstraints = false
  74.        
  75.         stackView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
  76.         stackView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
  77.         stackView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
  78.         stackView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
  79.     }
  80.    
  81.     func onSegmentedControlChanged(buttonClicked: UIButton) {
  82.         for (buttonIndex, button) in buttons.enumerated() {
  83.             button.setTitleColor(textColor, for: .normal)
  84.            
  85.             if button == buttonClicked {
  86.                 animateSegmentedControlChange(buttonTargetIndex: buttonIndex)
  87.                 button.setTitleColor(selectorTextColor, for: .normal)
  88.             }
  89.         }
  90.     }
  91.    
  92.     func animateSegmentedControlChange(buttonTargetIndex: Int) {
  93.         let selectorStartPosition = frame.width / CGFloat(buttons.count) * CGFloat(buttonTargetIndex)
  94.        
  95.         UIView.animate(withDuration: 0.3, animations: {
  96.             self.selector.frame.origin.x = selectorStartPosition
  97.         })
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement