GithubHelp home page GithubHelp logo

chinahui / spscrollpageview Goto Github PK

View Code? Open in Web Editor NEW

This project forked from carabina/spscrollpageview

0.0 2.0 0.0 801 KB

Help some common horizontal scroll pages to switch more smooth & easy

License: MIT License

Ruby 2.48% Objective-C 97.52%

spscrollpageview's Introduction

SPScrollPageView

Version license

SPScrollPageView

掘金 简书 博客

作用

  • 让横向页面切换更流畅顺滑,解决非相邻页面切换时带来的快速略过问题
  • 避免非相邻页面动画切换时创建非目标页面view造成的性能损耗
  • 可直接设置初始化目标位置
  • 支持无动画的直接页面切换

跳转

拖动

怎么使用

  • 使用快速构建类方法创建并设置代理sp_delegate
// # How to use #
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    SPScrollPageView *pageView = [SPScrollPageView scrollPageViewWithPageCount:5
                                                                  initialIndex:3
                                                                         frame:(CGRect){CGPointZero,screenSize}];
    pageView.sp_delegete = self;
    [self.view addSubview:pageView];

pageCount:总页面数 initialIndex:初始化页面所处的位置(0开始)

  • 在代理方法中根据位置创建并返回目标View
- (UIView *)scrollPageView:(SPScrollPageView *)pageView pageForIndex:(NSInteger)index{
    UIView *view = [pageView dequeuePageViewWithIndex:index];
    if (!view) {
        if (index) {
            view = self.subscribed;
        }else{
            view = self.news;
        }
    }
    return view;
}
  • 在代理方法中,获知当前显示的位置及View
- (void)scrollPageDidEndBounceAtPage:(UIView *)stillPage index:(NSInteger)index
{
    NSLog(@"Current page number:%ld",index);
}

在这里,你可以控制页面的刷新,更新导航条位置等操作

  • 跳转,直接使用- (void)jumpImmediatelyToIndex:(NSInteger)index animated:(BOOL)animated; index:目标位置 animated:是否使用动画

怎么应用

  1. pod 'SPScrollPageView' ,'~> 0.0.3'
  2. 直接将Gayhub上项目中的SPScrollPageView文件夹拖入工程

Gayhub:SPScrollPageView 求个star ^-^

基本原理

  1. 继承自UIScrollView
  2. 设置pagingEnabledYES,用于切换分页效果的实现
  3. 借鉴UITableview的复用机制,只创建两个Cell用于承载数据源中获取的View,避免在滚动时出现无意义的视图创建
  4. 根据位置缓存视图,管理展示视图,无需在Controller中进行冗余的处理
  5. 使用KVO监听contentOffset的变化,控制页面的复用及相关数据的更新。这里多说一点,很多情况下利用现有的UIScrollViewdelegate可以更容易获悉相关状态,但是笔者没有这么做,为什么呢?假如我们过分依赖原生代理方法,一但使用者想使用相关代理方法去重新设置了delegate,那么功能一定会出现问题。
  6. 使用UIScrollViewpanGestureRecognizer,结合第5条,监听处理手指拖动页面时的变化

写在最后

作为新年节后的恢复轮子,肯定有不妥或者不足的地方。如果大佬有任何意见,或者发现任何不足,希望大佬能留言或issue拍砖,野生鶸鸡在此拜谢

写在最后的最后

“愿**青年都摆脱冷气,只是向上走,不必听自暴自弃者流的话。”

spscrollpageview's People

Contributors

tr2e avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.