如何保持UIButton浮动后,scrollViewDidScroll(_ scrollView:UIScrollView)到达底部?

时间:2017-09-03 19:27:12

标签: ios swift uiscrollview

Current implementation of floating button

以下是当前的实施。灰色按钮浮动在滚动视图上。有没有办法在达到黄色视图(滚动视图结束)后显示按钮。然后将它漂浮在最底部的屏幕上。

我正在使用以下代码:

override func scrollViewDidScroll(scrollView: UIScrollView) {

if (scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height)) {
    //reached bottom - how to show button below yellow
    // and keep it floating as shown above
}
}

添加我迄今为止尝试过的其他代码:

    @IBOutlet weak var btnScroll: UIButton!

var startingFrame : CGRect!
var endingFrame : CGRect!

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if (scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height)) && self.btnScroll.isHidden {
        self.btnScroll.isHidden = false
        self.btnScroll.frame = startingFrame // outside of screen somewhere in bottom
        UIView.animate(withDuration: 1.0) {
            self.btnScroll.frame = self.endingFrame // where it should be placed
        }
    }
}

func configureSizes() {
    let screenSize = UIScreen.main.bounds
    let screenWidth = screenSize.width
    let screenHeight = screenSize.height

    startingFrame = CGRect(x: 0, y: screenHeight+100, width: screenWidth, height: 100)
    endingFrame = CGRect(x: 0, y: screenHeight-100, width: screenWidth, height: 100)

}

override func viewDidLoad() {
    super.viewDidLoad()

    configureSizes()
}

Latest image shown like this. Button is constant and doesn't move

1 个答案:

答案 0 :(得分:1)

如果我理解你,你想把按钮放在gif上显示的位置

试试这段代码:

override func scrollViewDidScroll(scrollView: UIScrollView) {
  if (scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height)) && self.button.isHidden {
    self.button.isHidden = false
    self.button.frame = startingFrame // outside of screen somewhere in bottom
    UIView.animate(withDuration: 1.0) {
      self.button.frame = yourFrame // where it should be placed
    }
  }
}

<强>更新

添加此代码以在动画显示之前隐藏您的按钮

override func viewDidLoad() {
    super.viewDidLoad()
    self.button.isHidden = true
    ...
}