【问题标题】:Swift UIView.animateWithDuration disappears while loading contentSwift UIView.animateWithDuration 在加载内容时消失
【发布时间】:2016-01-25 07:11:48
【问题描述】:

我使用了一个简单的动画,它会向下滑动一个小视图(这个视图包含一些标签和一个 imageView)。 动画有效,但是当我尝试设置标签和 imageView 的内容时,视图消失了。

有什么帮助吗?

UIView.animateWithDuration(1, delay: 0,
                usingSpringWithDamping: springDamping,
                initialSpringVelocity: velocity, options: option!, animations: {
                    self.sliderUserInfo.frame.origin = self.sliderUserInfoPosition
                }, completion: {finished  in
                    if finished {
                        self.loadContentView()
                    }
            })

【问题讨论】:

    标签: ios swift swift2 uianimation


    【解决方案1】:

    修改你的代码为:

        var newFrame = self.sliderUserInfo.frame
        newFrame.origin = self.sliderUserInfoPosition
        UIView.animateWithDuration(1, delay: 0,
            usingSpringWithDamping: springDamping,
            initialSpringVelocity: velocity, options: option!, animations: {
                self.sliderUserInfo.frame = newFrame
            }, completion: {finished  in
                if finished {
                    self.loadContentView()
                }
        })
    

    【讨论】:

      【解决方案2】:

      自动布局将视图放回它所属的位置。在您的动画中,不要更改帧原点,而是更改视图和超级视图顶部之间的约束的约束常量。

      要么创建一个:

      var cHeight: NSLayoutConstraint!
      
      // Start the bottom of the view off the screen
      cHeight = NSLayoutConstraint(item: sliderUserInfo, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1.0, constant: -1.0)
      

      或者从情节提要中引用它:

      @IBOutlet var cHeight: NSLayoutConstraint!
      

      然后动画变化:

      // Animate the change of the constraint
      self.cHeight.constant = self.sliderUserInfo.frame.height
      // Note we animate the layoutIfneeded
      UIView.animateWithDuration(1, delay: 0,
                  usingSpringWithDamping: springDamping,
                  initialSpringVelocity: velocity, options: option!, animations: {
                      self.sliderUserInfo.layoutIfNeeded()
                  }, completion: {finished  in
                      if finished {
                          self.loadContentView()
                      }
              })
      

      【讨论】:

        猜你喜欢
        • 2021-11-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-24
        • 1970-01-01
        • 2022-07-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多