如何在我的自定义开关切换上触发事件

所有者

我正在创建我的自定义开关,但我不知道如何创建一个自定义事件,该事件将在用户每次切换开关时触发

我知道这与它有关,UIControl但我对此一无所知。

这是我的课

@IBDesignable
public class CustomSwitch: UIView {

    enum MyCustomEvents: UInt{
        case valueChanged
    }

    @IBInspectable public var isOn: Bool = true
    @IBInspectable public var OffColor: UIColor! = .white
    @IBInspectable public var onColor: UIColor! = .green

    public let valueChanged: UIControl = UIControl()

    private var ball: UIView = UIView()
    private var ballwidth: CGFloat!

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupSwitch()
    }

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

    private func setupSwitch() {

        if isOn {
            backgroundColor = onColor
        }else {
            backgroundColor = OffColor
        }

        self.layer.cornerRadius = height/2
        setupBall()

        let tap = UITapGestureRecognizer(target: self, action: #selector(tapped(_:)))
        self.isUserInteractionEnabled = true
        self.addGestureRecognizer(tap)

    }

    private func setupBall() {
        ballwidth = height-2
        ball.frame = .init(x: 1, y: 1, width: ballwidth, height: ballwidth)
        ball.layer.cornerRadius = ballwidth/2
        ball.backgroundColor = .white

        if isOn {
            self.ball.frame.origin.x = self.width - self.ballwidth - 1
        }else {
            self.ball.frame.origin.x = 1
        }

        self.addSubview(ball)
    }

    public func toggle(_ animated: Bool) {

        isOn = !isOn

        if animated {
            UIView.animate(withDuration: 0.3) {
                if self.isOn {
                    self.ball.frame.origin.x = self.width - self.ballwidth - 1
                    self.backgroundColor = self.onColor
                }else {
                    self.ball.frame.origin.x = 1
                    self.backgroundColor = self.OffColor
                }
            }
        }else{
            if isOn {
                ball.frame.origin.x = width - ballwidth - 1
                backgroundColor = onColor
            }else {
                ball.frame.origin.x = 1
                backgroundColor = OffColor
            }
        }

    }

    @objc private func tapped(_ gesture: UIGestureRecognizer) {
        toggle(true)
    }


}

请帮忙!

罗素

您可以将基础从UIView更改为UIControl,然后在切换功能中添加值更改操作

public class CustomSwitch: UIControl {
...
...
    public func toggle(_ animated: Bool) {
    isOn = !isOn
    sendActions(for: UIControl.Event.valueChanged)
    ...
    }

然后您可以添加所需的目标

customSwitchTest.addTarget(self, action: #selector(switchChanged), for: UIControl.Event.valueChanged)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在JavaScript中获取开关切换状态(真/假)

如何在Spring中通过自定义用户事件管理日志文件切换?

如何在自定义单元格中切换开关时调用函数 - TableViewCell Swift

如何在加载页面时停止触发开关上的切换事件?

如何在角度触发自定义事件

定制开关切换按钮

如何在reactjs的渲染函数中有条件地使用开关切换

如何在我的自定义指令上触发方法?

如何使用C#检查Selenium中的开关切换状态

如何为 HTML 中的每个开关切换分配不同的值

自定义事件触发

带有标签的 CSS 开关切换过渡

JQuery ajax在开关切换时执行多次

在 vuejs 中更改按钮开关切换

如何在Android中自定义开关

如何在开关上设置自定义样式

如何在导航栏中包含自定义开关

没有更改我的自定义切换台

切换开关自定义在使用 laravel 的数据表中不起作用

选择ul li中的所有项目并启用自定义切换开关

具有简单html的自定义切换开关

根据切换开关在WooCommerce Checkout中添加自定义折扣

切换主题后如何保持我的(自定义)IDE配色方案?

如何在 getmdl 的切换上禁用涟漪效应?

如果仅手动切换了翻盖开关,如何仅触发事件

基于自定义切换开关的切换启用和禁用复选框

如何在生命周期事件上触发自定义函数?

如何在angular中创建和触发自定义事件

如何在回发时捕获并触发用户控件的自定义事件