CRDropdownAlert
一个简洁、可定制的 Swift 控件,是 RKDropdownAlert 和 DropdownAlert 的替代品,支持自定义视图。向每位贡献者致敬,特别是 DropdownAlert。
概述
本项目受 RKDropdownAlert 和 DropdownAlert 启发,结合了它们的优点。
- 使用 Swift 4.2 编写(撰写本文时,DropdownAlert 尚未更新至 Swift 3)
- 响应式 - 使用自动布局
- 触摸激活 - 支持通过代理人响应用户点击
- 支持自定义视图 - 不限制您只使用标题和消息。您可以创建自定义视图并将其传递给 CRDropdownAlert。
使用
CRDropdownAlert
提供了 3 种动画类型,基本
、弹簧
和 自定义
/**
Animation types the dropdown can be presented with.
- Basic: Basic, simple animation.
- Spring: Spring animation.
- Custom: Custom animation.
*/
public enum AnimationType {
case Basic(timingFunction: CAMediaTimingFunction)
case Spring(bounce: CGFloat, speed: CGFloat)
case Custom(POPPropertyAnimation)
}
示例(除了标题和消息之外,其他参数是可选的)
CRDropdownAlert.show(animationType: .Basic(timingFunction: CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeIn)), title: "New Message", message: "I'm on my way!")
自定义
以下为可自定义的类级别属性
class var defaultBackgroundColor: UIColor
class var defaultTextColor: UIColor
class var defaultTitle: String
class var defaultMessage: String
class var defaultAnimationDuration: Double
class var defaultDuration: Double
class var defaultTitleFont: UIFont
class var defaultMessageFont: UIFont
class var defaultFontSize: CGFloat
DropdownAlert 声明弹窗大小始终大于等于内容大小,但情况并不总是如此。CRDropdownAlert 通过允许消息行换行来修复此问题。还进行了一些默认布局改进,包括间距、边框和加粗。
要自定义特定属性
DropdownAlert.defaultHeight = 110
DropdownAlert.defaultBackgroundColor = UIColor.blueColor()
要使用自定义视图而不是内建的标题和消息标签(除 view 以外其余参数均为可选)
CRDropdownAlert.show(animationType: .Spring(bounce: 0.5, speed: 0.5), view: customView, backgroundColor: .white, duration: 3);
例如,这是我在 CRDropdownAlert 中创建进度条的方式
let parentView = UIView.init();
let progressView = UIProgressView(progressViewStyle: .default);
progressView.translatesAutoresizingMaskIntoConstraints = false;
parentView.addConstraint(NSLayoutConstraint(item: progressView, attribute: .left, relatedBy: .equal, toItem: parentView, attribute: .left, multiplier: 1, constant: 0));
parentView.addConstraint(NSLayoutConstraint(item: progressView, attribute: .right, relatedBy: .equal, toItem: parentView, attribute: .right, multiplier: 1, constant: 0));
parentView.addConstraint(NSLayoutConstraint(item: progressView, attribute: .top, relatedBy: .equal, toItem: parentView, attribute: .top, multiplier: 1, constant: 10));
parentView.addConstraint(NSLayoutConstraint(item: progressView, attribute: .bottom, relatedBy: .equal, toItem: parentView, attribute: .bottom, multiplier: 1, constant: -10));
parentView.addSubview(progressView);
CRDropdownAlert.show(animationType: .Spring(bounce: 0.5, speed: 0.5), view: parentView, backgroundColor: .black, duration: 3);
CRDropdownalert 使用自动布局来确定大小。在调用 show() 时传递自定义视图,视图将被水平居中并在边缘周围添加一些填充。您可能需要将 progressView.translatesAutoresizingMaskIntoConstraints 设置为 false。在自定义视图中,translatesAutoresizingMaskIntoConstraints 将自动设置为 false。
您还可以传递一个特定的窗口,让下拉菜单从这里启动。
支持
请随时提交问题或发起 PR!查看贡献指南获取更多信息。
安装
CRDropdownAlert 可通过 CocoaPods 使用。要安装,只需将以下行添加到 Podfile 中
pod "CRDropdownAlert"
作者
Charles Reitz,[email protected]。
许可证
CRDropdownAlert 兼容 MIT 许可协议。有关更多信息,请参阅 LICENSE 文件。