通过图标分段控制

时间:2019-01-03 23:52:16

标签: ios swift

我正在使用MVVM体系结构且没有情节提要的情况下快速完成第二个任务,因此我尝试使用[String]类型的项目以编程方式对分段控件进行编码,效果很好,但现在我想使用图标/图像更改项目。

我试图将项目类型从[String]更改为[UIImage] ...

import UIKit

class VehicleSegmentedControl:UIControl {
    private var labels = [UILabel]()
    var thumbView = UIView()

    // here are my items of icons
    var items:[UIImage?] = [UIImage(named: "car.png"),UIImage(named: "feet.png"),UIImage(named: "transit.png")]{
        didSet{
            setupLabels()
        }
    }

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

    init(items: [UIImage]) {
        super.init(frame: CGRect.zero)
        self.items = items
        setupView()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupView()
    }

    func setupView(){
        layer.cornerRadius = frame.height / 2
        layer.borderColor = UIColor.walkthroughOrangeAccent.cgColor
        layer.borderWidth = 2
        backgroundColor = UIColor.clear
        setupLabels()
        insertSubview(thumbView, at: 0)
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        var selectedFrame = self.bounds
        let newWith = selectedFrame.width / CGFloat(items.count)
        selectedFrame.size.width = newWith
        layer.cornerRadius = frame.height / 2
        thumbView.frame = selectedFrame
        thumbView.backgroundColor = .walkthroughOrangeAccent
        thumbView.layer.cornerRadius = thumbView.frame.height / 2

        let labelHeight = self.bounds.height
        let labelWidth = self.bounds.width / CGFloat(labels.count)

        for index in 0...labels.count - 1 {
            let label = labels[index]
            let xPosition = CGFloat(index) * labelWidth
            label.frame = CGRect(x: xPosition,y: 0,width: labelWidth,height: labelHeight)
        }
    }

    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)
        }
        return false
    }

    func displayNewSelectedIndex(){
        let label = labels[selectedIndex]
        self.thumbView.frame = label.frame
    }

    func setupLabels(){
        for label in labels {
            label.removeFromSuperview()
        }
        labels.removeAll(keepingCapacity: true)
        for index in 0..<items.count{
            let label = UILabel(frame: CGRect.zero)
            //label.text = items[index - 1]
            //label.textAlignment = .center
            //label.textColor = UIColor.white.withAlphaComponent(0.8)

            //here is my ImageView
            let imgview = UIImageView(image: items[index])

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

我正在互相显示3个图标,但它们没有对齐。

0 个答案:

没有答案