我要求通过单击“向右”按钮将UICollectionView单元格滚动到数组中的下5个项目,然后单击“向左”按钮显示前5次。
任何人都可以通过提出任何建议来帮助我迅速做到这一点。
下面是编写的代码,在水平滚动中显示集合视图上的列表。
override func viewDidLoad() {
super.viewDidLoad()
self.arrFalconList = ["ic_falcon_1","ic_falcon_2","ic_falcon_3","ic_falcon_4","ic_falcon_5","ic_falcon_6","ic_falcon_7"]
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.arrFalconList.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let aFalconCell = collectionView.dequeueReusableCell(withReuseIdentifier: "FalconCollectionViewCell", for: indexPath) as! FalconCollectionViewCell
aFalconCell.imgFalcon.image = UIImage(named: self.arrFalconList[indexPath.row])
aFalconCell.lblFalcon.text = self.arrFalconList[indexPath.row]
aFalconCell.imgFalcon.layer.cornerRadius = ((aFalconCell.imgFalcon.frame.width) / 2)
aFalconCell.imgFalcon.clipsToBounds = true
if aFalconCell.isSelected {
aFalconCell.imgFalcon.layer.borderColor = UIColor.init(red: 241.0/255.0, green: 55.0/255.0, blue: 61.0/255.0, alpha: 1.0).cgColor
aFalconCell.imgFalcon.layer.borderWidth = 2
}else{
aFalconCell.imgFalcon.layer.borderColor = UIColor.clear.cgColor
aFalconCell.imgFalcon.layer.borderWidth = 0
}
return aFalconCell
}
请分享您的答案以处理“左”和“左” “右”按钮点击事件。
答案 0 :(得分:14)
我找到了查询的解决方案。
下面的代码有助于满足要求。
@IBAction func btnLeftArrowAction(_ sender: Any) {
let collectionBounds = self.collectionView.bounds
let contentOffset = CGFloat(floor(self.collectionView.contentOffset.x - collectionBounds.size.width))
self.moveCollectionToFrame(contentOffset: contentOffset)
}
@IBAction func btnRightArrowAction(_ sender: Any) {
let collectionBounds = self.collectionView.bounds
let contentOffset = CGFloat(floor(self.collectionView.contentOffset.x + collectionBounds.size.width))
self.moveCollectionToFrame(contentOffset: contentOffset)
}
func moveCollectionToFrame(contentOffset : CGFloat) {
let frame: CGRect = CGRect(x : contentOffset ,y : self.collectionView.contentOffset.y ,width : self.collectionView.frame.width,height : self.collectionView.frame.height)
self.collectionView.scrollRectToVisible(frame, animated: true)
}
答案 1 :(得分:2)
akash在Objective-C中的答案,除了我做了一点微小的改变:我需要一次滚动一个单元格,而不是一直向左或向右滚动。如果您需要该功能,请更改
float cellWidth = self.collectionView.visibleCells[0].bounds.size.width;
与
float collectionViewWidth = self.collectionView.bounds;
- (IBAction)scrollLeftAction:(id)sender
{
float cellWidth = self.collectionView.visibleCells[0].bounds.size.width;
float contentOffset = (float)(floor(self.collectionView.contentOffset.x - cellWidth));
[self moveCollectionViewToFrame:contentOffset];
}
- (IBAction)scrollRightAction:(id)sender
{
float cellWidth = self.collectionView.visibleCells[0].bounds.size.width;
float contentOffset = (float)(floor(self.collectionView.contentOffset.x + cellWidth));
[self moveCollectionViewToFrame:contentOffset];
}
- (void)moveCollectionViewToFrame:(CGFloat)contentOffset
{
CGRect frame = CGRectMake(contentOffset, self.collectionView.contentOffset.y, self.collectionView.frame.size.width, self.collectionView.frame.size.height);
[self.collectionView scrollRectToVisible:frame
animated:YES];
}
答案 2 :(得分:0)
你可以试试这段代码
let I = 1
func next_btn_clicked()
{
clooectionview.setContentOffset(CGPoint(x: (i) * clooectionview.frame.size.width, y: 0), animated: true)
I = I + 1
}