替换UIAlertView、UIAlertController
来源:互联网 发布:ps4 刀剑神域 知乎 编辑:程序博客网 时间:2024/05/21 19:33
UIAlertView是iOS9开始不建议使用了,UIAlertController是iOS8提倡使用,但是这两个风格可能都不是自己想要的,那么下面介绍如何自定义一款
//// TGActionSheet.swift// TGPhotoPicker//// Created by targetcloud on 2017/8/13.// Copyright © 2017年 targetcloud. All rights reserved.//import UIKitfileprivate let TGActionSheetCancelTag = 1999fileprivate let TGActionSheetBaseTag = 1000fileprivate let TGActionSheetAnimationDuration: TimeInterval = 0.25protocol TGActionSheetDelegate: NSObjectProtocol { func actionSheet(actionSheet: TGActionSheet?, didClickedAt index: Int)}class TGActionSheet: UIView { weak var delegate: TGActionSheetDelegate? fileprivate lazy var btnArr: [UIButton] = [UIButton]() fileprivate lazy var dividerArr: [UIView] = [UIView]() fileprivate lazy var coverView: UIView = { [unowned self] in let coverView = UIView() coverView.backgroundColor = UIColor(white: 0, alpha: 0.3) coverView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(coverViewDidClick))) return coverView }() fileprivate lazy var actionSheet: UIView = { let actionSheet = UIView() actionSheet.backgroundColor = .clear return actionSheet }() fileprivate lazy var cancelBtn: UIButton = { let cancelBtn = UIButton(type: .custom) cancelBtn.tag = TGActionSheetCancelTag cancelBtn.backgroundColor = UIColor(white: 1, alpha: 1) cancelBtn.titleLabel?.textAlignment = .center cancelBtn.titleLabel?.font = UIFont.systemFont(ofSize: TGPhotoPickerConfig.shared.fontSize) cancelBtn.setTitleColor(.darkGray, for: .normal) cancelBtn.addTarget(self, action: #selector(actionSheetClicked(_:)), for: .touchUpInside) return cancelBtn }() class func showActionSheet(with delegate: TGActionSheetDelegate?, cancelTitle: String, otherTitles: [String]) -> TGActionSheet { return TGActionSheet(delegate: delegate, cancelTitle: cancelTitle, otherTitles: otherTitles) } override init(frame: CGRect) { super.init(frame: frame) } init(delegate: TGActionSheetDelegate?, cancelTitle: String, otherTitles: [String]) { super.init(frame: CGRect.zero) btnArr.removeAll() dividerArr.removeAll() self.backgroundColor = .clear self.delegate = delegate self.addSubview(coverView) self.coverView.addSubview(actionSheet) for i in 0..<otherTitles.count { self.createBtn(with: otherTitles[i], bgColor: UIColor(white: 1, alpha: 1), titleColor: .darkGray, tagIndex: i + TGActionSheetBaseTag) } cancelBtn.setTitle(cancelTitle, for: .normal) self.actionSheet.addSubview(cancelBtn) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } fileprivate func createBtn(with title: String?, bgColor: UIColor?, titleColor: UIColor?, tagIndex: Int) { let actionBtn = UIButton(type: .custom) actionBtn.tag = tagIndex actionBtn.titleLabel?.font = UIFont.systemFont(ofSize: TGPhotoPickerConfig.shared.fontSize) actionBtn.backgroundColor = bgColor actionBtn.titleLabel?.textAlignment = .center actionBtn.setTitle(title, for: .normal) actionBtn.setTitleColor(titleColor, for: .normal) actionBtn.addTarget(self, action: #selector(actionSheetClicked(_:)), for: .touchUpInside) self.actionSheet.addSubview(actionBtn) self.btnArr.append(actionBtn) let divider = UIView() divider.backgroundColor = UIColor.hexInt(0xebebeb) actionBtn.addSubview(divider) dividerArr.append(divider) } @objc fileprivate func coverViewDidClick() { self.dismiss() } @objc fileprivate func actionSheetClicked(_ btn: UIButton) { if btn.tag != TGActionSheetCancelTag { self.delegate?.actionSheet(actionSheet: self, didClickedAt: btn.tag - TGActionSheetBaseTag) self.dismiss() } else { self.dismiss() } } func show() { if self.superview != nil { return } let keyWindow = UIApplication.shared.keyWindow self.frame = (keyWindow?.bounds)! keyWindow?.addSubview(self) coverView.frame = CGRect(x: 0, y: 0, width: TGPhotoPickerConfig.ScreenW, height: TGPhotoPickerConfig.ScreenH) let actionH = CGFloat(self.btnArr.count + 1) * TGPhotoPickerConfig.shared.toolBarH + 5.0 actionSheet.frame = CGRect(x: 0, y: self.frame.height, width: TGPhotoPickerConfig.ScreenW, height: actionH) cancelBtn.frame = CGRect(x: 0, y: actionH - TGPhotoPickerConfig.shared.toolBarH, width: self.frame.width, height: TGPhotoPickerConfig.shared.toolBarH) let btnW: CGFloat = self.frame.width let btnH: CGFloat = TGPhotoPickerConfig.shared.toolBarH let btnX: CGFloat = 0 var btnY: CGFloat = 0 for i in 0..<btnArr.count { let btn = btnArr[i] let divider = dividerArr[i] btnY = TGPhotoPickerConfig.shared.toolBarH * CGFloat(i) btn.frame = CGRect(x: btnX, y: btnY, width: btnW, height: btnH) divider.frame = CGRect(x: btnX, y: btnH - 1, width: btnW, height: 1) } UIView.animate(withDuration: TGActionSheetAnimationDuration) { self.actionSheet.frame.origin.y = self.frame.height - self.actionSheet.frame.height } } fileprivate func dismiss() { UIView.animate(withDuration: TGActionSheetAnimationDuration, animations: { self.actionSheet.frame.origin.y = self.frame.height }) { (_) in if self.superview != nil { self.removeFromSuperview() } } }}
使用时实现其代理方法即可
extension TGPhotoPicker: TGActionSheetDelegate { func actionSheet(actionSheet: TGActionSheet?, didClickedAt index: Int) { switch index { case 0: //your code case 1://your codedefault: break } }}效果如下
完整应用DEMO见https://github.com/targetcloud/TGPhotoPicker
阅读全文
0 0
- 替换UIAlertView、UIAlertController
- UIAlertView 和UIAlertController二合一
- UIAlertView以及UIAlertController
- UIAlertView变成了UIAlertController
- UIAlertView 和 UIAlertController
- UIAlertController与 UIAlertView
- UIAlertController(UIActionSheet、UIAlertView)
- 浅谈UIAlertView与UIAlertController
- UIAlertController & UIAlertView 对话框
- UIAlertController UIAlertAction (UIAlertView, UIActionSheet)
- UIAlertController和UIAlertView
- UIAlertController代替UIAlertView
- UIAlertController(UIAlertView && UIActionSheet)
- 浅谈UIAlertView与UIAlertController
- UIAlertView与UIAlertController
- UIAlertController 和 UIAlertView的使用
- UIAlertView和UIAlertController的使用
- UIAlertView和UIAlertController的封装
- 新安装的oracle数据库 创建用户 表空间 还原数据库信息
- HDU-3966-树链剖分
- nginx下ftp服务搭建
- shepp-logan模型笛卡尔坐标变换到极坐标,环状伪影仿真
- linux进程间通信消息队列
- 替换UIAlertView、UIAlertController
- BZOJ 1609:[Usaco2008 Feb]Eating Together麻烦的聚餐
- 我所看到程序员的几种病症
- 选择排序算法(十)
- Java定时任务之Timer(慕课网笔记)
- 【JavaScript】isNaN()函数
- 搭建 react+webpack开发环境
- android fragment 菜单项/工具栏不显示
- 最大子数组问题