[iOS] 🎨 UI, UX κ°œμ„  - Bounceable Cell

CharlieΒ·2024λ…„ 3μ›” 14일
0

이번 ν¬μŠ€νŒ…μ—μ„œλŠ” μ‚¬μš©μžκ°€ 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
profile
Hello

0개의 λŒ“κΈ€