JJFloatingActionButton

适用于 iOS 的浮动操作按钮


Swift 5.0 Version License Platform Build Status codecov Code Climate Documentation Contributions Welcome

特性预览要求安装用法作者许可

特性

预览

Preview Basics Preview Configuration

Preview Circular Preview Single Item

要求

安装

CocoaPods

CocoaPods 是 Cocoa 项目的依赖管理器。您可以使用以下命令安装它

gem install cocoapods

要使用 CocoaPods 将 JJFloatingActionButton 集成到您的 Xcode 项目中,请在您的 Podfile 中指定它

source 'https://cdn.cocoapods.org/'
platform :ios, '13.0'
use_frameworks!

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

然后,运行以下命令

pod install

手动

如果您不想使用依赖管理器,您可以手动将 JJFloatingActionButton 集成到您的项目中。

用法

快速开始

let actionButton = JJFloatingActionButton()

actionButton.addItem(title: "item 1", image: UIImage(named: "First")?.withRenderingMode(.alwaysTemplate)) { item in
  // do something
}

actionButton.addItem(title: "item 2", image: UIImage(named: "Second")?.withRenderingMode(.alwaysTemplate)) { item in
  // do something
}

actionButton.addItem(title: "item 3", image: nil) { item in
  // do something
}

view.addSubview(actionButton)
actionButton.translatesAutoresizingMaskIntoConstraints = false
actionButton.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -16).isActive = true
actionButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -16).isActive = true

// last 4 lines can be replaced with
// actionButton.display(inViewController: self)

配置

可以自定义按钮的外观和行为

actionButton.handleSingleActionDirectly = false
actionButton.buttonDiameter = 65
actionButton.overlayView.backgroundColor = UIColor(white: 0, alpha: 0.3)
actionButton.buttonImage = UIImage(named: "Dots")
actionButton.buttonColor = .red
actionButton.buttonImageColor = .white
actionButton.buttonImageSize = CGSize(width: 30, height: 30)

actionButton.buttonAnimationConfiguration = .transition(toImage: UIImage(named: "X"))
actionButton.itemAnimationConfiguration = .slideIn(withInterItemSpacing: 14)

actionButton.layer.shadowColor = UIColor.black.cgColor
actionButton.layer.shadowOffset = CGSize(width: 0, height: 1)
actionButton.layer.shadowOpacity = Float(0.4)
actionButton.layer.shadowRadius = CGFloat(2)

actionButton.itemSizeRatio = CGFloat(0.75)
actionButton.configureDefaultItem { item in
    item.titlePosition = .trailing

    item.titleLabel.font = .boldSystemFont(ofSize: UIFont.systemFontSize)
    item.titleLabel.textColor = .white
    item.buttonColor = .white
    item.buttonImageColor = .red

    item.layer.shadowColor = UIColor.black.cgColor
    item.layer.shadowOffset = CGSize(width: 0, height: 1)
    item.layer.shadowOpacity = Float(0.4)
    item.layer.shadowRadius = CGFloat(2)
}

actionButton.addItem(title: "Balloon", image: UIImage(named: "Balloon")) { item in
    // Do something
}

let item = actionButton.addItem()
item.titleLabel.text = "Owl"
item.imageView.image = UIImage(named: "Owl")
item.buttonColor = .black
item.buttonImageColor = .white
item.buttonImageColor = CGSize(width: 30, height: 30)
item.action = { item in
    // Do something
}

代理

optional func floatingActionButtonWillOpen(_ button: JJFloatingActionButton)
optional func floatingActionButtonDidOpen(_ button: JJFloatingActionButton)
optional func floatingActionButtonWillClose(_ button: JJFloatingActionButton)
optional func floatingActionButtonDidClose(_ button: JJFloatingActionButton)

示例

要运行示例项目,只需运行以下命令

pod try JJFloatingActionButton

资源

作者

Jochen Pfeiffer https://github.com/jjochen

许可

JJFloatingActionButton 在 MIT 许可下可用。 有关更多信息,请参见 LICENSE 文件。