Skip to content

enzoliu/StickyScrollView

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

StickyScrollView

Version License Platform

Sicky header for UIScrollView in Swift

Requirements

Installation

StickyScrollView is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'StickyScrollView'

If you're looking for Swift 2 version, you can get it from branch 'swift2'.

Usage

Swift 3

import StickyScrollView

// you should use UIScrollViewDelegate here.
class ViewController: UIViewController, UIScrollViewDelegate {
	override func viewDidLoad() {
        super.viewDidLoad()

        // ...
        // init your image here. (imageView)
        // ...

        // Initialize StickyScrollView
        let scrollView = StickyScrollView(frame: self.view.frame)
        scrollView.setStickyDisplayHeight(height: 150)
        scrollView.setStickyImage(imageView: imageView)
        scrollView.delegate = self

        // ... 
        // adjust scrollView contentSize
        // ...

        // add to view
        self.view.addSubview(imageView)
        self.view.addSubview(scrollView)
    }
}

Object-C
In your .h file, you need to add UIScrollViewDelegate :

@interface ViewController : UIViewController <UIScrollViewDelegate>
// ...
@end

And implement StickyScrollView in your .m file :

@import StickyScrollView;

- (void)viewDidLoad {
    [super viewDidLoad];

    // ...
    // init your image here. (imageView)
    // ...

    // Initialize StickyScrollView
    StickyScrollView *scrollView = [[StickyScrollView alloc] initWithFrame: [self.view frame]];
    [scrollView setStickyImageWithImageView: imageView];
    [scrollView setStickyDisplayHeightWithHeight: 150];
    scrollView.delegate = self;

    // ... 
    // adjust scrollView contentSize
    // ...

    // add to view
    [self.view.addSubview: imageView];
    [self.view.addSubview: scrollView];
}

Advanced usage

Set scrollView as sticky header view.

import StickyScrollView

// you should use UIScrollViewDelegate here.
class ViewController: UIViewController, UIScrollViewDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()

        // ...
        // init your image1 and image2 here. 
        // ...

        // Initialize header scroll view
        let bgScrollView = UIScrollView(frame: image1.frame)
        bgScrollView.addSubview(image1)
        bgScrollView.addSubview(image2)
        bgScrollView.contentSize = CGSize(width: image1.frame.width + image2.frame.width, height: image1.frame.height)

        // Initialize StickyScrollView
        let scrollView = StickyScrollView(frame: self.view.frame)
        scrollView.setStickyDisplayHeight(height: 150)
        scrollView.setStickyView(view: bgScrollView)  // This will add bgScrollView into scrollView
        scrollView.delegate = self

        // ... 
        // adjust scrollView contentSize
        // ...

        // add to view
        self.view.addSubview(scrollView)
    }
}

You can set scale ratio, alpha ratio or parallel moving ratio by your self, just call :

    scrollView.setScaleRatio(ratio: 1)
    scrollView.setAlphaRatio(ratio: 0.7)
    scrollView.setParallelRatio(ratio: 0.4)

Change log

v0.1.6 - Bump to Swift 3.
v0.1.4 - Fix pod [cannot satisfy optional requirement of @objc protocol] error.
v0.1.3 - Fix vertical scrolling bug.
v0.1.2 - Now support vertical scrolling behavior in sticky view area.
v0.1.1 - Now support passing gesture to sticky header view, and support UIView as a sticky header.
v0.1.0 - Initial release

Inspired from

License

StickyScrollView is available under the MIT license. See the LICENSE file for more info.

About

Sicky header for UIScrollView in Swift

Resources

License

Stars

Watchers

Forks

Packages

No packages published