Code Monkey home page Code Monkey logo

floatingviewprotocol's Introduction

特性

  1. 支持拖动、自动吸附、吸附后部分隐藏
  2. 支持自定义可吸附的 edges
  3. 支持自定义水平和纵向吸附的优先级
  4. 支持自定义开始吸附的最小距离
  5. 支持自定义悬浮视图可到达区域
  6. 支持自定义添加到UIView子类或者本库提供的keyWindow

环境要求

  • iOS 8.0+
  • Swift 5.0

安装

Cocoapods

Podfile添加:

pod 'FloatingViewProtocol'

使用

简单运用

  1. 遵守FloatingViewProtocol协议

  2. 实现component属性

  3. 调用addFloatingPanGestureRecognir()方法添加拖动手势

此时悬浮视图已经支持拖动和自动吸附。水平默认超过屏幕中点,纵向默认距离屏幕边缘小于 100 时即可吸附。

自定义

在简单运用的基础上,可在悬浮视图中重写各种属性:

class FloatingView: UIView, FloatingViewProtocol {
    /// 是否可以拖拽
    var isDraggable = true
    /// 是否自动吸附
    var isAutoAdsorb = true
    /// 可自动吸附的方向
    var adsorbableEdges: FloatingAdsorbableEdges = [.left, .right]
    /// 水平方向吸附和垂直方向吸附的优先级
    var adsorbPriority: FloatingAdsorbPriority = .equal
    /// 吸附动画时间
    var adsorbAnimationDuration: TimeInterval = 0.35
    /// 停止拖拽后,最小距离 floatingEdge 边缘多少可以自动吸附
    var minAdsorbableSpacings: UIEdgeInsets = UIEdgeInsets(top: 100, left: 50, bottom: 100, right: 50)
    /// 吸附在边缘后是否自动地部分隐藏
    var isAutoPartiallyHide = true
    /// 隐藏的百分比
    var partiallyHidePercent: CGFloat  = 0.3
    /// 部分隐藏动画时长
    var partiallyHideAnimationDuration: TimeInterval = 0.35
    /// 悬浮视图可到达区域,距离父视图各边的缩进
    var floatingEdgeInsets: UIEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
}

此外当悬浮视图已经添加到父视图后,还可以对悬浮视图对象的属性直接赋值修改以上特性。

父视图

悬浮视图可以直接添加到普通的UIView子类中,也可以添加到新建的keyWindow中。

添加到普通UIView子类:

navigationController?.view.addSubview(floatingView)

添加到新建的keyWindow

floatingView.makeFloatingWindowKeyAndVisible()

对于直接添加到keyWindow中的悬浮视图,可以动态更改keyWindow根视图的状态栏样式:

floatingView.updateFloatingWindowStatusBarStyle(to: default)

代理

在悬浮视图开始拖动、结束拖动、拖动中和完成部分隐藏动画时,分别有以下代理方法:

func floatingViewDidBeginDragging(panGestureRecognizer: UIPanGestureRecognizer)
func floatingViewDidEndDragging(panGestureRecognizer: UIPanGestureRecognizer)
func floatingViewDidMove(panGestureRecognizer: UIPanGestureRecognizer)
func floatingViewFinishedPartiallyHideAnimation()

floatingviewprotocol's People

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

Watchers

 avatar  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.