如果您希望在触摸单元格时开始动画,则可以实现didHighlightItemAt
。您可能希望在didUnhighlightItemAt
中将其反转:
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
}
}
}
这产生的结果是:
transform
而不是调整frame
,确切地说是为了避免干扰/冲突autolayout。如果你真的想要,你可以通过调整前导、后继、顶部和底部约束并动画调用layoutIfNeeded()
来实现相同的事情,但这是过于复杂了。 - Roboverride var isHighlighted: Bool {
didSet {
UIView.animate(withDuration: 0.5) {
let scale: CGFloat = 0.9
self.transform = self.isHighlighted ? CGAffineTransform(scaleX: scale, y: scale) : .identity
}
}
}
这与[pgdev][1]建议的相同,但针对isHighlighted
属性。
在 UICollectionViewCell 中使用 Swift 4.2
//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)
}
}
试试这个:
在您的自定义UICollectionViewCell
中,当选择该单元格时,请更改imageView
的transform
,即
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
}
}
}
}
Swift : 4 您需要实现自定义的UICollectionViewCell
,在选中单元格时更改contentView
或imageView
的transform
。
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
})
}
}
}
你有几个选项。
你可以实现集合视图委托方法collectionView(:didSelectItemAtIndexPath:)
并在那里放置你的代码。
你可以将一个轻拍手势识别器附加到你想要响应轻拍的视图上。
你可以创建一个自定义按钮并将图像安装到其中,然后像平常一样使用按钮的IBAction方法。