Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIButton动画扩展_Ios_Swift_Animation_Uibutton - Fatal编程技术网

Ios UIButton动画扩展

Ios UIButton动画扩展,ios,swift,animation,uibutton,Ios,Swift,Animation,Uibutton,我试图通过创建一个标准的动画功能来减少整个应用程序中使用的重复代码的冗余 我想要的结果是一个带有可选完成处理程序的按钮的动画。这是我的密码: extension UIButton { func animate(duration: TimeInterval, completion: ((Bool) -> Swift.Void)?) { UIView.animate(withDuration: duration, delay: 0.0, usingSpringWithDampi

我试图通过创建一个标准的动画功能来减少整个应用程序中使用的重复代码的冗余

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

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.performsgue以确保segue没有硬连接到情节提要上,并验证了它没有

我还尝试用以下代码声明动画函数:

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

它不是动画,因为我想你们在调用扩展方法之前忘了给按钮初始变换值

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

试着给一些延迟,比如说0.3秒。同样的结果。如果发生这种情况,动画则不会。请尝试给予一些延迟,例如0.3秒。相同的结果。发生了Segue,动画没有。