2017-10-19 51 views
3

enter image description here如何縮放選擇Swift 3.0上的UICollectionViewCell?

我在這裏試圖如圖所示image.I嘗試添加邊框選定單元格和邊境而來的單元格內容視圖內突出UICollectionViewCell的選擇。這裏是我的嘗試:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    let cell = priorityCollectionView.cellForItem(at: indexPath) as? BCPriorityListCollectionViewCell 
    let borderWidth: CGFloat = 6 
    cell?.contentView.frame = (cell?.labelBackground.frame.insetBy(dx: +borderWidth, dy: +borderWidth))! 
    cell?.contentView.layer.borderColor = cell?.backgroundColor?.cgColor 
    cell?.contentView.layer.borderWidth = borderWidth 
} 

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) { 
    let cell = priorityCollectionView.cellForItem(at: indexPath) as? BCPriorityListCollectionViewCell 
    let borderWidth: CGFloat = 0 
    cell?.contentView.frame = (cell?.labelBackground.frame.insetBy(dx: +borderWidth, dy: +borderWidth))! 
    cell?.contentView.layer.borderColor = UIColor.clear.cgColor 
    cell?.contentView.layer.borderWidth = borderWidth 

} 

如何做到這一點?

回答

3

而不是爲所選單元格添加邊框寬度,只需使用變換比例縮放所選單元格即可。在didSelect寫這樣的代碼:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    let selectedCell = priorityCollectionView.cellForItem(at: indexPath) as? BCPriorityListCollectionViewCell 
    priorityCollectionView.bringSubview(toFront: selectedCell!) 

    UIView.animate(withDuration: 0.2, delay: 0, usingSpringWithDamping: 5, initialSpringVelocity: 0, options: [], animations: { 
     selectedCell?.transform = CGAffineTransform(scaleX: 1.2, y: 2) 
     }) 
} 

而且在didDeselect:

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) { 
    let unselectedCell = priorityCollectionView.cellForItem(at: indexPath) as? BCPriorityListCollectionViewCell 
    UIView.animate(withDuration: 0.2, delay: 0, usingSpringWithDamping: 5, initialSpringVelocity: 0, options: [], animations: { 
     unselectedCell?.transform = .identity 
    }) 
} 

結果:enter image description here