iOS-swift-自定义弹框

本文介绍了如何在iOS应用中使用Swift自定义弹框,并结合Spring动画框架为弹框添加生动的动画效果。内容包括界面中间的弹框、底部弹框以及顶部通知层的实现和调用方法。
摘要由CSDN通过智能技术生成

弹框(在这里使用了 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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值