教你如何自定義AlertView

圖片來源于網(wǎng)絡
前言:

系統(tǒng)自帶的alertView界面有點呆板,動畫有點單一,總之隨著業(yè)務的發(fā)展,系統(tǒng)自帶的alertView已經(jīng)很難滿足我們的需求,那自定義的就很有必要。本文就介紹如何自定義alertView,看完你就懂得制作屬于自己的alertView了

一、創(chuàng)建DWAlert.swift

創(chuàng)建一個類名為在DWAlert.swift,在class DWAlert: UIView里面添加一些常量和屬性

//const 常量
let kAlertWidth = 245.0
let kAlertHeight = 160.0
let kTitleYOffset = 15.0
let kTitleHeight = 25.0

let kContentOffset = 30.0
let kBetweenLabelOffset = 20.0
let kSingleButtonWidth = 160.0
let kCoupleButtonWidth = 107.0
let kButtonHeight = 40.0
let kButtonBottomOffset = 10.0

//property 屬性
var alertTitleLabel: UILabel!
var alertContentLabel: UILabel!
var button: UIButton!
var backImageView: UIView!

上面代碼const是為了定義彈出框需要的坐標和長寬,由于是不變,所有l(wèi)et修飾,與OC中的常量類似

二、繪制alertView

寫一個繼承init的方法,把title(alert標題),content(alert內容),Title(按鈕標題),作為參數(shù)

convenience init(alertTitle title: String, alertContent content: String, title Title: String) {

    
    self.init()
    self.layer.cornerRadius = 5.0
    self.backgroundColor = UIColor.white
    self.alertTitleLabel = UILabel.init(frame: CGRect(x: 0, y: kTitleYOffset, width: kAlertWidth, height: kTitleHeight))
    self.alertTitleLabel.font = UIFont.boldSystemFont(ofSize: 20.0)
    self.alertTitleLabel.textColor = UIColor.init(red: 56.0/255.0, green: 64.0/255.0, blue: 71.0/255.0, alpha: 1)
    self.addSubview(self.alertTitleLabel)
    
    let contentLabelWidth = kAlertWidth - 16
    let alertContentMaxY: Double = Double(self.alertTitleLabel.frame.maxY)
    self.alertContentLabel = UILabel.init(frame: CGRect(x: (kAlertWidth - contentLabelWidth) * 0.5, y: alertContentMaxY, width: contentLabelWidth, height: 60))
    self.alertContentLabel.numberOfLines = 0
    
    self.alertTitleLabel.textAlignment = .center
    self.alertContentLabel.textAlignment = self.alertTitleLabel.textAlignment
    self.alertContentLabel.textColor = UIColor.init(red: 127/255.0, green: 127/255.0, blue: 127/255.0, alpha: 1)
    self.alertContentLabel.font = UIFont.systemFont(ofSize: 15.0)
    self.addSubview(self.alertContentLabel)
    
    // about button
    let btnFrame = CGRect(x: (kAlertWidth - kSingleButtonWidth) * 0.5, y: kAlertHeight - kButtonBottomOffset - kButtonHeight, width: kSingleButtonWidth, height: kButtonHeight)
    self.button = UIButton.init(type: UIButtonType.custom)
    self.button.frame = btnFrame
    
    self.button.backgroundColor = UIColor.init(red: 245/255.0, green: 24/255.0, blue: 42/255.0, alpha: 1)
    self.button.setTitle(Title, for: .normal)
    self.button.titleLabel?.font = UIFont.systemFont(ofSize: 14.0)
    self.button.setTitleColor(UIColor.white, for: .normal)
    self.button.addTarget(self, action: #selector(BtnClick), for: .touchUpInside)
    self.button.layer.cornerRadius = 3.0
    self.addSubview(self.button)
    
    self.alertTitleLabel.text = title
    self.alertContentLabel.text = content
    
    //cancle button
    let cancleBtn = UIButton.init(type: .custom)
    cancleBtn.setImage(UIImage.init(named: "1.png"), for: .normal)
    cancleBtn.setImage(UIImage.init(named: "2.png"), for: .highlighted)
    cancleBtn.frame = CGRect(x: kAlertWidth - 32, y: 0, width: 32, height: 32)
    self.addSubview(cancleBtn)
    cancleBtn.addTarget(self, action: #selector(dismissAlert), for: .touchUpInside)

}

因為調用self.init(),所以得使用關鍵字convenence,使上述函數(shù)變成便利構造函數(shù),具體看convenence介紹

三、alertView的顯示與隱藏

1、show實現(xiàn)alertView顯示

func show() {
//1
    let shareWindow = UIApplication.shared.keyWindow
//2
    self.frame = CGRect(x: (Double((shareWindow?.bounds.width)!) - kAlertWidth)*0.5, y: -kAlertHeight - 30, width: kAlertWidth, height: kAlertHeight)
//3
    shareWindow?.addSubview(self)
}

上面代碼介紹:
1、拿到當前顯示的主窗口。 注意:主窗口一定得有,否則會崩。
2、設置alertView的frame
3、把alertView添加到主窗口

2、removeFromSuperview實現(xiàn)AlertView隱藏

override func removeFromSuperview() {
    //1
    self.backImageView.removeFromSuperview()
    self.backImageView = nil
    
    //2
    let shareWindow = UIApplication.shared.keyWindow
    let afterFrame = CGRect(x: (Double((shareWindow?.bounds.width)!) - kAlertWidth)*0.5, y: Double((shareWindow?.bounds.height)!), width: kAlertWidth, height: kAlertHeight)
    
    //3
    UIView.animate(withDuration: 0.35, delay: 0.0, options: .curveEaseOut, animations: { 
        self.frame = afterFrame
        let angle = M_1_PI / 1.5
        self.transform = CGAffineTransform.init(rotationAngle: CGFloat(angle))
    }) { (finished) in
        //4
        super.removeFromSuperview()
    }
}

上面代碼介紹:
1、移除掉待會講到的willMove(toSuperview newSuperview: UIView?)方法中添加的backImageView背景蒙版
2、獲取當前主窗口,并定義一個alertView的frame
3、利用UIView.animate對alertView進行動畫操作。 注意:angle值為M_1_PI / 1.5,只是個參考,您可以換其他的值,試試效果
4、完成動畫后,調用父類的removeFromSuperview移除alertView

四、實現(xiàn)alertView墜落效果

willMove(toSuperview newSuperview: UIView?)里面實現(xiàn),該方法會在當alertView即將加入主窗口時被系統(tǒng)自動調用,詳情請看UIView不可不知的秘密

override func willMove(toSuperview newSuperview: UIView?) {
    if newSuperview == nil {
        return
    }
    let shareWindow = UIApplication.shared.keyWindow
    
    if self.backImageView == nil {
        self.backImageView = UIView.init(frame: (shareWindow?.bounds)!)
    }
    
    self.backImageView.backgroundColor = UIColor.black
    self.backImageView.alpha = 0.6
    shareWindow?.addSubview(self.backImageView)
    let angle = M_1_PI / 2
    self.transform = CGAffineTransform.init(rotationAngle: CGFloat(angle))
    let afterFrame = CGRect(x: (Double((shareWindow?.bounds.width)!) - kAlertWidth)*0.5, y: (Double((shareWindow?.bounds.height)!) - kAlertHeight)*0.5, width: kAlertWidth, height: kAlertHeight)
    UIView.animate(withDuration: 0.35, delay: 0.0, options: .curveEaseIn, animations: {
        self.transform = CGAffineTransform.init(rotationAngle: 0)
        self.frame = afterFrame
    }) { (finished) in
    }
    super.willMove(toSuperview: newSuperview)
}

上面代碼重點是UIView.animate里的,實現(xiàn)了墜落動畫效果。注意:self.transform = CGAffineTransform.init(rotationAngle: 0)設置旋轉角度,再設置frame。

五、使用DWAlert

ViewController創(chuàng)建一個按鈕,并添加一個點擊事件ClickMe,在方法里面創(chuàng)建alertView

@IBAction func ClickMe(_ sender: Any) {
    let alert = DWAlert(alertTitle: "注意", alertContent: "流星墜落效果來了??", title: "確定")
    alert.show()
}

在此,該alertView已經(jīng)完成,效果如下:

代碼傳送門

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 發(fā)現(xiàn) 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,255評論 4 61
  • 在iOS中隨處都可以看到絢麗的動畫效果,實現(xiàn)這些動畫的過程并不復雜,今天將帶大家一窺iOS動畫全貌。在這里你可以看...
    F麥子閱讀 5,147評論 5 13
  • 在iOS中隨處都可以看到絢麗的動畫效果,實現(xiàn)這些動畫的過程并不復雜,今天將帶大家一窺ios動畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,573評論 6 30
  • *7月8日上午 N:Block :跟一個函數(shù)塊差不多,會對里面所有的內容的引用計數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,553評論 1 14
  • 中午去吃了上海老餛飩,大個的,之前經(jīng)過想去吃沒吃上,今天又路過就進去了,這家店開的也不久,兩三個月時間,不過好像生...
    鄭偉芝閱讀 262評論 0 0