简体   繁体   中英

how to fire an event on my custom switch toggle

i am creating my custom switch but i don't know how to create a custom event which will be fired every time a user toggle the switch

i know that it has something to do with UIControl but i don't know anything about the same.

here is my class

@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)
    }


}

please help!

You can change your base from UIView to UIControl, and then add a value changed action in your toggle function

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

and then you can add the target that you need

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM