在我的应用程序中,我有一个屏幕,允许用户滚动浏览他所有朋友的列表。这是通过完全编程方式 WITHOUT故事板或XIB / NIB文件-使用UICollectionView来实现的。
在屏幕顶部,有一个搜索栏,以便用户可以搜索朋友。但是,我不希望它总是显示在屏幕上。
这是屏幕外观:
我想实现淡出(在顶部)搜索栏(在上图的蓝色显示)的功能–应该[发生;意味着它应该与集合视图滚动一起发生。
如果用户向后滚动滚动条,应返回
,并且以正确的平滑速度和样式滚动视图。如果动画与用户的“拖动”一起工作
会很酷;如果用户拖动速度非常慢,则该栏应以相同速度淡出,直到不再可见。用户向上滚动并且搜索栏应淡入时也是如此。我有什么func setupCollectionView() {
let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = .vertical
flowLayout.minimumLineSpacing = 0
resultsCollectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
resultsCollectionView.contentInset = UIEdgeInsets(top: 16, left: 0, bottom: 0, right: 0)
resultsCollectionView.backgroundColor = .clear
resultsCollectionView.delegate = self
resultsCollectionView.dataSource = self
resultsCollectionView.register(SearchResultCell.self, forCellWithReuseIdentifier: "cell")
resultsCollectionView.register(SearchHeaderCell.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "header-cell")
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let headerCell = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "header-cell", for: indexPath) as! SearchHeaderCell
headerCell.setHeaderTitleText(text: "Friends")
return headerCell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 20
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: self.view.frame.width, height: 80)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: self.view.frame.width, height: 20)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! SearchResultCell
if indexPath.row != 0 {
cell.addTopBorder()
}
return cell
}
我真的不知道如何实现(以所描述的[[smooth方式),对此我将不胜感激!
override func scrollViewDidScroll(scrollView: UIScrollView) {
//1. decide on the distance from the bottom that if the user scrolls to that point you will perform an action
let minimumTrigger = scrollView.bounds.size.height + self.triggerDistanceFromBottom
// 2. Now you are checking to see that the height of all the content in the scrollview/tableView.( i.e. all the cells heights stacked ontop of eachother) is greater than the minimum height for triggering a load
if scrollView.contentSize.height > minimumTrigger {
//3. This calculated the distance from the bottom of the scrollview.
let distanceFromBottom = scrollView.contentSize.height - (scrollView.bounds.size.height - scrollView.contentInset.bottom) - scrollView.contentOffset.y
//4. then this is the crucial check.
if distanceFromBottom < self.scrollTriggerDistanceFromBottom {
// Do your stuff
}
}
}
//下做你的东西。在此块中,我将实现类似的内容。
UIView.animate(withDuration: 2.0, animations: {
//Disappear The View
self.headerView.alpha = 0.0
}
但是,当用户向后滚动超过您定义的点时,您还需要实现相反的代码。
2秒似乎是一个不错的动画,但是您可以根据需要自定义。让我知道是否有帮助
编辑------------------------------------根据您更新的评论...如果只希望它进行筛选...则需要移动放置标题的位置。如果标题视图不在tableView内,则它将始终停留在顶部。如果将标题视图添加到tableView中,它将始终位于顶部,并且始终与用户一起滚动到屏幕外。