2017-10-19 12 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

関連する問題