μ΄λ² ν¬μ€ν μμλ μ¬μ©μκ° Cellμ ν΄λ¦ μ μ‘°κΈ λ μμ°μ€λ‘μ΄ κ²½νμ μ 곡νκΈ° μν΄ Cellμ΄ μμμ‘λ€κ° λ€μ μ»€μ§ μ μλλ‘ νλ λ°©λ²μ λν΄ μ 리νλ € ν©λλ€.
import UIKit
public protocol BounceableCell: UICollectionViewCell {
func animateBouncing(isTouched: Bool)
}
extension BounceableCell {
public func animateBouncing(isTouched: Bool) {
if isTouched {
Self.animate(withDuration: 0.4,
delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 1,
options: .allowUserInteraction) { [weak self] in
self?.transform = .init(scaleX: 0.96, y: 0.96)
}
} else {
Self.animate(withDuration: 0.4,
delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 0,
options: .allowUserInteraction) { [weak self] in
self?.transform = .identity
}
}
}
}
animateBouncing(isTouched: ) λ©μλλ₯Ό ν΅ν΄ ν°μΉ μ λλ ν°μΉκ° λλ¬μ λ Cellμ ν¬κΈ°λ₯Ό μ‘°μ νλ μ λλ©μ΄μ
μ μ€νν©λλ€.
μ λλ©μ΄μ
μ μ€νν λ μ¬μ©λλ λ§€κ°λ³μλ λ€μκ³Ό κ°μ΅λλ€.
withDuration: μ λλ©μ΄μ
μ΄ μ€νλλ μκ°delay : μ λλ©μ΄μ
μ΄ μ€νλκΈ° κΉμ§μ λκΈ° μκ°usingSpringWithDamping : Spring μ λλ©μ΄μ
μ μ§λ μ λ. 0μ κ°κΉμΈμλ‘ μ λλ©μ΄μ
μ΄ μ§λνλ ννμ΄κ³ , 1μ κ°κΉμΈμλ‘ μ§λ μμ΄ μμ°μ€λ½κ² μ§νλ©λλ€.initialSpringVelocity : μ λλ©μ΄μ
μ μ΄κΈ° μλ. 0μ κ°κΉμΈμλ‘ λλ¦¬κ² μμνλ©° 1μ κ°κΉμΈμλ‘ μ΅μ’
μλμ κ°κΉμ΄ μλλ‘ μμν©λλ€. λ³΄λ€ λΉ λ₯Έ λ°μμ 보μ¬μ£ΌκΈ° μν΄μ ν°μΉ μμλ 1λ‘ μ€μ νμμ΅λλ€.options : .allowUserInteractionμ ν΅ν΄ μ¬μ©μμμ μνΈμμ©μ νμ©νμ΅λλ€. (λ°λ‘ μ€μ νμ§ μμλ μ λμνκΈ΄ νμ΅λλ€)μ΄ν Cellμμ delegate λ©μλλ₯Ό ν΅ν΄ ν°μΉ μ μ λ©μλλ₯Ό νΈμΆνμ¬ μ λλ©μ΄μ μ 보μ¬μ€λλ€.
extension MyCollectionViewCell: BounceableCell {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
self.animateBouncing(isTouched: true)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
self.animateBouncing(isTouched: false)
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesCancelled(touches, with: event)
self.animateBouncing(isTouched: false)
}
}
μ΄ λ μμ°μ€λ¬μ΄ UXλ₯Ό μν΄ CollectionViewμ scroll eventλ³΄λ€ Cellμ touch eventμ μ°μ μμλ₯Ό λκ² μ€μ ν μ μλλ‘ μλμ κ°μ΄ CollectionViewμ delaysContentTouchesλ₯Ό μ€μ ν΄μ€λλ€.
// UICollectionView
self.delaysContentTouches = false