가장 잘 쓰고 있는 하나중에 손에 꼽힌다.

 

안드로이드에 있는 Toast를 똑같이 구현해본거다. 

원래는 상 | 중 | 하 단으로 선택해서 구현을 했지만, 딱히 상 | 하 단을 이용하지 않아 지워버렸다!

 

써보고 싶은 사람은 써보고 개선할점이 있으면 알려주기 바랍니다.

아니면 개선해서 알려주세요 ㅎㅎ

 

xib 파일은 다운받아서 써보세요. 쓰는 방법은

 

Toast.showToastMessage(message: "")

let kTOAST_DURATION_VALUE: TimeInterval = 0.5
let kTOAST_HEIGHT_VALUE: CGFloat = 80.0
let kTOAST_WIDTH_RATE_VALUE: CGFloat = 0.9

class Toast: UIView {
    
    @IBOutlet weak var msg_lb: UILabel!

    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    class func showToastMessage(message: String) {
        if let topVC = Utils.topViewController() {
            let name = String(describing: Toast.self)
            guard let loadedNib = Bundle.main.loadNibNamed(name, owner: self, options: nil) else { return }
            guard let toast = loadedNib.first as? Toast else { return }
            toast.msg_lb.text = message
            toast.show(activeView: topVC.view, message: message)
            
            toast.translatesAutoresizingMaskIntoConstraints = false
            toast.centerXAnchor.constraint(equalTo:topVC.view.centerXAnchor).isActive = true
            toast.centerYAnchor.constraint(equalTo:topVC.view.centerYAnchor).isActive = true
            toast.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.size.width * kTOAST_WIDTH_RATE_VALUE).isActive = true
        }
    }
    
    func show(activeView :UIView, message:String) {
        self.msg_lb.layer.cornerRadius = 5
        self.msg_lb.text = message
        
        self.alpha = 0
        self.isUserInteractionEnabled = false
        
        activeView.addSubview(self)
        
        UIView.animate(withDuration: kTOAST_DURATION_VALUE, delay: 0, options: .curveEaseIn, animations: {
            self.alpha = 1.0
        }) { (finished) in
            self.isUserInteractionEnabled = true
            UIView.animate(withDuration: kTOAST_DURATION_VALUE, delay: 1.0, options: .curveEaseIn, animations: {
                self.alpha = 0.0
            }) { (finished) in
                self.removeFromSuperview()
            }
        }
    }
}

 

Toast.swift
0.00MB
Toast.xib
0.00MB

 

 


 

'[ 자기개발 ] > [ Swift ]' 카테고리의 다른 글

Alert.swift  (0) 2020.11.19
UIColor+Extension.swift  (0) 2020.11.19

 

 

UIAlertController | UIAlertAction

 

더 좋은 방법이 있을까요?

 

 

Alert.swift

class Alert: NSObject {
    class func showAlert(title :String, message :String, actions :[String], completion :@escaping (_ text :String, _ index :NSInteger) -> Void) -> Void {
        let alert :UIAlertController    = UIAlertController.init(title: title, message: message, preferredStyle: .alert)
        for i in 0 ..< actions.count {
            let action :UIAlertAction = UIAlertAction.init(title: actions[i],
                                                           style: .default) { (action) in
                completion(actions[i], i)
            }
            alert.addAction(action)
        }
        alert.modalPresentationStyle = .popover
        
        // getTopViewController : 최상단 뷰 컨트롤러
        getTopViewController()?.present(alert, animated: true, completion: {
            
        })
    }
    
    class func showAction(title :String, message :String, actions :[String], completion :@escaping (_ text :String, _ index :NSInteger) -> Void) -> Void {
        let alert :UIAlertController    = UIAlertController.init(title: title, message: message, preferredStyle: .actionSheet)
        for i in 0 ..< actions.count {
            let action :UIAlertAction = UIAlertAction.init(title: actions[i],
                                                           style: i == (actions.count-1) ? .cancel : .default) { (action) in
                completion(actions[i], i)
            }
            alert.addAction(action)
        }
        alert.modalPresentationStyle = .popover
        alert.view.tintColor = UIColor.black

	// getTopViewController : 최상단 뷰 컨트롤러
        getTopViewController()?.present(alert, animated: true, completion: {
            
        })
    }
}

 

사용방법

 

Alert 사용방법

Alert.showAlert(title: "[ 타이틀 ]",
                        message: "[ 메세지 ]]",
                        actions: ["액션 - 1","액션 - 2","액션 - 3","액션 - 4","액션 - 5","닫기"]) { (text, index) in
    if text != "닫기" {
                
    }
}
더보기

Alert 결과화면

showAlert 호출 화면

 

 

ActionSheet 사용방법

Alert.showAction(title: "[ 타이틀 ]",
                        message: "[ 메세지 ]]",
                        actions: ["액션 - 1","액션 - 2","액션 - 3","액션 - 4","액션 - 5","닫기"]) { (text, index) in
    if text != "닫기" {
                
    }
}
더보기

ActionSheet 결과화면

 

showAction 호출 화면

'[ 자기개발 ] > [ Swift ]' 카테고리의 다른 글

Toast.Swift  (0) 2021.04.27
UIColor+Extension.swift  (0) 2020.11.19

 

 

UIColor+Extension.swift

extension UIColor {
    public convenience init?(hex: String) {
        let r, g, b, a: CGFloat

        if hex.hasPrefix("#") {
            let start = hex.index(hex.startIndex, offsetBy: 1)
            let hexColor = String(hex[start...])

            if hexColor.count == 8 {
                let scanner = Scanner(string: hexColor)
                var hexNumber: UInt64 = 0

                if scanner.scanHexInt64(&hexNumber) {
                    r = CGFloat((hexNumber & 0xff000000) >> 24) / 255
                    g = CGFloat((hexNumber & 0x00ff0000) >> 16) / 255
                    b = CGFloat((hexNumber & 0x0000ff00) >> 8) / 255
                    a = CGFloat(hexNumber & 0x000000ff) / 255

                    self.init(red: r, green: g, blue: b, alpha: a)
                    return
                }
            }
        }
        return nil
    }
}

 

 

사용방법

UIColor.init(hex: "#ffe700ff")

 

'[ 자기개발 ] > [ Swift ]' 카테고리의 다른 글

Toast.Swift  (0) 2021.04.27
Alert.swift  (0) 2020.11.19

+ Recent posts