Моя цель - добавить тень вокруг границы ячейки UICollectionView, одновременно выполняя пакетное обновление UICollectionView, в результате чего выбранная ячейка будет расширяться по высоте. Идея состоит в том, что тень вокруг границы исчезнет или появится сразу же, как в этом GIF, но следуйте фактическому размеру ячейки по мере ее роста. Как показано в коде, я специально хочу, чтобы тень была вокруг ячейки, а не внутри самой ячейки - это для поддержания надлежащих границ.
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if weekTasks[indexPath.section].count == 0 {
let emptyCell = collectionView.dequeueReusableCell(withReuseIdentifier: "emptyCellID", for: indexPath) as! EmptyCell
return emptyCell
} else {
let assignmentCell = collectionView.dequeueReusableCell(withReuseIdentifier: "assignCellID", for: indexPath) as! AssignmentCell
assignmentCell.myAssignment = weekTasks[indexPath.section][indexPath.item]
assignmentCell.contentView.backgroundColor = UIColor.white
return assignmentCell
}
}
// Sets up size of cell
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if weekTasks[indexPath.section].count == 0 {
return CGSize(width: screenWidth, height: 36 )
} else if (collectionView.indexPathsForSelectedItems?.contains(indexPath))! {
return CGSize(width: screenWidth, height: 110)
} else {
return CGSize(width: screenWidth, height: 46)
}
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell?.contentView.layer.cornerRadius = 12
cell?.contentView.layer.masksToBounds = true;
cell?.contentView.backgroundColor = UIColor.white
cell?.layer.shadowOffset = CGSize(width: 0 ,height: 1)
cell?.layer.shadowRadius = 4
cell?.layer.shadowOpacity = 0.2
collectionView.performBatchUpdates(nil, completion: { done in
if done {
print("Sweet")
}}
)
}
override func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell?.contentView.layer.cornerRadius = 0
cell?.contentView.layer.masksToBounds = true;
cell?.layer.shadowOpacity = 0
}
Моя ячейка CollectionView
class AssignmentCell: UICollectionViewCell, UIGestureRecognizerDelegate {
var heightConstraint: NSLayoutConstraint?
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
let helperView = UIView()
func setupViews() {
self.contentView.addSubview(helperView)
let topConstraint = NSLayoutConstraint(item: helperView, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: superview, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0)
let leftConstraint = NSLayoutConstraint(item: helperView, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: superview, attribute: NSLayoutAttribute.left, multiplier: 1, constant: 0)
let rightConstraint = NSLayoutConstraint(item: helperView, attribute: NSLayoutAttribute.right, relatedBy: NSLayoutRelation.equal, toItem: superview, attribute: NSLayoutAttribute.right, multiplier: 1, constant: 0)
let bottomConstraint = NSLayoutConstraint(item: helperView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: superview, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0)
let heightConstraint = helperView.heightAnchor.constraint(equalToConstant: self.frame.height)
helperView.addConstraints([topConstraint, leftConstraint, rightConstraint, heightConstraint])
helperView.addSubview(titleView)
titleView.frame = CGRect(x: 48, y: (self.frame.height / 2) - 10, width: screenWidth - 190, height: 20)
helperView.addSubview(checkboxView)
checkboxView.frame = CGRect(x: 10, y: (self.frame.height / 2) - 9, width: 18, height: 18)
}
func expandCell(to height: CGFloat) {
heightConstraint?.constant = 110
UIView.animate(withDuration: 0.3, animations: {
self.layoutIfNeeded()
}) { (completed) in
}
}
}