야금야금

[iOS] NavigationBar Fade Animation when scroll 본문

iOS

[iOS] NavigationBar Fade Animation when scroll

hyk0425 2022. 1. 6. 16:30

Constraint Animation

아이디어스 어플처럼 화면 스크롤에 맞춰 네비게이션바가 자연스럽게 fade 되는 애니메이션을 구현하려 했으나

이렇게 애니메이션이 적용되지 않았다.

layoutIfNeeded를 상위 view에 넣었어야 했는데 카테고리바에 넣어가지고.. 엄한 것만 한참 찾았네..

결과물

코드

extension NavigationBarFadeAnimation: UITableViewDelegate {
    func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
       if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0 {
           UIView.animate(withDuration: 0.3, delay: 0, options: UIView.AnimationOptions(), animations: {
               self.tabBarTopAnchor.constant = 0
               self.customNaviBar.layer.opacity = 0
               self.view.layoutIfNeeded()
//               self.customNaviBar.layoutIfNeeded()
               
           }, completion: nil)

       } else {
           UIView.animate(withDuration: 0.3, delay: 0, options: UIView.AnimationOptions(), animations: {
               self.tabBarTopAnchor.constant = 70
               self.customNaviBar.layer.opacity = 1
               self.view.layoutIfNeeded()
//               self.customNaviBar.layoutIfNeeded()
           }, completion: nil)
       }
    }

 

참고: https://stackoverflow.com/questions/25649926/trying-to-animate-a-constraint-in-swift