2017-08-12 10 views
6

私の問題は本当に簡単です。私はcollectionView内のセルをアニメーション化したいと思います。確かに、私は細胞の背後に灰色の背景を表示し、内部の画像を縮小したいと思います。Swiftを使用して押したときの細胞をアニメ化3

それはPinterestのよりも(ほぼ)同じ効果になります:

enter image description here

私は、ボタンの上にそのアニメーションを符号化するために使用されるが、私は、セルの上にそれをしたことはありません。たとえば、セルをtouchUpInsideアクションまたはTouchDownアクションにリンクするにはどうすればよいですか?

+1

私はタッチダウンアクションを出せ、と – KevinB

答えて

17

あなたがセルに触れたときにアニメーションを開始したい場合は、 didHighlightItemAtを実装できます。おそらくdidUnhighlightItemAtでそれを逆にする:

override func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) { 
    UIView.animate(withDuration: 0.5) { 
     if let cell = collectionView.cellForItem(at: indexPath) as? CustomCell {    
      cell.imageView.transform = .init(scaleX: 0.95, y: 0.95) 
      cell.contentView.backgroundColor = UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1) 
     } 
    } 
} 

override func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) { 
    UIView.animate(withDuration: 0.5) { 
     if let cell = collectionView.cellForItem(at: indexPath) as? CustomCell { 
      cell.imageView.transform = .identity 
      cell.contentView.backgroundColor = .clear 
     } 
    } 
} 

それ収量を:

demo

+0

のwonderfullの内側ときのタッチアップ解放したいと思います!それはまさに私が望んでいたものです!どうもありがとうございました ! – KevinB

0

いくつかの選択肢があります。

  • あなたは、コレクションビューのデリゲートメソッドcollecdtionView(:didSelectItemAtIndexPath:)を実装し、そこにあなたのコードを置くことができます。

  • タップに応答するタップジェスチャ認識機能をビューに追加することができます。

  • カスタムボタンを作成してイメージをインストールし、通常はボタンのIBActionメソッドを使用できます。

0

はこのお試しください:変更、カスタムUICollectionViewCell

imageViewtransformセルが選択されている場合、すなわち

class CollectionViewCell: UICollectionViewCell 
{ 
    @IBOutlet weak var imageView: UIImageView! 

    override var isSelected: Bool{ 
     didSet{ 
      UIView.animate(withDuration: 2.0) { 
       self.imageView.transform = self.isSelected ? CGAffineTransform(scaleX: 0.9, y: 0.9) : CGAffineTransform.identity 
      } 
     } 
    } 
} 
関連する問題