【发布时间】:2020-04-04 09:13:22
【问题描述】:
选中后我需要在集合视图中添加动画下划线栏。
下面是我的代码:
class TestViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {
@IBOutlet var segmentCollectionView: UICollectionView!
var segmentTitle = ["Transport","Hotels","Food","Beverages","Boardings"]
var selectedIndex = 0
override func viewDidLoad() {
super.viewDidLoad()
self.collectionViewFitScreen()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return segmentTitle.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SegmentCollectionViewCell", for: indexPath) as! SegmentCollectionViewCell
let segmentValue = segmentTitle[indexPath.item]
cell.nameLbl.text = segmentValue
if selectedIndex == indexPath.item{
cell.underlineBarView.backgroundColor = #colorLiteral(red: 0.1921568627, green: 0.2, blue: 0.3333333333, alpha: 1)
}else{
cell.underlineBarView.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.selectedIndex = indexPath.item
self.segmentCollectionView.selectItem(at: indexPath, animated: false, scrollPosition: .centeredVertically)
self.segmentCollectionView.scrollToItem(at: indexPath, at: [.centeredHorizontally], animated: true)
self.segmentCollectionView.reloadData()
}
通过上述方法我已经实现了这个Segment-Selection。 但我需要的是像这样Segment-Scroll-Animate.How to do 这样的动画?任何帮助表示赞赏。
【问题讨论】:
-
根据您的代码,您正在更改取消线栏的颜色而不是栏的位置,所以我认为您需要将该栏放在集合视图之外,只需更改取消线栏的位置和这样你就可以实现你的动画了。
-
那么当uiview在collection view之外时如何根据cell改变它的位置呢?
标签: swift uicollectionview uisegmentedcontrol