iOS

Swift - 开关 UISwitch

2020-09-30  本文已影响0人  微笑中的你

由于系统的开关可能不符合各种需求
尺寸大小间距等。。。
所以用控件堆一个 开关

无图无真相!


lzswitch.png

点击事件使用了代理返回,当然你可以自行修改为 block
间距颜色等改为所需即可

环境

Swift 5
依赖 SnapKit

用法

class SettingVC: LzBaseVC {

  ...
   ///懒加载开关
    lazy var switch1: LzSwitch = {
        let v = LzSwitch(frame: CGRect.zero)
        v.layer.cornerRadius = 15
        return v
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        ...
        self.view.addSubview(switch1)
        switch1.delegate = self
///约束
        switch1.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-15)
            make.height.equalTo(30)
            make.width.equalTo(60)
            make.centerY.equalTo(lblName1)
        }
///添加约束后 必须设置
        switch1.setDefaultStatus(select: true)


    }

}

extension SettingVC: LzSwitchDelegate {
    
    func click(view: LzSwitch) {
        switch view {
        case switch1:
            debugPrint("------ \(switch1.isSelected ? "开启了" : "关闭了")-------开关1")
        case switch2:
            debugPrint("------ \(switch2.isSelected ? "开启了" : "关闭了")-------开关2")
        case switch3:
            debugPrint("------ \(switch3.isSelected ? "开启了" : "关闭了")-------开关3")
        default:
            debugPrint("其他")
        }
    }

}

LzSwitch.swift

//
//  LzSwitch.swift
//  xczn
//
//  Created by lg on 2020/9/30.
//  Copyright © 2020 lxf. All rights reserved.
//

import UIKit


/// 代理
protocol LzSwitchDelegate {
    func click(view: LzSwitch)
}

class LzSwitch: UIControl {
    
    // 标记是否滑动中
    var sliding: Bool = false
    
    var delegate: LzSwitchDelegate?
    
     lazy var viewCircle: UIView = {
        let v = UIView()
        v.backgroundColor = .white
        v.isUserInteractionEnabled = false
        return v
    }()
    
    var circleRadius: CGFloat = 10

    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(viewCircle)
        viewCircle.layer.cornerRadius = self.circleRadius
        
        addTarget(self, action: #selector(changeStatus), for: .touchUpInside)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public func setDefaultStatus(select: Bool) {
        isSelected = select
        if isSelected {
            self.viewCircle.snp.remakeConstraints { (make) in
                make.left.equalToSuperview().offset(self.circleRadius/2)
                make.width.height.equalTo(self.circleRadius*2)
                make.centerY.equalToSuperview()
            }
            self.backgroundColor = AppConst.color_main

        } else {
            self.viewCircle.snp.remakeConstraints { (make) in
                make.right.equalToSuperview().offset(-self.circleRadius/2)
                make.width.height.equalTo(self.circleRadius*2)
                make.centerY.equalToSuperview()
            }
            self.backgroundColor = .lightGray
        }
        
        self.layoutIfNeeded()

    }
    
    @objc private func changeStatus() {
        if sliding {
            return
        }
        
        if isSelected {
            sliding = true
            isSelected = false
            //动画 从左边滚到右边
            UIView.animate(withDuration: 0.25, animations: {
                self.viewCircle.snp.remakeConstraints { (make) in
                    make.right.equalToSuperview().offset(-self.circleRadius/2)
                    make.width.height.equalTo(self.circleRadius*2)
                    make.centerY.equalToSuperview()
                }
                self.backgroundColor = .lightGray
                self.layoutIfNeeded()
            }) { (ok) in
                if ok {
                    self.sliding = false
                }
            }
        } else {
            sliding = true
            isSelected = true
            //动画 从右 到 左
            UIView.animate(withDuration: 0.25, animations: {
                self.viewCircle.snp.remakeConstraints { (make) in
                    make.left.equalToSuperview().offset(self.circleRadius/2)
                    make.width.height.equalTo(self.circleRadius*2)
                    make.centerY.equalToSuperview()
                }
                self.backgroundColor = AppConst.color_main
                self.layoutIfNeeded()
            }) { (ok) in
                if ok {
                    self.sliding = false
                }
            }
        }
        if let delegate = self.delegate {
            delegate.click(view: self)
        }
    }
    
}

上一篇 下一篇

猜你喜欢

热点阅读