Twitter-Like Scrolling Title

后端 未结 3 1619
离开以前
离开以前 2021-02-01 09:10

On the Twitter iOS app when you scroll past your name in the profile section below the navigation bar, your name begins to scroll into view on the navigation bar itself and stic

3条回答
  •  执笔经年
    2021-02-01 10:03

    the code above is good, clear and simple for scrolling of title only.

    but if you want to make a view same as Twitter, you can use this tutorial:

    http://www.thinkandbuild.it/implementing-the-twitter-ios-app-ui/

    this tutorial is implemented in swift.

    you can download the source code by clicking on button "Download Source" in end of this tutorial.

    or go directly to github: https://github.com/ariok/TB_TwitterUI

    below we will explain how implement the fuction scrollViewDidScroll:

    1- These are the first lines for the scrollViewDidScroll function:

    var offset = scrollView.contentOffset.y
    var avatarTransform = CATransform3DIdentity
    var headerTransform = CATransform3DIdentity
    

    Here we get the current vertical offset and we initialize two transformations that we are going to setup later on with this function.

    2- manage the Pull Down action

    if offset < 0 {
    
         let headerScaleFactor:CGFloat = -(offset) / header.bounds.height
         let headerSizevariation = ((header.bounds.height * (1.0 + headerScaleFactor)) - header.bounds.height)/2.0
         headerTransform = CATransform3DTranslate(headerTransform, 0, headerSizevariation, 0)
         headerTransform = CATransform3DScale(headerTransform, 1.0 + headerScaleFactor, 1.0 + headerScaleFactor, 0)
    
         header.layer.transform = headerTransform
    }
    

    3- manage the scroll up/down

    else{
      // Header -----------
    
       headerTransform = CATransform3DTranslate(headerTransform, 0, max(-offset_HeaderStop, -offset), 0)
    
    //  ------------ Label
    
       let labelTransform = CATransform3DMakeTranslation(0, max(-distance_W_LabelHeader, offset_B_LabelHeader - offset), 0)
                headerLabel.layer.transform = labelTransform
    
    //  ------------ Blur
    
       headerBlurImageView?.alpha = min (1.0, (offset - offset_B_LabelHeader)/distance_W_LabelHeader)
    
    // Avatar -----------
    
       let avatarScaleFactor = (min(offset_HeaderStop, offset)) / avatarImage.bounds.height / 1.4 // Slow down the animation
       let avatarSizeVariation = ((avatarImage.bounds.height * (1.0 + avatarScaleFactor)) - avatarImage.bounds.height) / 2.0
       avatarTransform = CATransform3DTranslate(avatarTransform, 0, avatarSizeVariation, 0)
       avatarTransform = CATransform3DScale(avatarTransform, 1.0 - avatarScaleFactor, 1.0 - avatarScaleFactor, 0)
    
       if offset <= offset_HeaderStop {
    
          if avatarImage.layer.zPosition < header.layer.zPosition{
                        header.layer.zPosition = 0
                    }
    
          }else {
            if avatarImage.layer.zPosition >= header.layer.zPosition{
                        header.layer.zPosition = 2
                    }
                }}
    

    4- Apply Transformations

    header.layer.transform = headerTransform
    avatarImage.layer.transform = avatarTransform
    

提交回复
热议问题