如何在不截取阴影的情况下遮罩UICollectionView单元内的视图

时间:2018-11-20 04:34:40

标签: swift uicollectionview uicollectionviewcell clipstobounds

我的目标是使单击该单元格时显示的视图隐藏在其扩展范围内,并在该单元格扩展时显示,而不会切断该单元格的阴影。单元格外部显示的视图包括该类的标签(微积分3和Econ 101),注释字段和灰线。

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.clear
        assignmentCell.layer.shadowOffset = CGSize(width: 0 ,height: 1)
        assignmentCell.layer.shadowRadius = 4
        return assignmentCell
    }
}

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    do {
        let cell = collectionView.cellForItem(at: indexPath)
        cell?.backgroundColor = UIColor.white
        cell?.layer.cornerRadius = 12
        let animation = CABasicAnimation(keyPath: "shadowOpacity")
        animation.fromValue = 0
        animation.toValue = 0.2
        animation.duration = 0.3
        cell?.layer.add(animation, forKey: animation.keyPath)
        cell?.layer.shadowOpacity = 0.2
    }

    collectionView.performBatchUpdates(nil, completion: nil)
}

以下是我的集合视图单元格类中的内容:

override var isSelected: Bool {
    didSet{
        if self.isSelected
        {
            self.addSubview(notesField)
            notesField.frame = CGRect(x: 50, y: 38, width: screenWidth - 60, height: 30)

            self.addSubview(seperator)
            seperator.frame = CGRect(x: 24, y: 71, width: self.frame.width - 48, height: 0.5)

            self.addSubview(classButton)
            classButton.frame = CGRect(x: 10, y: 80, width: 60, height: 20)
        }
    }
}

The response I get.

0 个答案:

没有答案