Animate cell when pressed using Swift 3

后端 未结 6 2055
感情败类
感情败类 2020-12-23 10:30

My problem is really simple. I would like to animate a cell within a collectionView. Indeed, I would like to show a grey background behind the cell and scale down the image

相关标签:
6条回答
  • 2020-12-23 11:00

    If you need implement this functionality only for the specific cell, just add this code to you cell implementation:

    override var isHighlighted: Bool {
      didSet {
        UIView.animate(withDuration: 0.5) {
          let scale: CGFloat = 0.9
          self.transform = self.isHighlighted ? CGAffineTransform(scaleX: scale, y: scale) : .identity
        }
      }
    }
    

    It is the same answer that suggested [pgdev][1] but for isHighlighted

    0 讨论(0)
  • 2020-12-23 11:02

    Try this:

    In your custom UICollectionViewCell, change the imageView transform when the cell is selected, i.e

    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
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-23 11:11

    If you want to start animation when you touch on the cell, you can implement didHighlightItemAt. You probably want to reverse it in 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
            }
        }
    }
    

    That yields:

    0 讨论(0)
  • 2020-12-23 11:13

    Swift : 4 You need to implement custom UICollectionViewCell, change the contentView OR imageView transform when the cell is selected

    override var isSelected: Bool {
                    didSet{
                           UIView.animate(withDuration: 1.0, animations: 
                            {
                                 self.contentView.transform = self.isSelected ? CGAffineTransform(scaleX: 0.95, y: 0.95) : CGAffineTransform.identity
                                 self.contentView.backgroundColor = UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1)
                            }) { (true) in
                            UIView.animate(withDuration: 0.5, animations:
                            {
                                 self.contentView.transform = self.isSelected ?  CGAffineTransform(scaleX: 1.0, y: 1.0) : CGAffineTransform.identity
                                 self.contentView.backgroundColor = UIColor.clear
                            })
                        } 
                     }
                 }
    
    0 讨论(0)
  • 2020-12-23 11:17

    Swift 4.2, inside the UICollectionViewCell

        //MARK:- Events
        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            super.touchesBegan(touches, with: event)
            animate(isHighlighted: true)
        }
    
        override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
            super.touchesEnded(touches, with: event)
            animate(isHighlighted: false)
        }
    
        override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
            super.touchesCancelled(touches, with: event)
            animate(isHighlighted: false)
        }
    
        //MARK:- Private functions
        private func animate(isHighlighted: Bool, completion: ((Bool) -> Void)?=nil) {
            let animationOptions: UIView.AnimationOptions = [.allowUserInteraction]
            if isHighlighted {
                UIView.animate(withDuration: 0.5,
                               delay: 0,
                               usingSpringWithDamping: 1,
                               initialSpringVelocity: 0,
                               options: animationOptions, animations: {
                                self.transform = .init(scaleX: 0.96, y: 0.96)
                }, completion: completion)
            } else {
                UIView.animate(withDuration: 0.5,
                               delay: 0,
                               usingSpringWithDamping: 1,
                               initialSpringVelocity: 0,
                               options: animationOptions, animations: {
                                self.transform = .identity
                }, completion: completion)
            }
        }
    
    0 讨论(0)
  • 2020-12-23 11:25

    You have several options.

    • You can implement the collection view delegate method collectionView(:didSelectItemAtIndexPath:) and put your code there.

    • You can attach a tap gesture recognizer to your view that you want to respond to taps.

    • You Can create a custom button and install an image into it and then use the IBAction method of the button as normal.

    0 讨论(0)
提交回复
热议问题