【问题标题】:UIButton animation extensionUIButton 动画扩展
【发布时间】:2017-10-04 11:34:26
【问题描述】:

我正在尝试通过创建标准动画函数来减少在整个应用程序中使用的重复代码的冗余。

我想要的结果是带有可选完成处理程序的按钮动画。这是我的代码:

extension UIButton {  
  func animate(duration: TimeInterval, completion: ((Bool) -> Swift.Void)?) {
    UIView.animate(withDuration: duration, delay: 0.0, usingSpringWithDamping: 0.2, initialSpringVelocity: 6.0, options: [], animations: {
      self.transform = .identity
    }, completion: completion)
  }
}

MyViewController,当我这样调用动画方法时,会发生segue,但动画不会:

myButton.animate(duration: 0.25) { _ in
  self.performSegue(withIdentifier: "mySequeIdentifier", sender: sender)
}

我注释掉了self.performSegue 以确保故事板上没有硬连线,并验证它不是。

我也试过用这段代码声明动画函数:

func animate(duration: TimeInterval, completion: @escaping (Bool)->Void?) {
    UIView.animate(withDuration: duration, delay: 0.0, usingSpringWithDamping: 0.2, initialSpringVelocity: 6.0, options: .allowUserInteraction, animations: {
      self.transform = .identity
    }, completion: completion as? (Bool) -> Void)
}

使用该代码,没有任何反应。甚至没有崩溃。

感谢您的阅读。我欢迎您的建议:我的错误在哪里。

【问题讨论】:

  • 尝试延迟 0.3 秒。
  • 同样的结果。 Segue 会发生,动画不会。

标签: ios swift animation uibutton


【解决方案1】:

它没有动画,因为我认为您在调用扩展方法之前忘记为按钮提供初始变换值。

  myButton.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
myButton.animate(duration: 0.25) { _ in
  self.performSegue(withIdentifier: "mySequeIdentifier", sender: sender)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-31
    • 1970-01-01
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多