SCLAlertView 0.8

SCLAlertView 0.8

测试已测试
Lang语言 SwiftSwift
许可证 MIT
发布最后一次发布2018年3月
SPM支持 SPM

Viktor Radchenko 维护。




SCLAlertView

用 Swift 编写的动画警报视图,可以作为 UIAlertViewUIAlertController 的替代品。由于 UIAlertView 已弃用,并且 UIAlertController 只适用于 iOS 8.x 或更高版本,如果您的 Swift 项目需要同时支持 iOS 7.x,则 SCLAlertView 是一个理想的替代方案。

BackgroundImage_ BackgroundImage

易于使用

入门

// Get started
SCLAlertView().showInfo("Important info", subTitle: "You are great")

更新警报视图

let alertViewResponder: SCLAlertViewResponder = SCLAlertView().showSuccess("Hello World", subTitle: "This is a more descriptive text.")

// Upon displaying, change/close view
alertViewResponder.setTitle("New Title") // Rename title
alertViewResponder.setSubTitle("New description") // Rename subtitle
alertViewResponder.close() // Close view

备用警报类型

SCLAlertView().showError("Hello Error", subTitle: "This is a more descriptive error text.") // Error
SCLAlertView().showNotice("Hello Notice", subTitle: "This is a more descriptive notice text.") // Notice
SCLAlertView().showWarning("Hello Warning", subTitle: "This is a more descriptive warning text.") // Warning
SCLAlertView().showInfo("Hello Info", subTitle: "This is a more descriptive info text.") // Info
SCLAlertView().showEdit("Hello Edit", subTitle: "This is a more descriptive info text.") // Edit

对 showTitle() 的原始调用

SCLAlertView().showTitle(
    "Congratulations", // Title of view
    subTitle: "Operation successfully completed.", // String of view
    duration: 2.0, // Duration to show before closing automatically, default: 0.0
    completeText: "Done", // Optional button value, default: ""
    style: .Success, // Styles - see below.
    colorStyle: 0xA429FF,
    colorTextButton: 0xFFFFFF
)

控件

自定义外观

// SCLAlertView.SCLAppearanc has more than 15 different properties to customize. See below.

let appearance = SCLAlertView.SCLAppearance(
    kTitleFont: UIFont(name: "HelveticaNeue", size: 20)!,
    kTextFont: UIFont(name: "HelveticaNeue", size: 14)!,
    kButtonFont: UIFont(name: "HelveticaNeue-Bold", size: 14)!,
    showCloseButton: false
)

let alert = SCLAlertView(appearance: appearance)

添加按钮

let alertView = SCLAlertView()
alertView.addButton("First Button", target:self, selector:Selector("firstButton"))
alertView.addButton("Second Button") {
    println("Second button tapped")
}
alertView.showSuccess("Button View", subTitle: "This alert view has buttons")

隐藏默认关闭按钮

let appearance = SCLAlertView.SCLAppearance(
    showCloseButton: false
)
let alertView = SCLAlertView(appearance: appearance)
alertView.showSuccess("No button", subTitle: "You will have hard times trying to close me")

隐藏默认关闭按钮以及警报关闭的时间

let appearance = SCLAlertView.SCLAppearance(
    showCloseButton: false
)
let alertView = SCLAlertView(appearance: appearance)
alertView.showWarning("No button", subTitle: "Just wait for 3 seconds and I will disappear", duration: 3)

隐藏警报图标

let appearance = SCLAlertView.SCLAppearance(
    showCircularIcon: false
)
let alertView = SCLAlertView(appearance: appearance)
alertView.showSuccess("No icon", subTitle: "This is a clean alert without Icon!")

使用自定义图标

let appearance = SCLAlertView.SCLAppearance(
    showCircularIcon: true
)
let alertView = SCLAlertView(appearance: appearance)
let alertViewIcon = UIImage(named: "IconImage") //Replace the IconImage text with the image name
alertView.showInfo("Custom icon", subTitle: "This is a nice alert with a custom icon you choose", circleIconImage: alertViewIcon)

添加文本字段

// Add a text field
let alert = SCLAlertView()
let txt = alert.addTextField(title:"Enter your name")
alert.addButton("Show Name") {
    println("Text value: \(txt.text)")
}
alert.showEdit("Edit View", subTitle: "This alert view shows a text box")

使用自定义子视图代替子标题

// Example of using the view to add two text fields to the alert
// Create the subview
let appearance = SCLAlertView.SCLAppearance(
    kTitleFont: UIFont(name: "HelveticaNeue", size: 20)!,
    kTextFont: UIFont(name: "HelveticaNeue", size: 14)!,
    kButtonFont: UIFont(name: "HelveticaNeue-Bold", size: 14)!,
    showCloseButton: false
)

// Initialize SCLAlertView using custom Appearance
let alert = SCLAlertView(appearance: appearance)

// Creat the subview
let subview = UIView(frame: CGRectMake(0,0,216,70))
let x = (subview.frame.width - 180) / 2

// Add textfield 1
let textfield1 = UITextField(frame: CGRectMake(x,10,180,25))
textfield1.layer.borderColor = UIColor.greenColor().CGColor
textfield1.layer.borderWidth = 1.5
textfield1.layer.cornerRadius = 5
textfield1.placeholder = "Username"
textfield1.textAlignment = NSTextAlignment.Center
subview.addSubview(textfield1)

// Add textfield 2
let textfield2 = UITextField(frame: CGRectMake(x,textfield1.frame.maxY + 10,180,25))
textfield2.secureTextEntry = true
textfield2.layer.borderColor = UIColor.blueColor().CGColor
textfield2.layer.borderWidth = 1.5
textfield2.layer.cornerRadius = 5
textfield1.layer.borderColor = UIColor.blueColor().CGColor
textfield2.placeholder = "Password"
textfield2.textAlignment = NSTextAlignment.Center
subview.addSubview(textfield2)

// Add the subview to the alert's UI property
alert.customSubview = subview
alert.addButton("Login") {
    print("Logged in")
}

// Add Button with Duration Status and custom Colors
alert.addButton("Duration Button", backgroundColor: UIColor.brownColor(), textColor: UIColor.yellowColor(), showDurationStatus: true) {
    print("Duration Button tapped")
}

alert.showInfo("Login", subTitle: "", duration: 10)

可自定义属性列表

// Button 
kButtonFont: UIFont                     
buttonCornerRadius : CGFloat            
showCloseButton: Bool                   
kButtonHeight: CGFloat                  

// Circle Image
showCircularIcon: Bool
kCircleTopPosition: CGFloat
kCircleBackgroundTopPosition: CGFloat
kCircleHeight: CGFloat
kCircleIconHeight: CGFloat

// Text
kTitleFont: UIFont
kTitleTop:CGFloat
kTitleHeight:CGFloat
kTextFont: UIFont
kTextHeight: CGFloat
kTextFieldHeight: CGFloat
kTextViewdHeight: CGFloat

// View 
kDefaultShadowOpacity: CGFloat          
kWindowWidth: CGFloat
kWindowHeight: CGFloat
shouldAutoDismiss: Bool // Set this false to 'Disable' Auto hideView when SCLButton is tapped
fieldCornerRadius : CGFloat
contentViewCornerRadius : CGFloat

警报视图样式

enum SCLAlertViewStyle: Int {
    case Success, Error, Notice, Warning, Info, Edit, Wait
}

警报显示动画样式

// Animation Styles
public enum SCLAnimationStyle {
    case NoAnimation, TopToBottom, BottomToTop, LeftToRight, RightToLeft
}

安装

SCLAlertView 通过 CocoaPods 提供。

要安装,请在您的 Podfile 中添加以下行

pod 'SCLAlertView'

合作

我试图构建一个易于使用的 API,同时足够灵活以适应多种变体,但我确信还有改进和添加更多功能的方法,因此请随时提出想法、问题或拉取请求以共同合作。

即将到来的改进

  • 更多动画
  • 性能测试

最初是为了 Scroll Feed 应用开发的