登录 立即注册
金钱:

Code4App-iOS开发-iOS 开源代码库-iOS代码实例搜索-iOS特效示例-iOS代码例子下载-Code4App.com

Swift 类似微信的自定义底部弹窗

[复制链接]
来自: dr19901106 分类: iOS精品源码 上传时间: 2016-6-12 12:59:19

项目介绍:

Swift 实现了类似微信的自定义底部弹窗 ActionSheet
实现代码示例:
[Objective-C] 查看源文件 复制代码
import UIKit

/// 屏幕适配
let kScreenWidth = UIScreen.mainScreen().bounds.width
let kScreenHeight = UIScreen.mainScreen().bounds.height
let kSCREEN_WIDTH_RATIO:CGFloat = kScreenWidth < 375 ? 1 : kScreenWidth/375.0
let kSCREEN_HEIGHT_RATIO:CGFloat = kScreenHeight < 667 ? 1 : kScreenHeight/667.0
func kAutoWEX(w:CGFloat)->CGFloat {
    return w*kSCREEN_WIDTH_RATIO
}
func kAutoHEX(h:CGFloat)->CGFloat {
    return h*kSCREEN_HEIGHT_RATIO
}
/**
*  这里修改每一行的高度
*/
let kModelViewCellHeight = kAutoHEX(55)
/**
*  颜色
*/
let kNormalColor = UIColor(HEX: "#333333")
let kRedColor = UIColor(HEX: "#df473b")
let kWhiteColor = UIColor(HEX: "#ffffff")
let kLineColor = UIColor(HEX: "#dddddd")
let kGrayColor = UIColor(HEX: "#666666")
let kBackGroundColor = UIColor(HEX: "#f4f4f4")
let kLightGrayColor = UIColor(HEX: "#999999")
let kGreenColor = UIColor(HEX: "#81c16d")


protocol YRPCommonModalViewDelegate:NSObjectProtocol {
    func clickBtnWithIndexAndView(index:Int,arr:[String])
}

internal class YRPCommonModalView: UIView {
    
    weak var modalViewDelegate:YRPCommonModalViewDelegate?
    var arr:[String]?
    init(arr: [String]) {
        super.init(frame: CGRectZero)
        self.frame = CGRect(x: 0, y: kScreenHeight, width: kScreenWidth, height: CGFloat(arr.count + 1) * kModelViewCellHeight + 5)
        self.arr = arr
        setupUI(arr)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(arr: [String]) {
        backgroundColor = kWhiteColor
        
        for i in 0..<arr.count {
            let btn = UIButton(title: arr[i])
            btn.tag = i
            btn.frame = CGRect(x: 0, y: CGFloat(i) * kModelViewCellHeight, width: kScreenWidth, height: kModelViewCellHeight)
            btn.addTarget(self, action: "clickBtns:", forControlEvents: .TouchUpInside)
            let line = UIView(frame: CGRect(x: 0, y: CGFloat(i) * kModelViewCellHeight, width: kScreenWidth, height: 0.5))
            line.backgroundColor = kLineColor
            addSubview(btn)
            addSubview(line)
        }
        let btn = UIButton(title: "取消", titleColor: kRedColor)
        btn.frame = CGRect(x: 0, y: CGFloat(arr.count) * kModelViewCellHeight + 5, width: kScreenWidth, height: kModelViewCellHeight)
        btn.addTarget(self, action: "clickBtns:", forControlEvents: .TouchUpInside)
        btn.tag = arr.count
        addSubview(btn)
        
    }
    
    @objc
    private func clickBtns(sender:UIButton) {
        
        if let del = modalViewDelegate {
            del.clickBtnWithIndexAndView(sender.tag, arr:self.arr!)
        }
        
        dismiss()
    }
    
    @objc
    private func dismiss() {
        UIView.animateWithDuration(0.5, animations: { () -> Void in
            self.frame.origin.y = kScreenHeight
            }, completion: { (finish) -> Void in
                self.dismissBtn.hidden = true
                self.dismissBtn.removeFromSuperview()
                self.hidden = true
                self.removeFromSuperview()
        })
    }
    
    func show() {
        let window = UIApplication.sharedApplication().keyWindow
        dismissBtn.addTarget(self, action: "dismiss", forControlEvents: .TouchUpInside)
        window?.addSubview(self)
        window?.insertSubview(dismissBtn, belowSubview: self)
        hidden = false
        dismissBtn.hidden = true
        UIView.animateWithDuration(0.5) {
            () -> Void in
            self.dismissBtn.hidden = false
            self.frame.origin.y = kScreenHeight - (CGFloat(self.arr!.count + 1) * kModelViewCellHeight + 5)
        }
    }
    
    /// 遮罩
    private var dismissBtn: UIButton = {
        let btn = UIButton()
        btn.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight)
        btn.backgroundColor = UIColor(white: 0, alpha: 0.2)
        return btn
    }()
    
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    
}


DEMO下载:
DRAlertView.zip (32.12 KB, 下载次数: 573)
3126 0 0
联系我们
首页/微信公众账号投稿

帖子代码编辑/版权问题

QQ:435399051,742864542


代码贡献英雄榜
用户名 下载数
通过邮件订阅最新 Code4App 信息
上一条 /4 下一条
联系我们
关闭
合作电话:
13802416937
Email:
435399051@qq.com
商务市场合作/投稿
问题反馈及帮助
联系我们

广告投放| 广东互联网违法和不良信息举报中心|中国互联网举报中心|Github|申请友链|手机版|Code4App ( 粤ICP备15117877号-1 )

快速回复 返回顶部 返回列表