【问题标题】:Swift 3 UIView animationSwift 3 UIView 动画
【发布时间】:2016-09-14 12:00:04
【问题描述】:

自从将我的项目升级到 swift 3 后,我的自动布局约束动画无法正常工作;更具体地说,它们是捕捉到新位置而不是动画。

UIView.animate(withDuration: 0.1,
               delay: 0.1,
               options: UIViewAnimationOptions.curveEaseIn,
               animations: { () -> Void in
                   constraint.constant = ButtonAnimationValues.YPosition.DefaultOut()
                   self.layoutIfNeeded()
    }, completion: { (finished) -> Void in
    // ....
})

我知道他们添加了 UIViewPropertyAnimator 课程,但我还没有尝试。

【问题讨论】:

  • 我最近一直在寻找解决方案。许多人都有同样的问题,即使使用新的 UIViewPropertyAnimator,我也无法让它工作。也许这是 iOS 10 中未解决的错误。
  • 您是否尝试在 animate 调用之前设置常量?
  • @lkraider 是的,已经尝试过了。
  • 仍然没有明确的答案,为什么这不起作用,代码在 iOS9 sim 中运行良好。现在我已经在我的视图上设置了self.translatesAutoresizingMaskIntoConstraints = true,并且正在为原点设置动画。

标签: ios swift uiview uiviewanimation swift3


【解决方案1】:

我在 swift 3 的最新更新中也遇到了这个问题。

确切地说,每当您想要为视图设置动画时,您实际上都会在该视图的父视图上调用 layoutIfNeeded。

试试这个:

UIView.animate(withDuration: 0.1,
           delay: 0.1,
           options: UIViewAnimationOptions.curveEaseIn,
           animations: { () -> Void in
               constraint.constant = ButtonAnimationValues.YPosition.DefaultOut()
               self.superview?.layoutIfNeeded()
}, completion: { (finished) -> Void in
// ....
})

过去,他们似乎对能够重新布置您想要动画的视图很宽容。但是在文档中,您确实应该在超级视图中调用 layoutIfNeeded。

【讨论】:

  • 位置在变化,但没有动画效果。
  • 请试试这个代码,它对我有用。 UIView.animate(withDuration: 1, delay: 0, options: .curveEaseInOut , 动画: { self.bottomViewTopConstraint.constant = 0.0 self.bottomView.superview?.layoutIfNeeded() }, 完成: nil)
【解决方案2】:

首先为您的约束设置新值,然后调用 animate。

self.YOUR_CONSTRAINT.constant = NEW_VALUE
UIView.animate(withDuration: 1.0) {
    self.view.layoutIfNeeded()
}

【讨论】:

    【解决方案3】:

    升级到 swift 3.0

    像苹果默认推送动画一样查看从右到左的动画

    //intially set x = SCREEN_WIDTH
    view.frame = CGRect(x: ScreenSize.SCREEN_WIDTH, y: ScreenSize.SCREEN_HEIGHT - heightTabBar , width: ScreenSize.SCREEN_WIDTH, height: heightTabBar)
    
    UIView.animate(withDuration: 0.50, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0, options: [], animations: {
     //Set x position what ever you want
                            view.frame = CGRect(x: 0, y: ScreenSize.SCREEN_HEIGHT - heightTabBar , width: ScreenSize.SCREEN_WIDTH, height: heightTabBar)
    
                        }, completion: nil)
    

    【讨论】:

      【解决方案4】:

      Swift 3.1,Xcode 8.3.2

      这段代码很适合我。您视图上的任何更改都将以慢动作动画。

      UIView.animate(withDuration: 3.0, animations: { // 3.0 are the seconds
      
      // Write your code here for e.g. Increasing any Subviews height.
      
      self.view.layoutIfNeeded()
      
      })
      

      【讨论】:

        【解决方案5】:

        Swift 4,Xcode 10

        从右到左的搜索动画

        //初始设置 x = Your_View_Width

            viewOfSearch.frame = CGRect(x: viewOfSearch.frame.size.width, y: 0 , width: viewOfSearch.frame.size.width, height: 50)
        
            UIView.animate(withDuration: 0.50, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0, options: [], animations: {
                //Set x position what ever you want
                self.viewOfSearch.frame = CGRect(x: 0, y: 0 , width: self.viewOfSearch.frame.size.width, height: 50)
        
            }, completion: nil)
        

        【讨论】:

          【解决方案6】:
          func setView(view: UIView) {
                  UIView.transition(with: view, duration: 1.0, options: .transitionFlipFromTop, animations: {
                  })
              }
          
          • 更改选项部分以在该特定 UIView 上应用新动画。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2017-09-28
            • 2017-08-09
            • 1970-01-01
            • 2016-03-15
            • 2015-03-15
            • 2014-07-27
            • 2016-05-25
            相关资源
            最近更新 更多