project app icon

SYButton

Platform Swift 5.0 iOS 12.0+ MIT

SYButton 是一个轻量级、高度可定制的 iOS 按钮,支持动态样式、高亮效果和加载状态。

📖 文档

Overview
---

用法

基本用法

创建和显示一个基本的 SYButton

import SYButton

let button = SYButton(title: "Tap Me", subtitle: "Button Subtitle")
button.handler = { sender in
    print("Button tapped!")
}
view.addSubview(button)
自定义按钮

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

button.icon = IconSource(image: UIImage(systemName: "heart.fill"), tintColor: .red)
button.cornerRadius = .radius(8)
button.imagePlacement = .leading
button.contentInsets = UIEdgeInsets(top: 8, left: 16, bottom: 8, right: 16)

// You can also apply a gradient background if desired:
button.backgroundView.gradientColor = [.systemBlue, .systemGreen]
加载状态
loading.gif

SYButton 提供了一种指示加载状态的方法,这对于诸如网络请求或其他异步任务之类的场景特别有用。当按钮进入加载状态时,它可以显示一个加载指示器来代替图标,并且标题可以更新以反映正在进行的过程。

button.setLoading(true, with: "Loading...")
// or simply
button.isLoading = true
自定义加载指示器

SYButton 带有几个内置的加载指示器,您可以使用它们来自定义加载状态

您可以通过将这些指示器分配给按钮的 activityIndicator 属性来设置它们

button.activityIndicator = SYBallPulseIndicator()
// or
button.activityIndicator = UIActivityIndicatorView()
SYLoadingIndicator 协议

SYLoadingIndicator 协议定义了可与 `SYButton` 一起使用的自定义加载指示器的要求。该协议包括用于控制指示器的动画和外观的属性和方法。要创建您自己的自定义加载指示器,只需遵循 SYLoadingIndicator 协议

public protocol SYLoadingIndicator: UIView {
    var isAnimating: Bool { get }
    var indicatorColor: UIColor { get set }
    
    func startAnimating()
    func stopAnimating()
}

// Custom Indicator
class CustomLoadingIndicator: UIView, SYLoadingIndicator {
    var isAnimating: Bool = false
    var indicatorColor: UIColor = .gray

    func startAnimating() {
        // Start custom animation
        isAnimating = true
    }

    func stopAnimating() {
        // Stop custom animation
        isAnimating = false
    }
}

...

button.activityIndicator = CustomLoadingIndicator()
高亮器
highlighter.ripple.gif highlighter.scale.gif highlighter.custom.gif

SYButton 支持多种高亮效果,可以在按下按钮时应用。这些高亮效果为用户提供视觉反馈,增强交互体验。高亮效果通过将高亮器分配给使用 highlighter 属性的 `SYButton` 来控制。

用法

要将高亮器应用于按钮,请将 highlighter 属性设置为符合 SYButtonHighlighter 协议的类的实例。

button.highlighter = SYScaleAlphaHighlighter()
可用高亮器

SYButton 带有几个内置的高亮器,您可以使用它们来自定义按钮的高亮效果

SYButtonHighlighter 协议

SYButtonHighlighter 协议定义了可与 SYButton 一起使用的自定义高亮器的要求。该协议包括用于启动、停止和跟踪高亮效果的方法。

public protocol SYButtonHighlighter {
    func highlight(_ button: SYButton, at location: CGPoint)
    func stopHighlight(_ button: SYButton)
    func locationMoved(_ button: SYButton, to location: CGPoint)
}

要创建您自己的自定义高亮器,只需遵循 SYButtonHighlighter 协议

class CustomHighlighter: SYButtonHighlighter {
    func highlight(_ button: SYButton, at location: CGPoint) {
        // Custom highlight effect
    }

    func stopHighlight(_ button: SYButton) {
        // Stop custom highlight effect
    }

    func locationMoved(_ button: SYButton, to location: CGPoint) {
        // Optional: Track touch movement
    }
}

...
// use the highlighter
button.highlighter = CustomHighlighter()
图片放置
imagePlacement.gif

SYButton 允许您自定义图片相对于按钮中文本的放置位置。这可以使用 imagePlacement 属性轻松配置。可用的选项是

.leading - 图片显示在文本的左侧。这是默认选项。.trailing - 图片显示在文本的右侧。.top - 图片显示在文本的上方。.bottom - 图片显示在文本的下方。

用法
let button = SYButton(title: "Example Button")
button.icon = IconSource(image: UIImage(named: "example"))
button.imagePlacement = .top

选项

选项 描述
titleLabel 公开可访问的标题标签,用于自定义。(UILabel, 默认: 系统标签)
subtitleLabel 公开可访问的副标题标签,用于自定义。(UILabel, 默认: 系统标签)
title 显示为按钮上主标题的文本。(String?, 默认: nil)
subtitle 显示为按钮上副标题的文本。(String?, 默认: nil)
icon 按钮的 IconSource,包括图像和着色颜色。(IconSource, 默认: IconSource())
isLoading 指示按钮是否处于加载状态的标志。(Bool, 默认: false)
highlighter 按下按钮时应用的高亮效果。(SYButtonHighlighter?, 默认: nil)
handler 按下按钮时将执行的闭包。(((SYButton) -> Void)?, 默认: nil)
feedback 在按钮按下时触发的可选触觉反馈配置。(Feedback?, 默认: nil)
cornerRadius 按钮的圆角半径设置。(CornerRadius, 默认: .radius(0))
imagePlacement 图标相对于标题的放置位置。(ImagePlacement, 默认: .leading)
activityIndicator isLoading 设置为 true 时使用的加载指示器。(SYLoadingIndicator, 默认: SYLoadingSpinner())
contentInsets 按钮内容的插图。(UIEdgeInsets, 默认: UIEdgeInsets(top: 7, left: 12, bottom: 7, right: 12))
backgroundColor 按钮的背景颜色。(UIColor?, 默认: nil)
alignment 按钮内内容的对齐方式。(UIStackView.Alignment, 默认: .fill)
imagePadding 图像和标题之间的间距。(CGFloat, 默认: 4)
updateHandler 允许在按钮状态更改时进行外部配置更新的处理程序。(((SYButton) -> Void)?, 默认: nil)
activityIndicator isLoading 设置为 true 时使用的加载指示器。(SYLoadingIndicator, 默认: SYLoadingSpinner())
feedback 在按钮按下时触发的可选触觉反馈配置。(Feedback?, 默认: nil)
highlighter 按下按钮时应用的高亮效果。(SYButtonHighlighter?, 默认: nil)
backgroundView 按钮的背景视图,允许自定义背景配置,如渐变。(SYButtonBackgroundView, 默认: SYButtonBackgroundView())
contentBackgroundView 包含按钮内容的视图,允许自定义配置,如边距和内边距。(UIView, 默认: UIView())

要求

安装

Swift 包管理器

要使用 Swift 包管理器将 SYButton 集成到您的项目中,请将以下内容添加到您的 Package.swift 文件中

dependencies: [
    .package(url: "https://github.com/emrearmagan/SYButton.git", from: "0.0.1")
]

手动安装

  1. 从上次发布中下载 SYButton.zip 并将其内容解压到您的项目文件夹中。
  2. 从 Xcode 项目中,从“文件”菜单中选择“添加到...”并将解压后的文件添加到其中。

贡献

欢迎贡献! 如果您想贡献,请在 GitHub 上打开一个 pull request 或 issue。

许可

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