你不应该在你的 tableView 中实现它,
你应该把它添加到你已经实现你的标签的你的 homeTabs
您既不能通过添加为 IBOutlet 来添加它,也不能只是将其声明为按钮
这是我的代码(我的竞赛标签中有一个浮动按钮)
@IBOutlet weak var leaderBoardBtn: UIButton!
//为你的按钮添加bottom-top-leading-trailing约束
@IBOutlet weak var leadBottom: NSLayoutConstraint!
///这会导致我的按钮浮动
// 这是我的按钮底部对 superView.bottom 的约束
然后在 viewDidLoad 中:
NotificationCenter.default.addObserver(self, selector: #selector(leadAnime(_:)),name:NSNotification.Name(rawValue: "animate"), object: nil)
然后在 viewDidLoad 之外的地方添加这个:
@objc func leadAnime(_ notification: NSNotification) {
if let count = notification.userInfo?["top"] as? Bool{
if count{
leadBottom.constant = 20
UIView.animate(withDuration: 0.3, animations: {
self.view.layoutIfNeeded()
})
}else{
leadBottom.constant = -100
UIView.animate(withDuration: 0.3, animations: {
self.view.layoutIfNeeded()
})
}
}
}
现在在你的表格视图控制器中:
var scrollS :CGFloat = 0
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "animate"), object: nil, userInfo: ["top":false])
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "animate"), object: nil, userInfo: ["top":true]) }
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollS - scrollView.contentOffset.y < 0{
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "animate"), object: nil, userInfo: ["top":true])
}else{
if scrollS - scrollView.contentOffset.y > 0{
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "animate"), object: nil, userInfo: ["top":false])
}
}
scrollS = scrollView.contentOffset.y
}
override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
if scrollS - scrollView.contentOffset.y < 0{
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "animate"), object: nil, userInfo: ["top":true])
}else{
if scrollS - scrollView.contentOffset.y > 0{
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "animate"), object: nil, userInfo: ["top":false])
}
}
scrollS = scrollView.contentOffset.y
}