Snapchat像tableview滚动标题

时间:2017-10-03 05:27:10

标签: ios swift uitableview uipangesturerecognizer snapchat

我一直在尝试实现像Snapchat tableview这样的滚动行为,其中向下滚动tableview移动到顶部标题后面并且列表滚动。

Stage 1

Stage 2

Stage 3

我试过下面的代码: 但它并不像Snapchat那么顺利,如果有人有任何想法请分享。

func scrollViewDidScroll(_ scrollView: UIScrollView)
{
    let CONTAINER_HEIGHT:CGFloat = 44
    let translation = scrollView.panGestureRecognizer.translation(in: scrollView.panGestureRecognizer.view!)
    if(translation.y <= 0)
    {
        //UP DIRECTION
        let percent = -translation.y / scrollView.panGestureRecognizer.view!.bounds.size.height
        print("PERCENT : \(percent)")
        switch (scrollView.panGestureRecognizer.state)
        {
        case .began:
            break;
        case .changed:
            if(percent <= 1.0 && percent >= 0)
            {
                var panSize = (CONTAINER_HEIGHT)*percent
                panSize = CONTAINER_HEIGHT-panSize
                print("Pan Value : \(panSize) -- Percent : \(percent)" )
                //panSize += 44
                self.bottomSlideTopConstraint.constant = panSize
                self.view.layoutIfNeeded()
            }
            break;
        // User is currently dragging the scroll view
        case .ended,.cancelled:
            let velocity = scrollView.panGestureRecognizer.velocity(in: scrollView.panGestureRecognizer.view)
            if (percent > 0.5 && velocity.y == 0) || velocity.y > 0
            {
                print("ENDED TRUE")
                self.bottomSlideTopConstraint.constant = 0
                UIView.animate(withDuration: 0.2, animations: {
                    self.view.layoutIfNeeded()
                })
            }
            else
            {
                print("CANCELLED")
                self.bottomSlideTopConstraint.constant = 44
                UIView.animate(withDuration: 0.2, animations: {
                    self.view.layoutIfNeeded()

                })
            }
        default:
            break;
        }
    }
}

0 个答案:

没有答案