简体   繁体   中英

How to keep UIButton floating AFTER scrollViewDidScroll(_ scrollView: UIScrollView) reached bottom?

浮动按钮的当前实现

The following is the current implementation. The grey button is floating on the scroll view. Is there a way to make the button appear once the yellow view (end of scroll view) is reached. Then keep it floating on the screen at the very bottom.

I'm using the following code:

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
}
}

Adding additional code of what I've tried so far:

    @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()
}

这样显示最新图像。按钮是恒定的,不会移动

If I understand you right you want to put button on the position which shown on the gif

Try this code:

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
    }
  }
}

UPDATE

add this code to hide your button before animation will show it

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM