微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

同一类按钮对点击的反应不同

如何解决同一类按钮对点击的反应不同

我想构建一个可展开的按钮,比如选择器。点击该按钮将显示可供选择的选项。出于某种原因,选项按钮无法识别触摸。 这是应用程序中广泛使用的主按钮。传递的动作在 objcopy -w -L _Z3fooI* t.o t1.o && objcopy -w --globalize-symbol _Z3fooI* t1.o t2.o && readelf -Ws t2.o | grep _Z3fooI 14: 0000000000000000 7 FUNC GLOBAL DEFAULT 7 _Z3fooIiEvv 15: 0000000000000000 7 FUNC GLOBAL DEFAULT 8 _Z3fooIsEvv 16: 0000000000000000 7 FUNC GLOBAL DEFAULT 9 _Z3fooIcEvv 中触发。我在 touchesEnded

中做了一些其他的事情,比如反馈和动画
touchesBegan

具有 class RoundButton: UIButton { private let action: (()->()) init(action: @escaping ()->()) { self.action = action super.init(frame: .zero) } override func touchesEnded(_ touches: Set<UITouch>,with event: UIEvent?) { action() } } 的基本视图和同样为 RoundButton 的选项

RoundButton

以及上面基本视图的实现。儿童按钮无法识别点击的问题。点击进入下一个响应者。不明白这怎么可能,因为孩子和主按钮是相同的类型。

class ExpandableRoundButton: UIView {
    
    private var children: [RoundButton] = []
    private lazy var main: RoundButton = {
        RoundButton(action: { [ weak self] in
            self?.didTapMainButton()
        })
    }()

    init(children: [RoundButton]) {
        self.children = children
        super.init(frame: .zero)
    }

    private func didTapMainButton() {
        if children.first?.transform == .identity {
            expand()
        } else {
            collapse()
        }
    }
    
    func expand() {
        let spacing: CGFloat = 16
        children.enumerated().forEach { index,button in
            button.isHidden = false
            UIView.animate(withDuration: 0.2,delay: 0,options: [.curveEaseIn]) {
                let translation = (button.frame.width + spacing) * CGFloat(index + 1)
                button.transform = CGAffineTransform(translationX: translation,y: 0)
            } completion: { _ in }
        }
    }
    
    func collapse() {
        children.enumerated().forEach { _,button in
            UIView.animate(withDuration: 0.2,options: [.curveEaseIn]) {
                button.transform = .identity
            } completion: { _ in
                button.isHidden = true
            }
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。