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)

}

}

效果图如下:

最新文章

  1. PAT mooc DataStructure 4-2 SetCollection
  2. ios-遍历和排序
  3. 20145207《Java程序设计》第6周学习总结
  4. C#句柄使用
  5. NSData 数据转换
  6. win7开启telnet客户端
  7. iOS企业开发者账号实现内部分发
  8. 079、监控利器 sysdig (2019-04-26 周五)
  9. [Swift]LeetCode214. 最短回文串 | Shortest Palindrome
  10. [Swift]LeetCode1025. 除数博弈 | Divisor Game
  11. 第5章 Hyperledger Fabric功能
  12. java.lang.LinkageError: JAXB 2.0 API is being loaded from the bootstrap classloader
  13. 恒生UFX交易接口基本介绍说明
  14. redis、memcache、mongoDB 对比
  15. STL基础--仿函数(函数对象)
  16. Bzoj3261/洛谷P4735 最大异或和(可持久化Trie)
  17. TEA加密算法java版
  18. 每天一个linux命令(权限):【转载】 /etc/group文件详解
  19. Jupyter Notebook远程服务器配置[转]
  20. Multi-thread & Multi-process

热门文章

  1. UVA 1599, POJ 3092 Ideal Path 理想路径 (逆向BFS跑层次图)
  2. UVA 1616 Caravan Robbers 商队抢劫者(二分)
  3. thisnkphp添加二维码
  4. SpringMVC-常用的注解
  5. LibreOffice Writer Comment
  6. JavaScript -- 条件语句和循环语句
  7. iOS应用架构谈part4-本地持久化方案及动态部署
  8. React动态import()
  9. OpenCV3.42+VS2017配置+模块计算机类型“X86”与目标计算机类型“x64”冲突”的问题解决
  10. STL之stack操作