【问题标题】:Can you make all objects of a class do the same thing with one method call?你能用一个方法调用让一个类的所有对象做同样的事情吗?
【发布时间】:2016-08-18 12:43:18
【问题描述】:

抱歉,这似乎是一个愚蠢的问题。如果我有一个继承自 UIButton 的 LargeButton 类,我有办法说当单击任何 LargeButton 时,所有 LargeButton 对象都会动画并消失?

    UIView.animateWithDuration(0.4, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
        AllLargeButtons.alpha = 0 
        }, completion: { finished in
            AllLargeButtons.hidden = true

有没有办法在不先将所有 LargeButtons 存储在数组中的情况下做这样的事情?

编辑:

class LargeButton: UIButton {
    required init?(coder: NSCoder) {
        super.init(coder: coder)

        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(oneLargeButtonTapped), name: "oneOfLargeButtonsTapped", object: nil)
    }

    func oneLargeButtonTapped() {
        UIView.animateWithDuration(0.4, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
        self.alpha = 0 // Here you will get the animation you want
        }, completion: { finished in
            self.hidden = true // Here you hide it when animation done
        })
    }

    deinit {
    NSNotificationCenter.defaultCenter().removeObserver(self)
    }
}


class viewController: UIViewController {
    @IBAction func buttonTapped(sender: UIButton) {
             NSNotificationCenter.defaultCenter().postNotificationName("oneOfLargeButtonsTapped", object: nil, userInfo: nil)
    }
}

创建大按钮:

    lazy var aButton: LargeButton = {
        let button = LargeButton()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(oneLargeButtonTapped), name: "oneOfLargeButtonsTapped", object: nil)

//        button.addTarget(self, action: #selector(oneLargeButtonTapped), forControlEvents: .TouchUpInside)

        return button
    }()

【问题讨论】:

    标签: swift animation inheritance uibutton jquery-animate


    【解决方案1】:

    在创建 LargeButton 对象时注册它以接收通知:

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(LargeButton.oneLargeButtonTapped), name: "oneOfLargeButtonsTapped", object: nil)
    

    当点击其中一个大按钮时发布通知。

    NSNotificationCenter.defaultCenter().postNotificationName("oneOfLargeButtonsTapped", object: nil, userInfo: nil)
    

    在“oneLargeButtonTapped”方法中实现所需的行为。

    不要忘记在按钮被销毁之前从通知中心取消注册。

    NSNotificationCenter.defaultCenter().removeObserver(self) 
    

    【讨论】:

    • 感谢您的回答。我应该把发布通知的代码放在哪里?在 oneLargeButtonTapped 我想对了吗?
    • 你能给我一个 oneLargeButtonTapped() 方法的例子吗?应该是 oneLargeButtonTapped(sender: UIButton) { // 在这里执行动画 }
    • @tryingtolearn - 您在接收点击的方法中发布通知。因此,当点击一个按钮时,它会通知所有其他按钮(以及它本身)。收到通知的按钮将执行 oneLargeButtonTapped 方法。在这种方法中,您应该放置动画的代码。 不要忘记从通知中注销,否则可能会导致崩溃和/或内存泄漏。
    • 对,但我如何引用每个按钮?在 OP 中,我编写了动画代码,但是如何在其中指定 LargeButton?
    • @tryingtolearn - 你在 LargeButton 类中实现它 'UIView.animateWithDuration(0.4, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: { self.alpha = 0 }, completion: {finished in self.hidden = true'
    【解决方案2】:

    不是你建议的方式,不。这将要求每个类都跟踪所有现有的对象,而他们没有。您可以通过让初始化程序将新实例添加到 static Array 来自己实现这一点,但您不应该这样做。如果您希望拥有同一类的对象,而这些对象不与其他对象一起动画,会发生什么?

    相反,您应该创建一个包含所有要设置动画的对象的数组。让LargeButtonIBAction 调用一个方法,该方法迭代对象数组并为其设置动画。

    【讨论】:

    • 或者引发一个自定义事件,然后从每个按钮实例处理它。
    • 你是什么意思,@chrisp_68?
    • @AlexanderMomchliov 如果我以编程方式编写它,我别无选择,只能为每个现有的 LargeButtons 编写动作,我猜对吗?
    • @AlexanderMomchliov 我不应该通过我的意思是通过数组来做到这一点。
    • 抱歉,我对 swift 的具体知识一无所知,但事件在技术上是相当标准的。看看这里:blog.scottlogic.com/2015/02/05/swift-events.html 和客户端,这里有一个很好的例子,说明如何使用 jquery 触发和订阅事件。 learn.jquery.com/events/introduction-to-custom-events。或者你可以使用 jquery 选择器
    猜你喜欢
    • 2015-12-14
    • 1970-01-01
    • 2020-11-19
    • 1970-01-01
    • 2014-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-24
    相关资源
    最近更新 更多