Skip to content

Commit

Permalink
Merge pull request #3 from mazmik25/bugFix/swift5_and_improvements
Browse files Browse the repository at this point in the history
Update to swift 5 and make code improvements
  • Loading branch information
hilalbaig authored Jun 24, 2020
2 parents 3abd07e + 573892a commit 63d192e
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 322 deletions.
175 changes: 66 additions & 109 deletions HBSegmentedControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,47 @@

import UIKit

@IBDesignable class HBSegmentedControl: UIControl {
@IBDesignable public class HBSegmentedControl: UIControl {

fileprivate var labels = [UILabel]()
var thumbView = UIView()
private var thumbView = UIView()

var items: [String] = ["Item 1", "Item 2", "Item 3"] {
public var items: [String] = ["Item 1", "Item 2", "Item 3"] {
didSet {
setupLabels()
if items.count > 0 { setupLabels() }
}
}

var selectedIndex : Int = 0 {
didSet {
displayNewSelectedIndex()
}
public var selectedIndex: Int = 0 {
didSet { displayNewSelectedIndex() }
}

@IBInspectable var selectedLabelColor : UIColor = UIColor.black {
didSet {
setSelectedColors()
}
@IBInspectable public var selectedLabelColor: UIColor = UIColor.black {
didSet { setSelectedColors() }
}

@IBInspectable var unselectedLabelColor : UIColor = UIColor.white {
didSet {
setSelectedColors()
}
@IBInspectable public var unselectedLabelColor: UIColor = UIColor.white {
didSet { setSelectedColors() }
}

@IBInspectable var thumbColor : UIColor = UIColor.white {
didSet {
setSelectedColors()
}
@IBInspectable public var thumbColor: UIColor = UIColor.white {
didSet { setSelectedColors() }
}

@IBInspectable var borderColor : UIColor = UIColor.white {
didSet {
layer.borderColor = borderColor.cgColor
}
@IBInspectable public var borderColor: UIColor = UIColor.white {
didSet { layer.borderColor = borderColor.cgColor }
}

@IBInspectable var font : UIFont! = UIFont.systemFont(ofSize: 12) {
didSet {
setFont()
}
@IBInspectable public var font: UIFont? = UIFont.systemFont(ofSize: 12) {
didSet { setFont() }
}

override init(frame: CGRect) {
public var padding: CGFloat = 0 {
didSet { setupLabels() }
}

public override init(frame: CGRect) {
super.init(frame: frame)

setupView()
}

Expand All @@ -67,72 +58,60 @@ import UIKit
setupView()
}

func setupView(){

private func setupView() {
layer.cornerRadius = frame.height / 2
layer.borderColor = UIColor(white: 1.0, alpha: 0.5).cgColor
layer.borderWidth = 2

backgroundColor = UIColor.clear

setupLabels()

addIndividualItemConstraints(labels, mainView: self, padding: 0)

insertSubview(thumbView, at: 0)
}

func setupLabels(){

private func setupLabels() {
for label in labels {
label.removeFromSuperview()
}

labels.removeAll(keepingCapacity: true)

for index in 1...items.count {

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 70, height: 40))
let label = UILabel()
label.text = items[index - 1]
label.backgroundColor = UIColor.clear
label.backgroundColor = .clear
label.textAlignment = .center
label.font = UIFont(name: "Avenir-Black", size: 15)
label.font = font
label.textColor = index == 1 ? selectedLabelColor : unselectedLabelColor
label.translatesAutoresizingMaskIntoConstraints = false

self.addSubview(label)
addSubview(label)
labels.append(label)
}

addIndividualItemConstraints(labels, mainView: self, padding: 0)
addIndividualItemConstraints(labels, mainView: self)
}

override func layoutSubviews() {
public override func layoutSubviews() {
super.layoutSubviews()

var selectFrame = self.bounds
let newWidth = selectFrame.width / CGFloat(items.count)
selectFrame.size.width = newWidth
thumbView.frame = selectFrame
thumbView.backgroundColor = thumbColor
thumbView.layer.cornerRadius = thumbView.frame.height / 2

displayNewSelectedIndex()

if labels.count > 0 {
let label = labels[selectedIndex]
label.textColor = selectedLabelColor
thumbView.frame = label.frame
thumbView.backgroundColor = thumbColor
thumbView.layer.cornerRadius = thumbView.frame.height / 2
displayNewSelectedIndex()
}
}

override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {

public override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
let location = touch.location(in: self)

var calculatedIndex : Int?
for (index, item) in labels.enumerated() {
if item.frame.contains(location) {
calculatedIndex = index
}
}


if calculatedIndex != nil {
selectedIndex = calculatedIndex!
sendActions(for: .valueChanged)
Expand All @@ -141,71 +120,49 @@ import UIKit
return false
}

func displayNewSelectedIndex(){
private func displayNewSelectedIndex() {
for (_, item) in labels.enumerated() {
item.textColor = unselectedLabelColor
}

let label = labels[selectedIndex]
label.textColor = selectedLabelColor




UIView.animate(withDuration: 0.5, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.8, animations: {

self.thumbView.frame = label.frame

}, completion: nil)

}, completion: nil)
}

func addIndividualItemConstraints(_ items: [UIView], mainView: UIView, padding: CGFloat) {

_ = mainView.constraints

private func addIndividualItemConstraints(_ items: [UIView], mainView: UIView) {
for (index, button) in items.enumerated() {

let topConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: mainView, attribute: NSLayoutConstraint.Attribute.top, multiplier: 1.0, constant: 0)

let bottomConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: mainView, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1.0, constant: 0)

var rightConstraint : NSLayoutConstraint!

if index == items.count - 1 {

rightConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.right, relatedBy: NSLayoutConstraint.Relation.equal, toItem: mainView, attribute: NSLayoutConstraint.Attribute.right, multiplier: 1.0, constant: -padding)

}else{

let nextButton = items[index+1]
rightConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.right, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nextButton, attribute: NSLayoutConstraint.Attribute.left, multiplier: 1.0, constant: -padding)
}


var leftConstraint : NSLayoutConstraint!

button.topAnchor.constraint(equalTo: mainView.topAnchor, constant: padding).isActive = true
button.bottomAnchor.constraint(equalTo: mainView.bottomAnchor, constant: -padding).isActive = true

///set leading constraint
if index == 0 {
/// set first item leading anchor to mainView
button.leadingAnchor.constraint(equalTo: mainView.leadingAnchor, constant: padding).isActive = true
} else {
let prevButton: UIView = items[index - 1]
let firstItem: UIView = items[0]

leftConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.left, relatedBy: NSLayoutConstraint.Relation.equal, toItem: mainView, attribute: NSLayoutConstraint.Attribute.left, multiplier: 1.0, constant: padding)

}else{

let prevButton = items[index-1]
leftConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.left, relatedBy: NSLayoutConstraint.Relation.equal, toItem: prevButton, attribute: NSLayoutConstraint.Attribute.right, multiplier: 1.0, constant: padding)

let firstItem = items[0]

let widthConstraint = NSLayoutConstraint(item: button, attribute: .width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: firstItem, attribute: .width, multiplier: 1.0 , constant: 0)

mainView.addConstraint(widthConstraint)
/// set remaining items to previous view and set width the same as first view
button.leadingAnchor.constraint(equalTo: prevButton.trailingAnchor, constant: padding).isActive = true
button.widthAnchor.constraint(equalTo: firstItem.widthAnchor).isActive = true
}

///set trailing constraint
if index == items.count - 1 {
/// set last item trailing anchor to mainView
button.trailingAnchor.constraint(equalTo: mainView.trailingAnchor, constant: -padding).isActive = true
} else {
/// set remaining item trailing anchor to next view
let nextButton: UIView = items[index + 1]
button.trailingAnchor.constraint(equalTo: nextButton.leadingAnchor, constant: -padding).isActive = true
}

mainView.addConstraints([topConstraint, bottomConstraint, rightConstraint, leftConstraint])
}
}

func setSelectedColors(){
private func setSelectedColors() {
for item in labels {
item.textColor = unselectedLabelColor
}
Expand All @@ -217,7 +174,7 @@ import UIKit
thumbView.backgroundColor = thumbColor
}

func setFont(){
private func setFont() {
for item in labels {
item.font = font
}
Expand Down
Loading

0 comments on commit 63d192e

Please sign in to comment.