弹框(在这里使用了 Spring动画框架)
Spring动画可以参考我的这篇文章
https://blog.csdn.net/wa172126691/article/details/80335130
界面中间的弹框
//
// SmileAlert.swift
// H56580E2E
//
//
import UIKit
import Spring
class SmileAlert: UIView {
/// 根据RGBA生成颜色(格式为:22,22,22,0.5)
var SM_RGBAColor: (CGFloat, CGFloat, CGFloat, CGFloat) -> UIColor = {red, green, blue, alpha in
return UIColor(red: red / 255, green: green / 255, blue: blue / 255, alpha: alpha);
}
typealias clickAlertClosure = (_ index: Int) -> Void //声明闭包,点击按钮传值
//把申明的闭包设置成属性
var clickClosure: clickAlertClosure?
//为闭包设置调用函数
func clickIndexClosure(_ closure:clickAlertClosure?){
//将函数指针赋值给myClosure闭包
clickClosure = closure
}
let Screen_width = UIScreen.main.bounds.size.width
let Screen_height = UIScreen.main.bounds.size.height
let bgView = SpringView() //白色框动画控件
let titleLabel = UILabel() //标题按钮
let contentLabel = UILabel() //显示内容
var title = "" //标题
var content = "" //内容
let cancelBtn = UIButton() //取消按钮
let sureBtn = UIButton() //确定按钮
let Bgtap = UITapGestureRecognizer() //点击手势
let scrollview = UIScrollView() //滚动视图层
init(title: String?, message: String?, cancelButtonTitle: String?, sureButtonTitle: String?) {
super.init(frame: CGRect(x: 0, y: 0, width: Screen_width, height: Screen_height))
createAlertView()
self.titleLabel.text = title
/**设置 UILable的行间距 **/
//通过富文本来设置行间距
let paraph = NSMutableParagraphStyle()
//将行间距设置为28
paraph.lineSpacing = 10
//样式属性集合
let attributes = [kCTFontAttributeName:UIFont.systemFont(ofSize: 15),
kCTParagraphStyleAttributeName: paraph]
self.contentLabel.attributedText = NSAttributedString(string: message!, attributes: attributes as [NSAttributedStringKey : Any])
//self.contentLabel.text = message
self.cancelBtn.setTitle(cancelButtonTitle, for: UIControlState())
self.sureBtn.setTitle(sureButtonTitle, for: UIControlState())
}
//MARK:创建
func createAlertView() {
//布局
self.frame = CGRect(x: 0, y: 0, width: Screen_width, height: Screen_height)
self.backgroundColor = UIColor.darkGray.withAlphaComponent(0.5)
Bgtap.addTarget(self, action: #selector(SmileAlert.dismiss))
self.addGestureRecognizer(Bgtap)
//spring动画白底(弹出主体)
bgView.frame = CGRect(x: 30, y: Screen_height/2 - 250, width: Screen_width - 60, height: 420)//设置大小及其位置
bgView.backgroundColor = UIColor.white//背景色
bgView.layer.cornerRadius = 9
bgView.clipsToBounds = true
bgView.animation = "pop"
bgView.curve = "Spring"
bgView.duration = 1.0
bgView.damping = 0.7
bgView.velocity = 0.7
bgView.force = 0.3
bgView.animate()
self.addSubview(bgView)
let width = bgView.frame.size.width
let height = bgView.frame.size.height
//标题
titleLabel.frame = CGRect(x: 0, y: 15, width: width, height: 25)
titleLabel.textColor = UIColor.black
titleLabel.font = UIFont.systemFont