替换UIAlertView、UIAlertController

UIAlertView是iOS9开始不建议使用了,UIAlertController是iOS8提倡使用,但是这两个风格可能都不是自己想要的,那么下面介绍如何自定义一款

//
//  TGActionSheet.swift
//  TGPhotoPicker
//
//  Created by targetcloud on 2017/8/13.
//  Copyright © 2017年 targetcloud. All rights reserved.
//

import UIKit

fileprivate let TGActionSheetCancelTag = 1999
fileprivate let TGActionSheetBaseTag = 1000
fileprivate let TGActionSheetAnimationDuration: TimeInterval = 0.25

protocol 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 code
default: break } }}


效果如下






完整应用DEMO见https://github.com/targetcloud/TGPhotoPicker


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值