iOS-Swift-自定义AlertView

作者: 轩辕小羽 | 来源:发表于2015-12-07 14:10 被阅读1154次

前言


昨天有位网友和我说想要AlertView的Swift版(OC版请见我另一篇博客),于是今天这边博客就诞生啦!
如果有其他网友有需求的都可以和我说,我会在空余时间把博客写出来的.

效果图:

CustomAlertView

上代码

首先创建一个CustomAlertView的类,该类继承自NSobject

CustomAlertView.swift

然后在CustomAlertView.swift中写入以下代码

import UIKit
// 定义代理
protocol CustomAlertViewDelegate {
    //代理方法
    func didClicPassIdentifire(identifire:String)
}
class CustomAlertView: NSObject {
    // 代理属性
    var delegate : CustomAlertViewDelegate?
     func layoutAlertView(dataArr:Array<String>)->UIView{
        let buttonH = CGFloat(61)
        let buttonW = CGFloat(250);
        // 通过数组长度创建view
        let alertView = UIView(frame:CGRectMake(0, 0, buttonW,CGFloat(CGFloat(dataArr.count)*buttonH)))
        for  i:Int in 0..<dataArr.count {
            // 创建容器view
            let view = UIView(frame: CGRectMake(0, CGFloat(i)*buttonH, buttonW, buttonH))
            view.backgroundColor = UIColor.whiteColor()
            // 创建button
            let button = UIButton(type: UIButtonType.System)
            button.frame = CGRectMake(0, 0, buttonW, buttonH)
            button.addTarget(self, action:Selector("buttonClickABC:"), forControlEvents: UIControlEvents.TouchUpInside)
            button.setTitle(dataArr[i], forState: UIControlState.Normal)
            view.addSubview(button)
            // 判断是否有取消
            if dataArr[i] == "取消"{
                button.tintColor = UIColor.whiteColor()
                view.backgroundColor = UIColor.blueColor()
            }else{
                // 不是取消绘制分割线
                button.tintColor = UIColor.lightGrayColor()
                let lineView = UIView(frame: CGRectMake(0, 60, buttonW, 1))
                lineView.backgroundColor = UIColor.lightGrayColor()
                view.addSubview(lineView)
            }
            alertView.addSubview(view)
        }
        return alertView
    }
    // 提示点击
    func buttonClickABC(button:UIButton) {
        // 提示代理执行方法
        self.delegate?.didClicPassIdentifire(button.titleLabel!.text!)
    }
}

在ViewController.swift中调用

import UIKit

class ViewController: UIViewController,CustomAlertViewDelegate{
    // 只有把类定义定义在全局才可以把点击方法封装到类中
    let alertView = CustomAlertView()
    // 提示view
    var customAlertView:UIView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // 搭建提示view
        layoutAlertView()
    }
    // 搭建提示view
    func layoutAlertView(){
        // 定义代理
        alertView.delegate = self;
        // 传值
        customAlertView = alertView.layoutAlertView(["测试1","测试2","测试3","确定","取消"])
        // 定义位置
        customAlertView.center = self.view.center
        // 切圆
        // Bool.init(1) 代表yes
        customAlertView.layer.masksToBounds = Bool.init(1)
        customAlertView.layer.cornerRadius = 10;
        // 隐藏
        customAlertView.alpha = 0.0
        customAlertView.hidden = true
        // 添加到控制器view上
        self.view.addSubview(customAlertView)
    }
    // 提示出现
    @IBAction func alertAction(sender: UIButton) {
        // UIView动画 出现
        UIView.animateWithDuration(0.1) { () -> Void in
            self.customAlertView.alpha = 1.0
            self.customAlertView.hidden = false
        }
    }
    // 代理方法
    func didClicPassIdentifire(identifire: String) {
        print(identifire)
        // UIView动画 隐藏
        UIView.animateWithDuration(0.1, animations: { () -> Void in
            self.customAlertView.alpha = 0.0
            }) { (Bool) -> Void in
            self.customAlertView.hidden = true
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

来张效果图

SwiftCustomAlertView

GitHub:https://github.com/Lafree317/Swfit-CustomAlertView


本人还是一只小菜鸡,不过是一只热心肠的菜鸡,如果有需要帮助或者代码中有更好的建议的话可以发邮件到lafree317@163.com中,我们一起进步XD

相关文章

  • iOS-Swift-自定义AlertView

    前言 昨天有位网友和我说想要AlertView的Swift版(OC版请见我另一篇博客),于是今天这边博客就诞生啦!...

  • 自定义AlertView

    自定义AlertView 之囧事 昨天被 AlertView、AlertController虐了 ...然鹅发现...

  • IOS 日常随笔

    自己的demo 自定义Alertview -- LSAlertView图片浏览器 -- LSShowImgs倒...

  • 自定义带输入框类型AlertView

    由于系统自带AlertView带输入框样式不太美观,决定自定义类似系统带有输入框样式AlertView,可根据需求...

  • 自定义AlertView

    CustomAlertView 一个自定义的AlertView,用户可以根据自己的需求来设置。 使用方法 类似于系...

  • 自定义alertview

    .h .m

  • 自定义AlertView

    公司有很多的地方需要用到弹出的提示界面,基本的效果是一样的,但是内容又有很大的不同,每个界面都要重写,工作起来很费...

  • 自定义alertview

    // // TestAlertView.m // YanKa // // Created by Lei on 20...

  • AlertController按钮颜色修改

    通过KVC修改alertView按钮的颜色 如果想要更自由的修改alertController的样式可以采用自定义...

  • 链式alertViewController

    快速创建alertview 1. 自定义创建一个弹框需要自己配置 AlertAction 2.快速创建alert ...

网友评论

本文标题:iOS-Swift-自定义AlertView

本文链接:https://www.haomeiwen.com/subject/ajrehttx.html