【发布时间】:2018-06-03 21:30:47
【问题描述】:
当我的 cellItem 被选中时,我想以某种方式增加一点大小,与其他的不同。实际上尺寸相同,但选择的尺寸比其他尺寸高一级。这和布局有关系吗?有什么方法吗?还是故事板?
我附上了一些照片,看看我到目前为止做了什么以及它应该是怎样的。
应该怎样
现在怎么样了。
【问题讨论】:
标签: ios swift uiview storyboard
当我的 cellItem 被选中时,我想以某种方式增加一点大小,与其他的不同。实际上尺寸相同,但选择的尺寸比其他尺寸高一级。这和布局有关系吗?有什么方法吗?还是故事板?
我附上了一些照片,看看我到目前为止做了什么以及它应该是怎样的。
应该怎样
现在怎么样了。
【问题讨论】:
标签: ios swift uiview storyboard
为该 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()
}
它会显示你所期望的效果。
【讨论】:
请试试这个代码
[UIView transitionWithView:collectionView
duration:.5
options:UIViewAnimationOptionTransitionCurlUp
animations:^{
//Set Size of Cell
cell.frame = CGRectMake(3, 14, 100, 100);
} completion:^(BOOL finished) {
}];
【讨论】:
如果你想放大一点,你可以使用:
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(例如)的单元格中,并且在选择时您可以更改该视图的边框颜色。
【讨论】:
提示: 缓存索引路径的计算布局属性,因为它们将被多次从集合视图中询问。
【讨论】: