【问题标题】:Is there a way way to add a "preprocess" to a UIButton?有没有办法向 UIButton 添加“预处理”?
【发布时间】:2016-12-02 17:33:39
【问题描述】:

我想在触摸 UIButton 时添加动画效果。有没有办法在按钮被发送到它的操作之前运行一个函数?

【问题讨论】:

  • 您可以添加图像以供选择和正常状态。这样可以指示按下按钮。

标签: ios swift xcode uibutton uiviewanimation


【解决方案1】:

当您开始按下按钮时会调用以下操作。

@IBAction internal func buttonTouchDown(_ sender: AnyObject)

当您移开手指(点击按钮)时会调用这个。

@IBAction internal func buttonTouchUpInside(_ sender: AnyObject)

因此,您可以在第一个动作开始动画,然后您可以结束动画并执行其余代码。除非您需要其他手势,否则应该足够了。

【讨论】:

    【解决方案2】:

    我认为您没有理由将触摸事件预处理为按钮。 您可以在 ViewController 的 ViewDidLoad 中声明动画,

    let animation = CABasicAnimation(keyPath: "position")
    animation.duration = 2.0
    animation.fromValue = self.testView.layer.position
    animation.toValue = CGPoint(x: self.testView.layer.position.x + 100, y: self.testView.layer.position.y)
    

    这里是动画按钮层的位置属性,你可以修改任何你想要动画的属性:)

    现在在按钮的 IBAction 中

    self.yourButton.layer.add(animation, forKey: "position")
    //this is necessary, you have to set the position again once the animation complete as core animation simply removes the animate object from layer once animation finishes 
    self.testView.layer.position = CGPoint(x: self.testView.layer.position.x + 100, y: self.testView.layer.position.y)
    //call your method do whatever you wanted to do once button tapped
    //example : lemme print my name
    print("sandeep")
    

    【讨论】:

    • 这些答案很好!谢谢。 Sandeep,我也在做一些其他的处理。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-25
    相关资源
    最近更新 更多