【问题标题】:How to modify the size of a selected CollectionViewItem如何修改所选 CollectionViewItem 的大小
【发布时间】:2018-06-03 21:30:47
【问题描述】:

当我的 cellItem 被选中时,我想以某种方式增加一点大小,与其他的不同。实际上尺寸相同,但选择的尺寸比其他尺寸高一级。这和布局有关系吗?有什么方法吗?还是故事板?

我附上了一些照片,看看我到目前为止做了什么以及它应该是怎样的。

应该怎样

现在怎么样了。

【问题讨论】:

    标签: ios swift uiview storyboard


    【解决方案1】:
    1. 为该 greenBorderd 视图的顶部和底部约束创建出口。

    2. 假设我们将它们的值设置为 10。点击 collectionView 项时,将其 topConstraint 值更改为 0,bottomConstraint 值更改为 20。

    3. 在 collectionViewDidSelect 方法中执行此操作,并在 colletionView didDeselectItemAt 方法中将它们设置为原始值

    4. 在 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()
        }
    

    它会显示你所期望的效果。

    【讨论】:

    • .viewTopConstraint 。 -> 不是会员
    • viewTopConstraint 是视图 TopConstraint 的 IBOutlet。将其值设为 10 并为 bottomConstraint 设置相同的值。这 2 个约束将在 UiView 的顶部和底部显示空白。只需更改该约束,它将按预期为您提供 o/p。
    【解决方案2】:

    请试试这个代码

    [UIView transitionWithView:collectionView 
                      duration:.5 
                       options:UIViewAnimationOptionTransitionCurlUp 
                    animations:^{
    
        //Set Size of Cell
        cell.frame = CGRectMake(3, 14, 100, 100);
    
    } completion:^(BOOL finished) {
    
    
    }];
    

    【讨论】:

    • yes UIView.transition(with: collectionView, duration: 0.5, options: .transitionCurlUp, animations: {() -> Void in cell.frame = CGRect(x: 3, y: 14, width :100,高度:100) },完成:{(_finished: Bool) -> Void in })
    【解决方案3】:

    如果你想放大一点,你可以使用:

     override func viewDidLoad()
        {
            super.viewDidLoad()
            collectionView.allowsSelection = true
        }
    
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
        {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
            if cell.isSelected == true
            {
                cell.transform = CGAffineTransform(scaleX: 1.2, y: 2)
            }
            else
            {
                cell.transform = .identity
            }
            return cell
        }
    
        func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
        {
            let cell = collectionView.cellForItem(at: indexPath)
            cell?.isSelected = true
            UIView.animate(withDuration: 0.2, delay: 0, usingSpringWithDamping: 5, initialSpringVelocity: 0, options: [], animations:
            {
                cell?.transform = CGAffineTransform(scaleX: 1.2, y: 2)
            })
        }
    

    还有另一种选择,您可以将子视图添加到其底部约束等于 16(例如)的单元格中,并且在选择时您可以更改该视图的边框颜色。

    【讨论】:

      【解决方案4】:
      1. 通过覆盖 UICollectionViewFlowLayout 创建新的流布局
      2. 将其分配给集合视图
      3. 将所选项目索引存储在自定义流程布局中
      4. layoutAttributesForItemAtIndexPath:时修改item的边框大小

      提示: 缓存索引路径的计算布局属性,因为它们将被多次从集合视图中询问。

      【讨论】:

        猜你喜欢
        • 2017-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-08
        • 2010-11-19
        • 1970-01-01
        • 2016-04-05
        相关资源
        最近更新 更多