MyAlertView.swift

// Pop Up Styles

enum MyAlertViewStyle: Int {

case success

case error

case notice

case warning

case info

}

// Allow alerts to be closed/renamed in a chainable manner

// Example: MyAlertView().showSuccess(self, title: "Test", subTitle: "Value").Close()

//class MyAlertViewClose {

//    let alertview: MyAlertView

//

//    // Initialisation and Title/Subtitle/Close functions

//    init(alertview: MyAlertView) { self.alertview = alertview }

//    func setTitle(_ title: String) { self.alertview.labelView.text = title; }

//    func setSubTitle(_ subTitle: String) { self.alertview.labelViewDescription.text = subTitle; }

//    func Close() { self.alertview.doneButtonAction() }

//}

class MyAlertView: UIView {

let kDefaultShadowOpacity: CGFloat = 0.7;

let kCircleHeight: CGFloat = 56.0;

let kCircleTopPosition: CGFloat = -12; // Should not be defined here. Make it dynamic

let kCircleBackgroundTopPosition: CGFloat = -15; // Should not be defined here. Make it dynamic

let kCircleHeightBackground: CGFloat = 62.0;

let kCircleIconHeight: CGFloat = 20.0;

let kWindowWidth: CGFloat = 240.0;

let kWindowHeight: CGFloat = 228.0;

// Font

let kDefaultFont: NSString = "HelveticaNeue"

// Members declaration

var labelView: UILabel

var labelViewDescription: UILabel

var shadowView: UIView

var contentView: UIView

var circleView: UIView

var circleViewBackground: UIView

var circleIconImageView: UIImageView

var doneButton: UIButton

var rootViewController: UIViewController

var durationTimer: Timer!

init () {

// Content View

self.contentView = UIView(frame: CGRect(x: 0, y: kCircleHeight / 4, width: kWindowWidth, height: kWindowHeight))

self.contentView.backgroundColor = UIColor(white: 1, alpha: 1);

self.contentView.layer.cornerRadius = 5;

self.contentView.layer.masksToBounds = true;

self.contentView.layer.borderWidth = 0.5;

// Circle View

self.circleView = UIView(frame: CGRect(x: kWindowWidth / 2 - kCircleHeight / 2, y: kCircleTopPosition, width: kCircleHeight, height: kCircleHeight))

self.circleView.layer.cornerRadius =  self.circleView.frame.size.height / 2;

// Circle View Background

self.circleViewBackground = UIView(frame: CGRect(x: kWindowWidth / 2 - kCircleHeightBackground / 2, y: kCircleBackgroundTopPosition, width: kCircleHeightBackground, height: kCircleHeightBackground))

self.circleViewBackground.layer.cornerRadius =  self.circleViewBackground.frame.size.height / 2;

self.circleViewBackground.backgroundColor = UIColor.white

// Circle View Image

self.circleIconImageView = UIImageView(frame: CGRect(x: kCircleHeight / 2 - kCircleIconHeight / 2, y: kCircleHeight / 2 - kCircleIconHeight / 2, width: kCircleIconHeight, height: kCircleIconHeight))

self.circleView.addSubview(self.circleIconImageView)

// Title

self.labelView = UILabel(frame: CGRect(x: 12, y: kCircleHeight / 2 + 22, width: kWindowWidth - 24, height: 40))

self.labelView.numberOfLines = 1

self.labelView.textAlignment = NSTextAlignment.center

self.labelView.font = UIFont(name: kDefaultFont as String, size: 20)

self.contentView.addSubview(self.labelView)

// Subtitle

self.labelViewDescription = UILabel(frame: CGRect(x: 12, y: 84, width: kWindowWidth - 24, height: 80))

self.labelViewDescription.numberOfLines = 3

self.labelViewDescription.textAlignment = NSTextAlignment.center

self.labelViewDescription.font = UIFont(name: kDefaultFont as String, size: 14)

self.contentView.addSubview(self.labelViewDescription)

// Shadow View

self.shadowView = UIView(frame: UIScreen.main.bounds)

self.shadowView.backgroundColor = UIColor.black

// Done Button

self.doneButton = UIButton(frame: CGRect(x: 12, y: kWindowHeight - 52, width: kWindowWidth - 24, height: 40))

self.doneButton.layer.cornerRadius = 3

self.doneButton.layer.masksToBounds = true

self.doneButton.setTitle("Done", for: UIControlState())

self.doneButton.titleLabel?.font = UIFont(name: kDefaultFont as String, size: 14)

self.contentView.addSubview(self.doneButton)

// Root view controller

self.rootViewController = UIViewController()

// Superclass initiation

super.init(frame: CGRect(x: ((320 - kWindowWidth) / 2), y: 0 - kWindowHeight, width: kWindowWidth, height: kWindowHeight))

// Show notice on screen

self.addSubview(self.contentView)

self.addSubview(self.circleViewBackground)

self.addSubview(self.circleView)

// Colours

self.contentView.backgroundColor = UIColorFromRGB(0xFFFFFF)

self.labelView.textColor = UIColorFromRGB(0x4D4D4D)

self.labelViewDescription.textColor = UIColorFromRGB(0x4D4D4D)

self.contentView.layer.borderColor = UIColorFromRGB(0xCCCCCC).cgColor

// On complete.

self.doneButton.addTarget(self, action: #selector(MyAlertView.doneButtonAction), for: UIControlEvents.touchUpInside)

}

required init?(coder aDecoder: NSCoder) {

fatalError("init(coder:) has not been implemented")

}

// showTitle(view, title, subTitle, style)

func showTitle(_ view: UIViewController, title: String, subTitle: String, style: MyAlertViewStyle)  {

return showTitle(view, title: title, subTitle: subTitle, completeText: nil, style: style)

}

// showSuccess(view, title, subTitle)

func showSuccess(_ view: UIViewController, title: String, subTitle: String)  {

return showTitle(view, title: title, subTitle: subTitle, completeText: nil, style: MyAlertViewStyle.success);

}

// showError(view, title, subTitle)

func showError(_ view: UIViewController, title: String, subTitle: String)  {

return showTitle(view, title: title, subTitle: subTitle, completeText: nil, style: MyAlertViewStyle.error);

}

// showNotice(view, title, subTitle)

func showNotice(_ view: UIViewController, title: String, subTitle: String)  {

return showTitle(view, title: title, subTitle: subTitle,  completeText: nil, style: MyAlertViewStyle.notice);

}

// showWarning(view, title, subTitle)

func showWarning(_ view: UIViewController, title: String, subTitle: String)  {

return showTitle(view, title: title, subTitle: subTitle, completeText: nil, style: MyAlertViewStyle.warning);

}

// showInfo(view, title, subTitle)

func showInfo(_ view: UIViewController, title: String, subTitle: String)  {

return showTitle(view, title: title, subTitle: subTitle, completeText: nil, style: MyAlertViewStyle.info);

}

// showTitle(view, title, subTitle, duration, style)

func showTitle(_ view:UIViewController, title: String, subTitle: String, completeText: String?, style: MyAlertViewStyle)  {

self.alpha = 0;

self.rootViewController = view

self.rootViewController.view.addSubview(self.shadowView)

self.rootViewController.view.addSubview(self)

// Complete text

if(completeText != nil) {

self.doneButton.setTitle(completeText, for: UIControlState())

}

// Alert colour/icon

var viewColor: UIColor = UIColor()

var iconImageName: NSString = ""

// Icon style

switch(style) {

case MyAlertViewStyle.success:

viewColor = UIColorFromRGB(0x22B573);

iconImageName = "notification-success"

case MyAlertViewStyle.error:

viewColor = UIColorFromRGB(0xC1272D);

iconImageName = "notification-error"

case MyAlertViewStyle.notice:

viewColor = UIColorFromRGB(0x727375)

iconImageName = "notification-notice"

case MyAlertViewStyle.warning:

viewColor = UIColorFromRGB(0xFFD110);

iconImageName = "notification-warning"

case MyAlertViewStyle.info:

viewColor = UIColorFromRGB(0x2866BF);

iconImageName = "notification-info"

}

// Title

if ((title as NSString).length > 0 ) {

self.labelView.text = title;

}

// Subtitle

if ((subTitle as NSString).length > 0) {

self.labelViewDescription.text = subTitle;

}

// Alert view colour and images

self.doneButton.backgroundColor = viewColor;

self.circleView.backgroundColor = viewColor;

self.circleIconImageView.image = UIImage(named: iconImageName as String)

// Animate in the alert view

UIView.animate(withDuration: 0.2, animations: {

self.shadowView.alpha = self.kDefaultShadowOpacity

var frame:CGRect = self.frame;

frame.origin.y = self.rootViewController.view.center.y - 100;

self.frame = frame;

self.alpha = 1;

}, completion: { finished in

UIView.animate(withDuration: 0.2, animations: {

self.center = self.rootViewController.view.center;

}, completion: { finished in })

})

}

// When click 'Done' button, hide view.

func doneButtonAction() { hideView(); }

//Close MyAlertView By duration

func hideMyAlertView(duration: TimeInterval?)  {

// Adding duration

if (duration != nil) {

durationTimer?.invalidate()

durationTimer = Timer.scheduledTimer(timeInterval: duration!, target: self, selector: #selector(MyAlertView.hideView), userInfo: nil, repeats: false)

}

}

// Close MyAlertView

func hideView() {

UIView.animate(withDuration: 0.2, animations: {

self.shadowView.alpha = 0;

self.alpha = 0;

}, completion: { finished in

self.shadowView.removeFromSuperview()

self.removeFromSuperview()

})

}

// Helper function to convert from RGB to UIColor

func UIColorFromRGB(_ rgbValue: UInt) -> UIColor {

return UIColor(

red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,

green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,

blue: CGFloat(rgbValue & 0x0000FF) / 255.0,

alpha: CGFloat(1.0)

)

}

}

ViewController.swift

import UIKit

let kSuccessTitle: NSString = "Congratulations"

let kErrorTitle: NSString = "Connection error"

let kNoticeTitle: NSString = "Notice"

let kWarningTitle: NSString = "Warning"

let kInfoTitle: NSString = "Info"

let kSubtitle: NSString = "You've just displayed this awesome Pop Up View"

let kDefaultAnimationDuration = 2.0

class ViewController: UIViewController {

let alertView = MyAlertView()

override func viewDidLoad() {

super.viewDidLoad()

}

@IBAction func success(_ sender: Any) {

alertView.showSuccess(self, title: kSuccessTitle as String, subTitle: kSubtitle as String)

alertView.hideMyAlertView(duration: 1.0)

}

@IBAction func error(_ sender: Any) {

alertView.showError(self, title: kErrorTitle as String, subTitle: kSubtitle as String)

alertView.hideMyAlertView(duration: 2.0)

}

@IBAction func notice(_ sender: Any) {

alertView.showNotice(self, title: kNoticeTitle as String, subTitle: kSubtitle as String)

alertView.hideMyAlertView(duration: 3.0)

}

@IBAction func warning(_ sender: Any) {

alertView.showWarning(self, title: kWarningTitle as String, subTitle: kSubtitle as String)

alertView.hideMyAlertView(duration: 4.0)

}

@IBAction func info(_ sender: Any) {

alertView.showInfo(self, title: kInfoTitle as String, subTitle: kSubtitle as String)

alertView.hideMyAlertView(duration: 5.0)

}

}

效果图如下:

自定义AlertView(Swift)的更多相关文章

  1. Swift自定义AlertView

    今天项目加新需求,添加积分过期提醒功能: 第一反应就用系统的UIAlertViewController,但是message中积分是需要红色显示. // let str = "尊敬的顾客,您有 ...

  2. iOS 第三方自定义Alertview项目MBProcessHud中的重要代码分析

    做ios,弹出一个自定义的alertview挺常见的.ios7以前,我们可以对系统的UIAlertView进行一点操作,实现一点简单的定制,但是ios7不再允许我们这样做了.因此,我们需要自己创建一个 ...

  3. 兼容iOs7的自定义alertView

    转载请注明出处. 升级到ios7后,旧项目中使用的继承UIAlertView的自定义alertview无法正常显示了,无奈只好换思路去实现,改成从当前keywindow下创建要显示的alertview ...

  4. 自定义AlertView的方法和改变Alert的弹出位置以及其宽度

    此方法在IOS7中不适合 一.自定义AlertView 1.首先新建一个OC类继承与AlertView. 2.然后再.m中添加方法 - (void)layoutSubviews 可以再这个方法里边改变 ...

  5. iOS开发——自定义AlertView

    自定义的AlertView,可以选择出现的动画方式,正文信息高度自动变化,特意做了几个可以对比.没啥难点,直接上代码,一看就懂. 1.在YYTAlertView.h文件中 // //  YYTAler ...

  6. 自定义AlertView实现模态对话框

    在Windows应用程序中,经常使用模态(Model)对话框来和用户进行简单的交互,比如登录框.在IOS应用程序中,有时我们也希望做同样的事情.但IOS的UI库中,没有模态对话框,最接近那个样子的应该 ...

  7. IOS自定义alertview

    在家闲来无事,于是就看起来ios绘图的那块,写点什么好呢? 鼓捣了一会,总算写出了一个小东西 这个是写完以后的效果 这里我实现了三种款式的alertview 分别是成功,错误和警告,剩下的呢有空继续添 ...

  8. iOS自定义AlertView 与 ActionSheet 遮罩提示+弹出动画

    产品大人总是能够想到很多让人欣慰的点子,基本所有能提示的地方都要很多文案啊图片之类 由此封装了一个半透明黑色遮罩的alert类(假装有图.jpg) 代码略糙,just share (逃 下载链接

  9. Swift之高德地图自定义标注弹出气泡样式

    在用到地图类的sdk,sdk自带的样式必定不能满足开发者的需求,于是开发者不得不进行自定义,由于官方同样是object-c 版语法,不得不将其转为swift语法,以满足项目需求. 新建两个类 Cust ...

随机推荐

  1. 101个MySQL的调节和优化技巧

    MySQL是一个功能强大的开源数据库.随着越来越多的数据库驱动的应用程序,人们一直在推动MySQL发展到它的极限.这里是101条调节和优化MySQL安装的技巧.一些技巧是针对特定的安装环境的,但这些思 ...

  2. [学习总结] python语言学习总结 (二)

    1.python中的拆包 之前就只写了*可以是未知数量的参数,**可以传入未知数量命名参数.这次详细记下拆包. def f1(a, *l): print(a) # 不拆包 print(l) # 拆包 ...

  3. 二、antd pro 删除eslint检测

    删除package.json 里 " pre-commit": "npm run lint-staged" 这个对象就可以.

  4. RSA AES 前端JS与后台JAVA的加密解密的是实现

    AES CryptoJS 前提是编码方式,key,vi中设置一样,就可以进行跨语言加密解密 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ...

  5. spring5之SAXParseException:cvc-elt.1: 找不到元素 “beans” 的声明

    之前SSM项目一直报错,就是找不到错误  气啊 后来在网上找到了答案:燕来spring5之后就不再需要写版本号了

  6. 源自http://www.cnblogs.com/sciencefans/p/4394861.html

    人脸识别的四大块:Face detection, alignment, verification and identification(recognization),本别代表从一张图中识别出人脸位置, ...

  7. Java面向对象基础 ——面试题

    1面向对象基础 JAVA基础语法自行掌握. 三大特性: 一 封装:★★★★★ 概念:是指隐藏对象的属性和实现细节,仅对外提供公共访问方式. 好处:将变化隔离:便于使用:提高重用性:安全性. 封装原则: ...

  8. cocos2dx 加密spine文件遇到的问题(暂时没有解决方法)

    今天我研究了一下加密spine动画的加密的方法,图片肯定要加密的,所以我只选择加密图片,另外的一个altas文件和json文件就不做加密打算. 我的思路是通过TexturePacker打包成加密的文件 ...

  9. VueJS坎坷之路222--vue cli 3.0引入静态文件

    前两天准备搭建一个vue小项目,当引入jquery脚本的时候一直找不到引入的文件: 在网上搜了好多vue添加静态文件的方法,发现大多数方法都是创建一个与文件夹src同等级的文件夹static存放引入的 ...

  10. 洛谷 4219/BZOJ 4530 大融合

    4530: [Bjoi2014]大融合 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 990  Solved: 604[Submit][Status] ...