ios - UIButton 动画扩展

标签 ios swift animation uibutton

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

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

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 以确保 segue 不是硬连接到 Storyboard上,并验证它不是。

我还尝试用这段代码声明动画函数:

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

使用该代码,什么也不会发生。甚至没有崩溃。

感谢阅读。我欢迎您就我的错误所在提出建议。

最佳答案

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

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

关于ios - UIButton 动画扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46563800/

相关文章:

javascript - 更改 2 个元素的不透明度以产生交叉淡入淡出效果

android - Android 中的滑动动画

javascript - 查询 : Increase animation speed during animation

ios - 以编程方式加载自定义 uiView,就像它们是单元格一样?

ios - Worklight 6.1 在 iOS Swift native 应用程序中处理用户登录

swift - 从 Amazon DynamoDB 的表中检索一个项目,然后将其传递给 numberOfItemsInSection (Swift 4)?

ios - Parse 的 CurrentUser 不保留刷新的数据

iphone - 尽管设置了contentsScale,为什么CAShapeLayer仍然太大?

ios - 不使用 Firebase 身份验证的实时数据库的 Firebase 规则

ios - UIStackView:将 subview 推到边缘 ("Unlimited"间距)