Coder Social home page Coder Social logo

jasnig / scrollpageview Goto Github PK

View Code? Open in Web Editor NEW
353.0 9.0 67.0 7.2 MB

简单快速灵活的集成类似网易新闻, 头条等带滑块的滚动视图,实现视图联动, 滑块, segment segmentController, scrollViewController

License: MIT License

Swift 99.75% Ruby 0.25%

scrollpageview's Introduction

#ScrollPageView

###OC版的请点这里

##使用示例效果

更新示例.gif 示例效果1.gif 示例效果2.gif示例效果3.gif

示例效果4.gif 示例效果5.gif示例效果6.gif

示例效果7.gif 示例效果8.gif 自定义下标效果.gif


可以简单快速灵活的实现上图中的效果


书写思路移步

###简书1
###简书2 ###简书3


Requirements

  • iOS 8.0+
  • Xcode 7.3 or above

Installation

CocoaPods

####1.在你的项目Podfile里面添加下面的内容

source 'https://github.com/CocoaPods/Specs.git' platform :ios, '8.0' use_frameworks!

pod 'ScrollPageView', '~> 0.1.4'

###2.终端中执行命令 pod install ###3. 使用{Project}.xcworkspace打开项目


###或者直接下载将下载文件的ScrollPageView文件夹下的文件拖进您的项目中就可以使用了

###Usage

###如果是使用cocoapods安装的需要在使用的文件中 ###import ScrollPageView

###特别说明 因为大家可能会复用同一个controller来显示内容, 这里提供两种方法

  • 在对应的controller的viewWillAppear()等生命周期里面可以根据不同的title来显示不同的内容或者刷新视图
  • 新增了一个通知ScrollPageViewDidShowThePageNotification, 你可以监听这个通知来获取到正在显示的页数, 使用的示例可以参照 SegmentStyle里面的说明, 特别需要注意的是如果你的控制器是在storyboard中初始化的那么请重写这个controller的required init?(coder aDecoder: NSCoder), 并在里面注册通知监听者

###Update (更新说明) -- 2016/04/29

  • 废弃了前面版本的使用方法(前面使用过的朋友请修改为新的使用方法), 提供了更合理的使用方法, 不需要addChildViewController, 只需要提供一个addChildViewControllers的数组即可
  • 添加了更新titles和childViewControllers的方法, 可以动态的修改和更新显示的内容

####一. 使用ScrollPageView , 提供了各种效果的组合,但是不能修改segmentView和ContentView的相对位置,两者是结合在一起的

	//1. 设置子控制器,类似
func setChildVcs() -> [UIViewController] {
    let vc1 = storyboard!.instantiateViewControllerWithIdentifier("test")
    vc1.title = "国内头条"
    let vc2 = UIViewController()
    vc2.view.backgroundColor = UIColor.greenColor()
    vc2.title = "国际要闻"
    
    let vc3 = UIViewController()
    vc3.view.backgroundColor = UIColor.redColor()
    vc3.title = "趣事"
    
    let vc4 = UIViewController()
    vc4.view.backgroundColor = UIColor.yellowColor()
    vc4.title = "囧图"
    
    let vc5 = UIViewController()
    vc5.view.backgroundColor = UIColor.lightGrayColor()
    vc5.title = "明星八卦"
    
    let vc6 = UIViewController()
    vc6.view.backgroundColor = UIColor.brownColor()
    vc6.title = "爱车"
    
    let vc7 = UIViewController()
    vc7.view.backgroundColor = UIColor.orangeColor()
    vc7.title = "国防要事"
    
    let vc8 = UIViewController()
    vc8.view.backgroundColor = UIColor.blueColor()
    vc8.title = "科技频道"
    
    let vc9 = UIViewController()
    vc9.view.backgroundColor = UIColor.brownColor()
    vc9.title = "手机专页"
    
    let vc10 = UIViewController()
    vc10.view.backgroundColor = UIColor.orangeColor()
    vc10.title = "风景图"
    
    let vc11 = UIViewController()
    vc11.view.backgroundColor = UIColor.blueColor()
    vc11.title = "段子"
    
    return [vc1, vc2, vc3,vc4, vc5, vc6, vc7, vc8, vc9, vc10, vc11]
}

    
    
    // 2.这个是必要的设置
    automaticallyAdjustsScrollViewInsets = false
    
    //3. 自定义效果组合
    var style = SegmentStyle()
    // 缩放文字
    style.scaleTitle = true
    // 颜色渐变
    style.gradualChangeTitleColor = true
    // segment可以滚动
    style.scrollTitle = true
    
    let childVcs = setChildVcs()
    // 4. 注意: 标题个数和子控制器的个数要相同
    let titles = childVcs.map { $0.title! }

	// 5. 这里的childVcs 需要传入一个包含childVcs的数组, parentViewController 传入self
    let scrollPageView = ScrollPageView(frame: CGRect(x: 0, y: 64, width: view.bounds.size.width, height: view.bounds.size.height - 64), segmentStyle: style, titles: titles, childVcs: childVcs, parentViewController: self)
    // 6.
    view.addSubview(scroll) 

####二 使用 ScrollSegmentView 和 ContentView, 提供相同的效果组合, 但是同时可以分离开segmentView和contentView,可以单独设置他们的frame, 使用更灵活

		// 1. 添加子控制器
		    // 设置childVcs
	 func setChildVcs()  -> [UIViewController]{
    	let vc1 = storyboard!.instantiateViewControllerWithIdentifier("test")
    	vc1.view.backgroundColor = UIColor.whiteColor()
    
    	let vc2 = UIViewController()
    	vc2.view.backgroundColor = UIColor.greenColor()
    
   		 return [vc1, vc2]
	 }
    
    //2. 这个是必要的设置
    automaticallyAdjustsScrollViewInsets = false
    
    // 3. 自定义效果组合
    var style = SegmentStyle()
    
    // 标题不滚动, 则每个label会平分宽度
    style.scrollTitle = false
    // 颜色渐变
    style.gradualChangeTitleColor = true
    // 遮盖
    style.showCover = true
    // 遮盖颜色
    style.coverBackgroundColor = UIColor.whiteColor()
    
    // title正常状态颜色 使用RGB空间值
    style.normalTitleColor = UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0)
    // title选中状态颜色 使用RGB空间值
    style.selectedTitleColor = UIColor(red: 235.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 1.0)
	
	// 4. 标题个数和子控制器的个数要相同
    let titles = ["国内头条", "国际要闻"]

	// 5. 初始化segmentView
    topView = ScrollSegmentView(frame: CGRect(x: 0, y: 0, width: 150, height: 28), segmentStyle: style, titles: titles)
    topView.backgroundColor = UIColor.redColor()
    topView.layer.cornerRadius = 14.0
    // 可以直接设置背景色
    //        topView.backgroundImage = UIImage(named: "test")
	
	// 6. 初始化 contentView 
    contentView = ContentView(frame: view.bounds, childVcs: setChildVcs(), parentViewController: self)
    // 7. 设置代理 必要的设置
    contentView.delegate = self // 必须实现代理方法
    
    // 8. 处理点击title时切换contentView的内容, 建议您可以直接使用和下面一样的代码
    topView.titleBtnOnClick = {[unowned self] (label: UILabel, index: Int) in
        self.contentView.setContentOffSet(CGPoint(x: self.contentView.bounds.size.width * CGFloat(index), y: 0), animated: false)
        
    }
    // 9. 添加segmentVIew 和ContentView
    navigationItem.titleView = topView
    view.addSubview(contentView)
    
    // 10. 实现代理, 只需要提供当前的segmentVIew即可
    
    extension Vc6Controller: ContentViewDelegate {
		var segmentView: ScrollSegmentView {
   		 return topView
	 	}
	}

三. 更新方法的使用:

  •       // 设置新的childVcs
      let vc1 = storyboard!.instantiateViewControllerWithIdentifier("test")
      vc1.view.backgroundColor = UIColor.redColor()
      vc1.title = "更换标题"
    
      let vc2 = UIViewController()
      vc2.view.backgroundColor = UIColor.greenColor()
      vc2.title = "换标题2"
    
      let newChildVcs = [vc1, vc2]
      // 设置新的标题
      let newTitles = newChildVcs.map {
          $0.title!
      }
      topView.reloadTitlesWithNewTitles(newTitles)
      contentView.reloadAllViewsWithNewChildVcs(newChildVcs)
      //        topView.selectedIndex(1, animated: true)
    
    let newChildVcs = currentChildVcs
    let newTitles = currentChildVcs.map { $0.title! }
    // 调用public方法刷新视图
    scrollPageView.reloadChildVcsWithNewTitles(newTitles, andNewChildVcs: newChildVcs)

这是我写的<iOS自定义控件剖析>这本书籍中的一个demo, 如果你希望知道具体的实现过程和其他的一些常用效果的实现, 那么你应该能轻易在网上下载到免费的盗版书籍.

当然作为本书的写作者, 还是希望有人能支持正版书籍. 如果你有意购买书籍, 在这篇文章中, 介绍了书籍中所有的内容和书籍适合阅读的人群, 和一些试读章节, 以及购买链接. 在你准备购买之前, 请一定读一读里面的说明. 否则, 如果不适合你阅读, 虽然书籍售价35不是很贵, 但是也是一笔损失.

如果你希望联系到我, 可以通过简书联系到我

License

ScrollPageView is released under the MIT license. See LICENSE for details.

scrollpageview's People

Contributors

jasnig avatar kemchenj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

scrollpageview's Issues

请问关于下拉刷新

基本的功能参照您的工程都已经做完了..
现在有一个问题是:我最外面的UIScrollView 不能使用SVPullToRefresh添加下拉刷新了!
上下都是响应的里面的tableview

切换tabbar问题

当前滑动到了第5个标题,此时切换tabbar,然后再切换回来。这时第5个子视图和前一个子视图都执行了(第4个子视图)viewWillAppear方法。即如果在viewWillAppear方法里输出当前titile,则控制台会出现title5和title4。这个正常么?

子控制器是tableview时,滚动高度问题

func setChildVCs() -> [UIViewController] {
var childsVC: [ChildViewController] = []
for title in topTitleArray {
let vc = ChildViewController()
vc.title = title
childsVC.append(vc)
}
return childsVC
}

ChildViewController中有个tableview
var contentTableView: UITableView!

运行后,tableview的可滚动高度变小了,最好2-3个cell看不到,要一直向上拉才能看到。怎么处理?

OMFG

Can u write in faking language that I can understand ? I can't read faking Moon runes!

关于刷新标题

你好 你的这两个框架(oc)版的我都在用 感觉很不错
但是想提议小意见
default
这种时候我要去刷星标题上的数字
default
类似这样
你虽然提供了一个刷新方法
但是当我在其他的页面下拉刷新的时候就会跳回到第一页
而我也只是简简单想更新一下标题
不想更新子控制器

从swift3更新到swift4

更新到swift4后,发现原来可以正常工作的仿简书类型的scrollpageView有问题,滑动视图到顶部时,contentView里嵌套的tableView滑动失效,需要点击一下tableView,然后才可以响应外部的scrollView滑动

首次打开含有scrollpageview的控制器

1、第一次打开含有scrollpageview的控制器时,其中scrollpageview包含的第一个vc并没有执行viewwillappear事件?只有滑动tabbar,滑动其他,再返回才会执行?这个不正常吧??

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.