1 Star 0 Fork 0

Danie1s / DNSPageView

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

DNSPageView

Version License Platform

English Introduction

DNSPageView一个纯Swift的轻量级、灵活且易于使用的PageView框架,titleViewcontentView可以布局在任意地方,可以纯代码初始化,也可以使用xib或者storyboard初始化,并且提供了常见样式属性进行设置。

如果你使用的开发语言是Objective-C,请使用DNSPageView-ObjC

Features:

  • 使用简单
  • 多种初始化方式
  • 灵活布局
  • 常见的样式
  • 双击titleView的回调
  • contentView滑动监听

Requirements

  • iOS 8.0+
  • Xcode 10.2+
  • Swift 5.0+

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

CocoaPods 1.1+ is required to build DNSPageView.

To integrate DNSPageView into your Xcode project using CocoaPods, specify it in your Podfile:

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

target '<Your Target Name>' do
    pod 'DNSPageView'
end

Then, run the following command:

$ pod install

Manually

If you prefer not to use any of the aforementioned dependency managers, you can integrate DNSPageView into your project manually.

Example

To run the example project, clone the repo, and run DNSPageView.xcodeproj .

Usage

直接使用PageView初始化

// 创建PageStyle,设置样式
let style = PageStyle()
style.isTitleViewScrollEnabled = true
style.isTitleScaleEnabled = true

// 设置标题内容
let titles = ["头条", "视频", "娱乐", "要问", "体育" , "科技" , "汽车" , "时尚" , "图片" , "游戏" , "房产"]

// 创建每一页对应的controller
let childViewControllers: [UIViewController] = titles.map { _ -> UIViewController in
    let controller = UIViewController()
    addChild(controller)
    return controller
}

let y = UIApplication.shared.statusBarFrame.height + (navigationController?.navigationBar.frame.height ?? 0)
let size = UIScreen.main.bounds.size

// 创建对应的PageView,并设置它的frame
// titleView和contentView会连在一起
let pageView = PageView(frame: CGRect(x: 0, y: y, width: size.width, height: size.height), style: style, titles: titles, childViewControllers: childViewControllers)
view.addSubview(pageView)

使用xib或者storyboard初始化

xib或者storyboard中拖出2个UIView,让它们分别继承PageTitleViewPageContentView,拖线到代码中

@IBOutlet weak var titleView: PageTitleView!

@IBOutlet weak var contentView: PageContentView!

对PageTitleView和PageContentView进行设置

// 创建PageStyle,设置样式
let style = PageStyle()
style.titleViewBackgroundColor = UIColor.red
style.isShowCoverView = true

// 设置标题内容
let titles = ["头条", "视频", "娱乐", "要问", "体育"]

// 设置默认的起始位置
let startIndex = 2

// 对titleView进行设置
titleView.titles = titles
titleView.style = style
titleView.currentIndex = startIndex

// 最后要调用setupUI方法
titleView.setupUI()


// 创建每一页对应的controller
let childViewControllers: [UIViewController] = titles.map { _ -> UIViewController in
    let controller = UIViewController()
    addChild(controller)
    return controller
}

// 对contentView进行设置
contentView.childViewControllers = childViewControllers
contentView.startIndex = startIndex
contentView.style = style

// 最后要调用setupUI方法
contentView.setupUI()

// 让titleView和contentView进行联系起来
titleView.delegate = contentView
contentView.delegate = titleView

使用PageViewManager初始化,再分别对titleView和contentView进行布局

创建DNSPageViewManager

private lazy var pageViewManager: PageViewManager = {
    // 创建PageStyle,设置样式
    let style = PageStyle()
    style.isShowBottomLine = true
    style.isTitleViewScrollEnabled = true
    style.titleViewBackgroundColor = UIColor.clear

    // 设置标题内容
    let titles = ["头条", "视频", "娱乐", "要问", "体育"]

    // 创建每一页对应的controller
    let childViewControllers: [UIViewController] = titles.map { _ -> UIViewController in
        let controller = UIViewController()
        addChild(controller)
        return controller
    }

    return PageViewManager(style: style, titles: titles, childViewControllers: childViewControllers)
}()

布局titleView和contentView

// 单独设置titleView的frame
navigationItem.titleView = pageViewManager.titleView
pageViewManager.titleView.frame = CGRect(x: 0, y: 0, width: 180, height: 44)

// 单独设置contentView的大小和位置,可以使用autolayout或者frame
let contentView = pageViewManager.contentView
view.addSubview(pageViewManager.contentView)
contentView.snp.makeConstraints { (maker) in
    maker.edges.equalToSuperview()
}

样式

PageStyle中提供了常见样式的属性,可以按照不同的需求进行设置,包括可以设置初始显示的页面

事件回调

DNSPageView提供了常见事件监听回调,它属于PageTitleViewDelegate的中的可选属性

/// DNSPageView的事件回调,如果有需要,请让对应的childViewController遵守这个协议
@objc public protocol PageEventHandleable: class {
    
    /// 重复点击pageTitleView后调用
    @objc optional func titleViewDidSelectSameTitle()
    
    /// pageContentView的上一页消失的时候,上一页对应的controller调用
    @objc optional func contentViewDidDisappear()
    
    /// pageContentView滚动停止的时候,当前页对应的controller调用
    @objc optional func contentViewDidEndScroll()

}

常见问题

  • style.isTitleViewScrollEnabled

    如果titles的数量比较少,建议设置style.isTitleViewScrollEnabled = falsetitleView会固定,style.titleMargin不起作用,每个title平分整个titleView的宽度,下划线的宽度等于title的宽度。

    如果标签比较多,建议设置style.isTitleViewScrollEnabled = truetitleView会滑动,下划线的宽度随着title文字的宽度变化而变化

  • 标签下划线的宽度跟随文字的宽度

    设置style.isTitleViewScrollEnabled = true,可以参考Demo中的第四种样式。

  • 由于DNSPageView是基于UIScrollView实现,那么就无法避免它的一些特性:

    • 当控制器被UINavigationController管理,且navigationBar.isTranslucent = true的时候,当前控制器的view是从y = 0开始布局的,所以为了防止部分内容被navigationBar遮挡,系统默认会给UIScrollView添加offset。如果想取消这个特性:

      • iOS 11 以前,在控制器中设置automaticallyAdjustsScrollViewInsets = false
      • iOS 11 以后引入SafeArea概念,设置UIScrollView的属性contentInsetAdjustmentBehavior = .never
      • 其实这个效果还与UIViewController的其他属性有关系,但因为各种组合的情景过于复杂,所以不在此一一描述
    • PageContentViewUICollectionView实现,所以这个特性有机会造成UICollectionView的经典警告:

      The behavior of the UICollectionViewFlowLayout is not defined because:

      the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values

      从而引发一些Bug

    • 以上只是可能出现的Bug之一,由于Demo不能覆盖所有的场景,不同的布局需求可能会引起不同的Bug,开发者需要明确了解自己的布局需求,注意细节,了解iOS布局特性,并且作出对应的调整,不能完全参照Demo

License

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

Copyright (c) 2018 Daniels <176516837@qq.com> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

一个纯Swift的轻量级、灵活且易于使用的pageView 展开 收起
Swift
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Swift
1
https://gitee.com/Danie1s/DNSPageView.git
git@gitee.com:Danie1s/DNSPageView.git
Danie1s
DNSPageView
DNSPageView
master

搜索帮助