子类化 UIView 时,Swift 过渡动画不起作用

标签 swift animation uiview subclass

在将此动画连接到我的 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。非常感谢

最佳答案

我认为您的代码/架构存在一些问题:

  1. awakeFromNib 不是触发动画的正确位置,请检查 here
  2. 你说加这个

    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/

相关文章:

ios - 从 IB 访问静态颜色

ios - AVAssetWriter - 设置自定义帧率

iphone - 在 UITableView 中以编程方式移动带动画的行

ios - 在 UIView 上绘制顶线和底线

ios - 在固定页脚的 UIView 中向页脚添加按钮

json - iOS - 快速下载 json

ios - 文本字段在填充数据后未格式化

javascript - 让脚本更好。烟雾不够平滑

ios - 为什么我的 CAShapeLayer 没有动画? (iOS swift )

ios - Superview 中的自定义 UIView 放置