在将此动画连接到我的 ViewController 并从 ViewDidAppear 调用后将此动画应用于 UIView、UIButton 或 UILabel 时,它工作正常:
UIView.transition(with: self.myButton, duration: 5, options: .transitionFlipFromRight, animations: nil, completion: nil)
但是,问题是,当我在继承自 UIView 的自定义类中输入该动画时,将其设置为 Storyboard中的该对象,并将“self”传递到“with:”参数,并将其放在 AwakeFromNib 上,它不起作用:
class myAnimatedView: UIView {
override func awakeFromNib() {
self.layoutIfNeeded()
UIView.transition(with: self, duration: 15, options: .transitionFlipFromRight, animations: nil, completion: nil)}
}
}
我应用的其他动画在此类子类的 AwakeFromNib 上运行良好 - 例如,这很好用:
UIView.animate(withDuration: 5.0, delay: 0.5, usingSpringWithDamping: 0.2, initialSpringVelocity: 5.0, options: UIViewAnimationOptions(rawValue: UInt(0)), animations:({//my animations here}))
我做错了什么?使用 Xcode 9.1、Swift 4。非常感谢
最佳答案
我认为您的代码/架构存在一些问题:
- awakeFromNib 不是触发动画的正确位置,请检查 here
你说加这个
UIView.animate(withDuration: 5.0, delay: 0.5, usingSpringWithDamping: 0.2, initialSpringVelocity: 5.0, options: UIViewAnimationOptions(rawValue: UInt(0)), animations:({//my animations here}))
在你的 awakeFromNib 中很好,但我注意到有 0.5 的延迟,所以 View 已经准备好并且在动画启动时被很好地唤醒
最终你可能会使用 didMoveToWindow() 为你的东西自动制作动画 https://developer.apple.com/documentation/uikit/uiview/1622527-didmovetowindow
关于子类化 UIView 时,Swift 过渡动画不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47503997/