[iOS] 페이드 할 팝업보기 만들기!

만든 것





・swift4 사용

코드



PopupView.swift

import Foundation
import UIKit

public class PopupView {

    private var popupView: UIView!
    static let sharedManager = PopupView()

    private func setup() {
        popupView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
        popupView.backgroundColor = UIColor.gray
        popupView.alpha = 0.8
        popupView.layer.cornerRadius = 8

        // ポップアップ内のコンテンツを設定
        let labelWidth: CGFloat = 100
        let labelHeight: CGFloat = 100
        let labelItem = UILabel(frame: CGRect(x: popupView.frame.width/2.0 - labelWidth/2, y: popupView.frame.height/2.0 - labelHeight/2, width: labelWidth, height: labelHeight))
        labelItem.text = "✅"
        labelItem.font = UIFont(name:"Helvetica", size: 70.0)
        labelItem.textAlignment = NSTextAlignment.center
        popupView.addSubview(labelItem)
    }

    public func show() {
        if popupView == nil {
            self.setup()
        }

        if let window = UIApplication.shared.delegate?.window {
            window!.addSubview(popupView)
            popupView.center = window!.center
        }
    }

    public func hide() {
        UIView.animate(withDuration: 1, delay: 1, options: .curveEaseIn, animations: {
            self.popupView.alpha = 0
        }) { _ in
            self.popupView.removeFromSuperview()
            self.popupView.alpha = 1
        }
    }
}


사용법



표시하고 싶은 타이밍에
PopupView.sharedManager.show()

숨기는 타이밍에
PopupView.sharedManager.hide()

이상 😆! !

좋은 웹페이지 즐겨찾기