我想以某种方式选择我的cellItem来增加一点大小,这与其他方法有所不同。实际上大小相同,但是一个选择比另一个高一个。这与布局有关吗?有什么方法吗?还是情节提要?

我附上了一些照片,以查看到目前为止我的工作以及应该做的事情。
ios - 如何修改所选CollectionViewItem的大小-LMLPHP

应该如何

ios - 如何修改所选CollectionViewItem的大小-LMLPHP

现在怎么样。

最佳答案

  • 为greenBorderd视图的顶部和底部约束创建出口。
  • 假设我们将其值设置为10。单击collectionView项时,将其topConstraint值更改为0,将bottomConstraint值更改为20。
  • 在collectionViewDidSelect方法和colletionView didDeselectItemAt方法中执行此操作,将它们设置为原始值
  • 在UICollectionView的cellForItem方法中编写相同的代码。

  • 请参见下面的代码
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
               let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TableCollectionViewCell", for: indexPath) as! TableCollectionViewCell
    
                if cell.isSelected == true{
                        self.changeSelectedCellFrame(cell)
                }
                else
                {
                        self.changeDeselectedCellFrame(cell)
                }
                return cell
        }
    
    
        func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
                let cell = collectionView.cellForItem(at: indexPath) as! TableCollectionViewCell
                self.changeSelectedCellFrame(cell)
    
        }
    
    
    
        func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    
               if  let cell = collectionView.cellForItem(at: indexPath) as?TableCollectionViewCell
               {
                        self.changeDeselectedCellFrame(cell)
               }
        }
    
    
        func changeSelectedCellFrame(_ cell:TableCollectionViewCell) {
                cell.layoutIfNeeded()
                cell.viewTopConstraint.constant = 0
                cell.viewBottomConstraint.constant = 20
                cell.layoutIfNeeded()
        }
    
    
        func changeDeselectedCellFrame(_ cell:TableCollectionViewCell) {
                cell.layoutIfNeeded()
                cell.viewTopConstraint.constant = 10
                cell.viewBottomConstraint.constant = 10
                cell.layoutIfNeeded()
        }
    

    它会显示出与您预期相同的效果。

    关于ios - 如何修改所选CollectionViewItem的大小,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47920988/

    10-10 14:21
    查看更多