[Swift]提示 UIAlertController、XHToastSwift和FWPopupView

目录

UIAlertController

XHToastSwift

FWPopupView


UIAlertController

AlertMessage(title: "提示", message: "请检查是否设置完成");
AlertMessage(title: "提示", message: "请检查是否设置完成", delay: 1);
AlertSheet(array: ["相册", "相机", "取消"]);
AlertInputText();
// 弹窗提示
func AlertMessage(title: String, message: String) {
    let alertController = UIAlertController(title:title, message:message, preferredStyle: .alert);
    let canceAction = UIAlertAction(title:"取消",style:.cancel,handler:nil);
    let okAciton = UIAlertAction(title:"确定",style:.default,handler: {
        action in
        print("确定");
    })
    alertController.addAction(canceAction);
    alertController.addAction(okAciton);
    self.present(alertController, animated: true, completion: nil)
}

// 弹窗提示 一段时间后消失
func AlertMessage(title: String, message: String, delay: Float) {
    let alertController = UIAlertController(title:title, message:message, preferredStyle: .alert);
    let canceAction = UIAlertAction(title:"取消",style:.cancel,handler:nil);
    let okAciton = UIAlertAction(title:"确定",style:.default,handler: {
        action in
        print("确定");
    })
    alertController.addAction(canceAction);
    alertController.addAction(okAciton);
    self.present(alertController, animated: true, completion: nil)
    // 几秒钟后自动消失
    let dispatchAfter = DispatchTimeInterval.seconds(Int(delay));
    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + dispatchAfter) {
        self.presentedViewController?.dismiss(animated: false, completion: nil);
    }
}

// 底部列表提示
func AlertSheet(array: Array) {
    let alertController = UIAlertController(title:"选取照片",message:"请选择照片的方式",preferredStyle: .actionSheet);
    
    for act in array {
        var tempStyle: UIAlertAction.Style = .default;
        if (act == "取消") {
            tempStyle = .cancel;
        }
        let tempAction = UIAlertAction(title: act, style: tempStyle) { (action) in
            if (action.title == "相机") {
                print("点击相机");
            }
        }
        alertController.addAction(tempAction);
    }

    self.present(alertController, animated: true, completion: nil)
}

// 带有输入框的提示框
func AlertInputText() {
    let alertController = UIAlertController(title:"支付",message:"请输入支付密码",preferredStyle: .alert);
    alertController.addTextField{(textField:UITextField!) in
        textField.placeholder = "支付密码";
        textField.isSecureTextEntry = true;

    }
    let canceAction = UIAlertAction(title:"取消",style:.cancel,handler:nil);
    let sureAciton = UIAlertAction(title:"确定",style:.default,handler: {
        action in
        print("支付密码:\(String(describing: alertController.textFields!.last?.text))")
    })

    alertController.addAction(canceAction);
    alertController.addAction(sureAciton);
    self.present(alertController,animated:true,completion: nil);
}

XHToastSwift

GitHub:https://github.com/CoderZhuXH/XHToastSwift
cocoaPods

pod 'XHToastSwift'

示意图
[Swift]提示 UIAlertController、XHToastSwift和FWPopupView_第1张图片
使用:

XHToast.showCenterWithText("提示", duration: 1);
self.view.showXHToastCenterWithText("提示", duration: 1);

源码:
1.显示至window

// MARK:-中间显示

/**
 中间显示+自定义时间
 
 - parameter text:     文字
 - parameter duration: 自定义停留时间
 */
public class func showCenterWithText(_ text:String, duration:CGFloat)

// MARK:-上方显示

/**
 上方显示+自定义停留时间
 
 - parameter text:     文字
 - parameter duration: 自定义停留时间
 */
public class func showTopWithText(_ text:String, duration:CGFloat)

/**
 上方显示+自定义到顶部距离
 
 - parameter text:      文字
 - parameter topOffset: 自定义到顶部距离
 */
public class func showTopWithText(_ text:String,topOffset:CGFloat)

/**
 上方显示+自定义到顶部距离+自定义停留时间
 
 - parameter text:      文字
 - parameter topOffset: 自定义到顶部距离
 - parameter duration:  自定义停留时间
 */
public class func showTopWithText(_ text:String, topOffset:CGFloat,duration:CGFloat)

// MARK:-下方显示

/**
 下方显示+自定义停留时间
 
 - parameter text:     文字
 - parameter duration: 自定义停留时间
 */
public class func showBottomWithText(_ text:String,duration:CGFloat)

/**
 下方显示+自定义到底部距离
 
 - parameter text:         文字
 - parameter bottomOffset: 自定义到底部距离
 */
public class func showBottomWithText(_ text:String,bottomOffset:CGFloat)

/**
 下方显示+自定义到底部距离+自定义停留时间
 
 - parameter text:         文字
 - parameter bottomOffset: 自定义到底部距离
 - parameter duration:     自定义停留时间
 */
public class func showBottomWithText(_ text:String,bottomOffset:CGFloat,duration:CGFloat)

2.显示至view

// MARK:- 中间显示


/// 中间显示+自定义停留时间
///
/// - Parameters:
///   - text: 文字
///   - duration: 自定义停留时间
public func showXHToastCenterWithText(_ text:String , duration:CGFloat)


/// 上方显示+自定义停留时间
///
/// - Parameters:
///   - text: 文字
///   - duration: 自定义停留时间
public func showXHToastTopWithText(_ text:String,  duration:CGFloat)


// MARK:- 上方显示 

/// 上方显示+自定义到顶部距离
///
/// - Parameters:
///   - text: 文字
///   - topOffset: 自定义到顶部距离
public func showXHToastTopWithText(_ text:String,topOffset:CGFloat)


/// 上方显示+自定义到顶部距离+自定义停留时间
///
/// - Parameters:
///   - text: 文字
///   - topOffset: 自定义到顶部距离
///   - duration: 自定义停留时间
public  func showXHToastTopWithText(_ text:String,topOffset:CGFloat,duration:CGFloat) 


// MARK:- 下方显示    

/// 下方显示+自定义停留时间
///
/// - Parameters:
///   - text: 文字
///   - duration: 自定义停留时间
public func showXHToastBottomWithText(_ text:String,  duration:CGFloat)


/// 下方显示+自定义到顶部距离
///
/// - Parameters:
///   - text: 文字
///   - topOffset: 自定义到顶部距离
public func showXHToastBottomWithText(_ text:String,bottomOffset:CGFloat)


/// 下方显示+自定义到顶部距离+自定义停留时间
///
/// - Parameters:
///   - text: 文字
///   - topOffset: 自定义到顶部距离
///   - duration: 自定义停留时间
public  func showXHToastBottomWithText(_ text:String,bottomOffset:CGFloat,duration:CGFloat)

FWPopupView

GitHub:https://github.com/choiceyou/FWPopupView

FWPopupView使用:https://www.jianshu.com/p/f1bb164e81b7
cocoaPods

pod 'FWPopupView'

let alertView = FWAlertView.alert(title: "标题", detail: "描述") { (popupView, index, string) in
    print(string);
}
alertView.show();
let sheetView = FWSheetView.sheet(title: "测试", itemTitles: ["Sheet0", "Sheet1", "Sheet2", "Sheet3"], itemBlock: { (popupView, index, string) in
    print("Sheet:点击了第\(index)个按钮");
}) {
    print("点击了取消");
}
sheetView.show();
lazy var menuView1: FWMenuView = {
    
    let vProperty = FWMenuViewProperty()
    vProperty.popupCustomAlignment = .topCenter
    vProperty.popupAnimationType = .position
    vProperty.popupArrowStyle = .round
    vProperty.touchWildToHide = "1"
    vProperty.topBottomMargin = 0
    vProperty.maskViewColor = UIColor(white: 0, alpha: 0.3)
    vProperty.popupViewEdgeInsets = UIEdgeInsets(top: 40, left: 0, bottom: 0, right: 0);
    
    let menuView = FWMenuView.menu(itemTitles: ["12","1231","2342"], itemImageNames: [nil,nil,nil] as? [UIImage], itemBlock: { (popupView, index, title) in
        print("Menu:点击了第\(index)个按钮")
    }, property: vProperty)
    menuView.attachedView = but1
    
    return menuView
}()

@IBAction func tapBut(_ sender: Any) {
    menuView1.show();
}

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:([Swift]学习笔记)