Swift로 가로 대화 (AlertDialog)
17968 단어 AlertDialogSwiftSwift5
시작하기
iPhone 앱에서 기본 대화 상자 (AlertDialog)
가로로 운용할 수 없었기 때문에 자작했다.
좌표는 iPhone11Pro를 기반으로 하고, 화면 중심에 정사각형으로 표시되도록 하고 있다.
디폴트 Alert를 가로 방향으로 표시하는 방법은 일단은 발견되었지만,
버튼 선택해 사라지는 순간에 세로 표시가 되어 버리기 때문에 불채용으로 했다.
참고
화면 이미지
출처
Vc.swift
import UIKit
//変数
var msgDialog01: MessageDialog?
class ViewController: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//ダイアログ準備
msgDialog01 = MessageDialog(self.view, true)
msgDialog01?.prepare("よろしいですか?", execFunc)
//実行
msgDialog01?.show()
}
func execFunc(){
print("execFunc.")
}
}
//画面表示用ダイアログクラス
public class MessageDialog{
//操作無効用の半透明フィルム
private let vwFilm = UIView()
//ダイアログ
private let vwDialog = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 300, height: 300))
//メッセージ
private let lblMsg = UILabel.init(frame: CGRect.init(x: 20, y: 20, width: 260, height: 200))
//キャンセルボタン
private let btnCancel = UIView.init(frame: CGRect.init(x: 20, y: 230, width: 120, height: 50))
private let lblCancel = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: 120, height: 50))
//OKボタン
private let btnOk = UIView.init(frame: CGRect.init(x: 160, y: 230, width: 120, height: 50))
private let lblOk = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: 120, height: 50))
//Ok押下時の実行関数
public var okAct: (() -> Void)?
init(_ mainView: UIView, _ isRotate: Bool){
//半透明部分
vwFilm.isHidden = true
vwFilm.alpha = 0.5
vwFilm.backgroundColor = .black
//ダイアログ
vwDialog.isHidden = true
vwDialog.backgroundColor = .systemGray5
vwDialog.alpha = 1
//Msg
vwDialog.addSubview(lblMsg)
lblMsg.numberOfLines = 5
lblMsg.textAlignment = .center
//okボタン
btnOk.addSubview(lblOk)
vwDialog.addSubview(btnOk)
lblOk.text = "OK"
lblOk.textAlignment = .center
lblOk.textColor = .white
btnOk.backgroundColor = .systemBlue
//Cancelボタン
btnCancel.addSubview(lblCancel)
vwDialog.addSubview(btnCancel)
lblCancel.text = "Cancel"
lblCancel.textColor = .white
lblCancel.textAlignment = .center
btnCancel.backgroundColor = .darkGray
setTapOneEvent()
mainView.addSubview(vwFilm)
mainView.addSubview(vwDialog)
vwDialog.center = mainView.center
vwFilm.frame = mainView.frame
if isRotate{
vwDialog.transform = CGAffineTransform(rotationAngle: CGFloat( CGFloat.pi / 2 * 1))
}
}
func prepare(_ msg: String, _ okAct: (() -> Void)?){
self.lblMsg.text = msg
self.okAct = okAct
}
func show(){
vwFilm.isHidden = false
vwDialog.isHidden = false
}
func hide(){
vwFilm.isHidden = true
vwDialog.isHidden = true
}
//タップイベントをセットする
private func setTapOneEvent(){
let tapOk = UITapGestureRecognizer(target: self, action: #selector(btnOk_Tap))
tapOk.numberOfTouchesRequired = 1
tapOk.numberOfTapsRequired = 1
btnOk.addGestureRecognizer(tapOk)
let tapCancel = UITapGestureRecognizer(target: self, action: #selector(btanCancel_Tap))
tapCancel.numberOfTouchesRequired = 1
tapCancel.numberOfTapsRequired = 1
btnCancel.addGestureRecognizer(tapCancel)
}
@objc private func btnOk_Tap(){
self.okAct?()
hide()
}
@objc private func btanCancel_Tap(){
hide()
}
}
Reference
이 문제에 관하여(Swift로 가로 대화 (AlertDialog)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/Nao9syu/items/e0f27b3e8f8554341abc
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
출처
Vc.swift
import UIKit
//変数
var msgDialog01: MessageDialog?
class ViewController: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//ダイアログ準備
msgDialog01 = MessageDialog(self.view, true)
msgDialog01?.prepare("よろしいですか?", execFunc)
//実行
msgDialog01?.show()
}
func execFunc(){
print("execFunc.")
}
}
//画面表示用ダイアログクラス
public class MessageDialog{
//操作無効用の半透明フィルム
private let vwFilm = UIView()
//ダイアログ
private let vwDialog = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 300, height: 300))
//メッセージ
private let lblMsg = UILabel.init(frame: CGRect.init(x: 20, y: 20, width: 260, height: 200))
//キャンセルボタン
private let btnCancel = UIView.init(frame: CGRect.init(x: 20, y: 230, width: 120, height: 50))
private let lblCancel = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: 120, height: 50))
//OKボタン
private let btnOk = UIView.init(frame: CGRect.init(x: 160, y: 230, width: 120, height: 50))
private let lblOk = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: 120, height: 50))
//Ok押下時の実行関数
public var okAct: (() -> Void)?
init(_ mainView: UIView, _ isRotate: Bool){
//半透明部分
vwFilm.isHidden = true
vwFilm.alpha = 0.5
vwFilm.backgroundColor = .black
//ダイアログ
vwDialog.isHidden = true
vwDialog.backgroundColor = .systemGray5
vwDialog.alpha = 1
//Msg
vwDialog.addSubview(lblMsg)
lblMsg.numberOfLines = 5
lblMsg.textAlignment = .center
//okボタン
btnOk.addSubview(lblOk)
vwDialog.addSubview(btnOk)
lblOk.text = "OK"
lblOk.textAlignment = .center
lblOk.textColor = .white
btnOk.backgroundColor = .systemBlue
//Cancelボタン
btnCancel.addSubview(lblCancel)
vwDialog.addSubview(btnCancel)
lblCancel.text = "Cancel"
lblCancel.textColor = .white
lblCancel.textAlignment = .center
btnCancel.backgroundColor = .darkGray
setTapOneEvent()
mainView.addSubview(vwFilm)
mainView.addSubview(vwDialog)
vwDialog.center = mainView.center
vwFilm.frame = mainView.frame
if isRotate{
vwDialog.transform = CGAffineTransform(rotationAngle: CGFloat( CGFloat.pi / 2 * 1))
}
}
func prepare(_ msg: String, _ okAct: (() -> Void)?){
self.lblMsg.text = msg
self.okAct = okAct
}
func show(){
vwFilm.isHidden = false
vwDialog.isHidden = false
}
func hide(){
vwFilm.isHidden = true
vwDialog.isHidden = true
}
//タップイベントをセットする
private func setTapOneEvent(){
let tapOk = UITapGestureRecognizer(target: self, action: #selector(btnOk_Tap))
tapOk.numberOfTouchesRequired = 1
tapOk.numberOfTapsRequired = 1
btnOk.addGestureRecognizer(tapOk)
let tapCancel = UITapGestureRecognizer(target: self, action: #selector(btanCancel_Tap))
tapCancel.numberOfTouchesRequired = 1
tapCancel.numberOfTapsRequired = 1
btnCancel.addGestureRecognizer(tapCancel)
}
@objc private func btnOk_Tap(){
self.okAct?()
hide()
}
@objc private func btanCancel_Tap(){
hide()
}
}
Reference
이 문제에 관하여(Swift로 가로 대화 (AlertDialog)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/Nao9syu/items/e0f27b3e8f8554341abc
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
import UIKit
//変数
var msgDialog01: MessageDialog?
class ViewController: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//ダイアログ準備
msgDialog01 = MessageDialog(self.view, true)
msgDialog01?.prepare("よろしいですか?", execFunc)
//実行
msgDialog01?.show()
}
func execFunc(){
print("execFunc.")
}
}
//画面表示用ダイアログクラス
public class MessageDialog{
//操作無効用の半透明フィルム
private let vwFilm = UIView()
//ダイアログ
private let vwDialog = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 300, height: 300))
//メッセージ
private let lblMsg = UILabel.init(frame: CGRect.init(x: 20, y: 20, width: 260, height: 200))
//キャンセルボタン
private let btnCancel = UIView.init(frame: CGRect.init(x: 20, y: 230, width: 120, height: 50))
private let lblCancel = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: 120, height: 50))
//OKボタン
private let btnOk = UIView.init(frame: CGRect.init(x: 160, y: 230, width: 120, height: 50))
private let lblOk = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: 120, height: 50))
//Ok押下時の実行関数
public var okAct: (() -> Void)?
init(_ mainView: UIView, _ isRotate: Bool){
//半透明部分
vwFilm.isHidden = true
vwFilm.alpha = 0.5
vwFilm.backgroundColor = .black
//ダイアログ
vwDialog.isHidden = true
vwDialog.backgroundColor = .systemGray5
vwDialog.alpha = 1
//Msg
vwDialog.addSubview(lblMsg)
lblMsg.numberOfLines = 5
lblMsg.textAlignment = .center
//okボタン
btnOk.addSubview(lblOk)
vwDialog.addSubview(btnOk)
lblOk.text = "OK"
lblOk.textAlignment = .center
lblOk.textColor = .white
btnOk.backgroundColor = .systemBlue
//Cancelボタン
btnCancel.addSubview(lblCancel)
vwDialog.addSubview(btnCancel)
lblCancel.text = "Cancel"
lblCancel.textColor = .white
lblCancel.textAlignment = .center
btnCancel.backgroundColor = .darkGray
setTapOneEvent()
mainView.addSubview(vwFilm)
mainView.addSubview(vwDialog)
vwDialog.center = mainView.center
vwFilm.frame = mainView.frame
if isRotate{
vwDialog.transform = CGAffineTransform(rotationAngle: CGFloat( CGFloat.pi / 2 * 1))
}
}
func prepare(_ msg: String, _ okAct: (() -> Void)?){
self.lblMsg.text = msg
self.okAct = okAct
}
func show(){
vwFilm.isHidden = false
vwDialog.isHidden = false
}
func hide(){
vwFilm.isHidden = true
vwDialog.isHidden = true
}
//タップイベントをセットする
private func setTapOneEvent(){
let tapOk = UITapGestureRecognizer(target: self, action: #selector(btnOk_Tap))
tapOk.numberOfTouchesRequired = 1
tapOk.numberOfTapsRequired = 1
btnOk.addGestureRecognizer(tapOk)
let tapCancel = UITapGestureRecognizer(target: self, action: #selector(btanCancel_Tap))
tapCancel.numberOfTouchesRequired = 1
tapCancel.numberOfTapsRequired = 1
btnCancel.addGestureRecognizer(tapCancel)
}
@objc private func btnOk_Tap(){
self.okAct?()
hide()
}
@objc private func btanCancel_Tap(){
hide()
}
}
Reference
이 문제에 관하여(Swift로 가로 대화 (AlertDialog)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/Nao9syu/items/e0f27b3e8f8554341abc텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)