Bauly 是一个简洁的小型库,用于在你的应用中显示紧凑的消息横幅。其设计灵感来源于 iOS 13 及更高版本中可见的系统横幅。
Bauly 可以通过 Swift Package Manager 安装。要在你的项目中使用它,打开 Xcode,前往菜单 File -> Swift Packages -> Add Package Dependency,并粘贴此仓库的 URL
https://github.com/wiencheck/Bauly.git
使用 Bauly 既有趣又简单。你所需要做的就是提供要显示的值,并可选择性地调整横幅的显示方式。
要显示横幅,请使用 present
方法
public class func present(withConfiguration configuration: BaulyView.Configuration,
presentationOptions: Bauly.PresentationOptions = .init(),
completion: (@MainActor (Bauly.PresentationState) -> Void)? = nil)
要设置横幅的内容,你可以使用 BaulyView.Configuration 结构体
public extension BaulyView {
struct Configuration {
public var title: String?
public var subtitle: String?
public var image: UIImage?
}
}
配置的每个属性都是可选的。如果未设置任何值,则相应的元素将从横幅中隐藏。
除了提供配置值之外,你还可以在 completion
代码块中直接自定义横幅视图。
PresentationState
枚举包含 .willPresent(BaulyView)
case,你可以使用它来获取对横幅的引用,并像自定义任何其他 UIView
一样自定义它(BaulyView
是 UIControl
的子类)
BaulyView
暴露了对 3 个主要元素的访问
titleLabel
: UILabel
对象,显示在顶部subtitleLabel
: UILabel
对象,显示在标题标签下方iconView
: UIImageView
,显示在两个标签旁边UIBlurEffect
用于横幅的背景。其样式可以通过设置 backgroundBlurEffectStyle
值来更改,默认使用 .prominent
样式。
Bauly.present(withConfiguration: configuration,
completion: { state in
switch state {
case .willPresent(let banner):
banner.overrideUserInterfaceStyle = .dark
banner.tintColor = .purple
banner.titleLabel.textColor = .yellow
banner.iconView.preferredSymbolConfiguration = .init(pointSize: 26)
default:
break
}
})
当调用 present
方法时,你可以为你展示横幅提供自己的选项。
public extension Bauly {
struct PresentationOptions {
public var topPadding: CGFloat
public var animationDuration: TimeInterval
public var dismissAfter: TimeInterval
public var animationDelay: TimeInterval
public var presentImmediately: Bool
public var windowScene: UIWindowScene?
public var feedbackStyle: UIImpactFeedbackGenerator.FeedbackStyle?
public var isDismissedByTap: Bool
}
}
所有属性都有默认值,因此你可以选择要修改的属性。
topPadding
: 横幅与安全区域顶部边距之间的空间量。animationDuration
: 展示/消失动画的持续时间(秒)。dismissAfter
: 横幅从屏幕上消失后的时间(秒)。animationDelay
: 动画开始的延迟。presentImmediately
: 控制横幅是否等待队列中的轮到它,还是立即显示。windowScene
: 用于显示横幅的 Window scene。feedbackStyle
: 横幅展示时发生的触感反馈样式。isDismissedByTap
: 指示点击横幅是否应将其关闭。默认情况下,每次调用 present
时,新的横幅都会被放置在队列的末尾,并在之前的所有横幅都显示完毕后才展示。
可以更改此行为,使新的横幅放置在队列中的下一个位置,并立即展示,从而导致屏幕上的任何其他横幅提前滑出屏幕。为此,请将传递给 present 方法的 PresentationOptions
上的 presentImmediately
属性设置为 true
var options = Bauly.PresentationOptions()
options.presentImmediately = true
Bauly.present(withConfiguration: configuration,
presentationOptions: options,
completion: { state in
...
要手动关闭横幅,请使用 dismiss(completion:)
方法。
它接受一个可选的闭包,该闭包在当前横幅从屏幕上消失后被调用。如果在调用此方法时未显示任何横幅,则不会调用 completion
。
如果你想确保在调用此方法之前横幅是可见的,你可以使用 currentBanner(in windowScene:)
方法并检查其返回值。
guard Bauly.currentBanner() != nil else {
return
}
Bauly.dismiss {
// Continue after dismissing the banner
}
present
方法接受一个可选的闭包,当用户点击展示的横幅时,该闭包会被调用
Bauly.present(withConfiguration: configuration,
presentationOptions: options,
onTap: { banner in
print("Banner was tapped!")
},
...
MIT
查看我在 App Store 中的应用!,下载和购买应用内购买对我在开发更多内容方面有很大帮助!