Swift-VPSwitchToggle

此软件包是“https://github.com/VansonLeung/SwiftUI-VPSwitchToggle”的替代方案,但使用 Swift UIKit 编写。

完全可定制的 UISwitch 替代品。

支持 iOS 11 或更高版本

支持以下背景过渡动画

演示视频

dSimulator.Screen.Recording.-.iPhone.14.Pro.-.2023-07-13.at.13.23.18.mov

安装

在 SPM 中安装 Swift 包

.package(url: "https://github.com/VansonLeung/Swift-VPSwitchToggle.git", from: "1.0.1")

用法

通过界面构建器创建 UI

image image

通过代码创建 UI

        let tg = VPSwitchToggle(frame: CGRect(x: 0, y: 0, width: 0, height: csv?.frame.height ?? 0))
        tg.isOn = true
        tg.widgetForegroundMargin = 6
        tg.state_on_backgroundImage = .init(named: "f_switch_bg_moon")
        tg.state_on_backgroundColor = .init(red: 29.0/255.0, green: 32.0/255.0, blue: 47.0/255.0, alpha: 1.0)
        tg.state_on_foregroundImage = .init(named: "f_switch_button_moon")
        tg.state_on_foregroundColor = .init(red: 203.0/255.0, green: 207.0/255.0, blue: 215.0/255.0, alpha: 1.0)
        tg.state_off_backgroundImage = .init(named: "f_switch_bg_sun")
        tg.state_off_backgroundColor = .init(red: 71.0/255.0, green: 125.0/255.0, blue: 182.0/255.0, alpha: 1.0)
        tg.state_off_foregroundImage = .init(named: "f_switch_button_sun")
        tg.state_off_foregroundColor = .init(red: 244.0/255.0, green: 201.0/255.0, blue: 81.0/255.0, alpha: 1.0)
        tg.backgroundImageOrder = VPSwitchToggle.BackgroundImageOrder.verticalReversed.rawValue
        tg.addTarget(self, action: #selector(onVPSwitchToggleValueChanged(me: )), for: .valueChanged)
        stackView?.addArrangedSubview(tg)