Giter VIP home page Giter VIP logo

Comments (4)

faganello60 avatar faganello60 commented on June 12, 2024

Hello @muhammedkarakul. Can you send your code?

from customsegmentedcontrol.

muhammedkarakul avatar muhammedkarakul commented on June 12, 2024

Hello @faganello60, thanks for quick response

i define segmentedControl like below;

private(set) lazy var segmentControl: LCSegmentedControl = { LCSegmentedControl(titles: AuthTypes.allCases.map { $0.rawValue }, textColor: .silver, selectedTextColor: .white, lineColor: .white) }()

and I makes some changes on CustomSegmentedControl class;

`import UIKit

import UIKit
protocol LCSegmentedControlDelegate:class {
func segmentedControl(_ segmentedControl: LCSegmentedControl, didIndexChanged index: Int)
}

class LCSegmentedControl: UIView {
//MARK:- Properties
private lazy var segmentTitles: [String] = []
private var buttons = UIButton
private lazy var selectorView = UIView()

private var textColor: UIColor? = .lightGray
private var selectorViewColor: UIColor? = .red
private var selectorTextColor: UIColor? = .red

private var selectorWidth: Int {
    Int(frame.width) / self.segmentTitles.count
}

var normalTitleFont: UIFont = .light14
var selectedTitleFont: UIFont = .medium16

weak var delegate: LCSegmentedControlDelegate?

private(set) var selectedIndex = 0


private lazy var stackView: UIStackView =  {
    let stackView = UIStackView(arrangedSubviews: buttons)
    stackView.axis = .horizontal
    stackView.alignment = .fill
    stackView.distribution = .fillEqually
    return stackView
}()


//MARK:- Lifecycle
convenience init(titles: [String], textColor: UIColor? = .lightGray, selectedTextColor: UIColor? = .black, lineColor: UIColor? = .red, normalTitleFont: UIFont = .light14, selectedTitleFont: UIFont = .medium16) {
    self.init()
    self.segmentTitles = titles
    self.textColor = textColor
    self.selectorTextColor = selectedTextColor
    self.selectorViewColor = lineColor
    self.normalTitleFont = normalTitleFont
    self.selectedTitleFont = selectedTitleFont
}

//MARK:- Helper methods
override func draw(_ rect: CGRect) {
    super.draw(rect)
    self.backgroundColor = .clear
    updateView()
}

private func setButtonTitles(segmentTitles:[String]) {
    self.segmentTitles = segmentTitles
    self.updateView()
}

private func setIndex(index:Int) {
    buttons.forEach({ $0.setTitleColor(textColor, for: .normal) })
    let button = buttons[index]
    selectedIndex = index
    button.setTitleColor(selectorTextColor, for: .normal)
    let selectorPosition = frame.width/CGFloat(segmentTitles.count) * CGFloat(index)
    UIView.animate(withDuration: 0.2) {
        self.selectorView.frame.origin.x = selectorPosition
    }
}

@objc func buttonAction(sender:UIButton) {
    for (buttonIndex, btn) in buttons.enumerated() {
        btn.setTitleColor(textColor, for: .normal)
        btn.titleLabel?.font = normalTitleFont

        if btn.isEqual(sender) {
            let selectorPosition = frame.width / CGFloat(segmentTitles.count) * CGFloat(buttonIndex)
            selectedIndex = buttonIndex
            delegate?.segmentedControl(self, didIndexChanged: buttonIndex)
            UIView.animate(withDuration: 0.3) {
                self.selectorView.frame.origin.x = selectorPosition
            }
            btn.setTitleColor(selectorTextColor, for: .normal)
            btn.titleLabel?.font = selectedTitleFont

        }
    }
}

private func createButton() {
    buttons.removeAll()
    subviews.forEach({$0.removeFromSuperview()})
    for buttonTitle in segmentTitles {
        let button = UIButton(type: .system)
        button.setTitle(buttonTitle, for: .normal)
        button.addTarget(self, action:#selector(LCSegmentedControl.buttonAction(sender:)), for: .touchUpInside)
        button.setTitleColor(textColor, for: .normal)
        buttons.append(button)
    }
    buttons.first?.setTitleColor(selectorTextColor, for: .normal)
    buttons.first?.titleLabel?.font = selectedTitleFont

}

private func updateView() {
    createButton()
    configSelectorView()
    prepareLayout()
}

private func prepareLayout() {

    addSubviews(stackView, selectorView)

    stackView.snp.makeConstraints { (make) in
        make.edges.equalToSuperview()
    }
    
    selectorView.snp.makeConstraints { (make) in
        make.height.equalTo(2)
        make.top.equalTo(snp.bottom)
        make.width.equalTo(selectorWidth)
    }
}

private func configSelectorView() {
    selectorView.backgroundColor = selectorViewColor
}

}
`

from customsegmentedcontrol.

faganello60 avatar faganello60 commented on June 12, 2024

Change isEqual to ==

from customsegmentedcontrol.

muhammedkarakul avatar muhammedkarakul commented on June 12, 2024

I try == and === but they not works either.

from customsegmentedcontrol.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.