使用Swift 3按下时动画单元格

Kev*_*inB 16 animation cell uicollectionview swift swift3

我的问题很简单.我想在collectionView中为单元格设置动画.实际上,我想在单元格后面显示灰色背景并缩小内部图像.

它(几乎)与Pinterest相同:

在此输入图像描述

我曾经在按钮上编码动画,但我从来没有在单元格上做过.例如,如何将单元格链接到touchUpInside或TouchDown操作?

Rob*_*Rob 55

如果要在触摸单元格时启动动画,则可以实现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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

产量:

演示


小智 8

如果您只需要为特定单元实现此功能,只需将此代码添加到您的单元实现中:

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
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这与建议 [pgdev][1] 的答案相同,但对于 isHighlighted


Bra*_*gKS 6

Swift 4.2,在 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)
        }
    }
Run Code Online (Sandbox Code Playgroud)