コードを用いて、下イメージ図のようにボタンを作成しました。数字ボタンの上に白いスペースがあり、入力した数字を左詰で表示します。銀行の暗証番号入力のように、特定の数字を入れることでアンロックされるようにします。
問題点として、1桁の数字であればlabelに表示できますが、2桁、3桁と、押した数字を順番通りに並べるとなったとき、どのような方法で行えばよいか、なかなか案が出ず困っています。
複数のlabelに分けて行ったほうがよいのか、一つのlabelで工夫し、押した順番通りに表示することが可能なのか…
お力をいただけると嬉しいです。
コードを付しておきます。
Swift
1import UIKit 2import AVFoundation 3 4class NumberPlateViewController: UIViewController { 5 @IBOutlet weak var label: UILabel! 6 7let zero = UIButton() 8let one = UIButton() 9//以下、9・決定ボタンまで同様 10 11override func viewDidLoad() { 12 super.viewDidLoad() 13 14//Buttonの設置場所を座標で決定 15 one.frame = CGRect(x:83,y:395,width:70,height:70) 16 two.frame = CGRect(x:154,y:395,width:70,height:70) 17//以下、9・決定ボタンまで(座標以外)同様 18 19//背景色 20 one.backgroundColor = UIColor.white 21 two.backgroundColor = UIColor.white 22//以下同様 23 24//文字色 25 one.setTitleColor(UIColor.black, for: UIControlState.normal) 26 two.setTitleColor(UIColor.black, for: UIControlState.normal) 27//以下同様 28 29//Buttonをタッチした時の背景色 30 one.setBackgroundImage(self.colorToImage(color: UIColor.gray), for: UIControlState.highlighted) 31 two.setBackgroundImage(self.colorToImage(color: UIColor.gray), for: UIControlState.highlighted) 32//以下同様 33 34//Buttonを押したときのアクションをするための渡し 35 one.addTarget(self, action: #selector(NumberPlateViewController.actionone(sender: )), for: .touchDown) 36 two.addTarget(self, action: #selector(NumberPlateViewController.actiontwo(sender: )), for: .touchDown) 37//以下同様 38 39//表示する文字 40 one.setTitle("1", for: .normal) 41 two.setTitle("2", for: .normal) 42//以下同様 43 44//文字サイズ 45 one.titleLabel?.font = UIFont.systemFont(ofSize: 40) 46 two.titleLabel?.font = UIFont.systemFont(ofSize: 40) 47//以下同様 48 49//角丸 50 one.layer.cornerRadius = 20 51 two.layer.cornerRadius = 20 52//以下同様(決定ボタンのみ値は15) 53 54//角丸を実行 55 one.layer.masksToBounds = true 56 two.layer.masksToBounds = true 57//以下同様 58 59//ButtonをViewに表示 60 view.addSubview(one) 61 view.addSubview(two) 62 view.addSubview(three) 63 view.addSubview(four) 64 view.addSubview(five) 65 view.addSubview(six) 66 view.addSubview(seven) 67 view.addSubview(eight) 68 view.addSubview(nine) 69 view.addSubview(zero) 70 view.addSubview(kettei) 71 72} 73 74func colorToImage(color:UIColor) -> UIImage { 75 let rect = CGRect(x:0,y:0,width:1.0,height:1.0) 76 UIGraphicsBeginImageContext(rect.size) 77 78 let context : CGContext = UIGraphicsGetCurrentContext()! 79 context.setFillColor(color.cgColor) 80 context.fill(rect) 81 82 let image : UIImage = UIGraphicsGetImageFromCurrentImageContext()! 83 UIGraphicsEndImageContext() 84 return image 85 } 86 87 88@objc func actionone(sender:Any) { 89 sound?.play() 90 sound?.currentTime = 0 91 self.label.alpha = 1.0 92 self.label.text = "1" } 93 94 @objc func actiontwo(sender:Any) { 95 sound?.play() 96 sound?.currentTime = 0 97 self.label.alpha = 1.0 98 self.label.text = "2" 99 } 100 101 @objc func actionthree(sender:Any) { 102 sound?.play() 103 sound?.currentTime = 0 104 self.label.alpha = 1.0 105 self.label.text = "3" 106 } 107 108 @objc func actionfour(sender:Any) { 109 sound?.play() 110 sound?.currentTime = 0 111 self.label.alpha = 1.0 112 self.label.text = "4" 113 } 114 115 @objc func actionfive(sender:Any) { 116 sound?.play() 117 sound?.currentTime = 0 118 self.label.alpha = 1.0 119 self.label.text = "5" 120 } 121 122 @objc func actionsix(sender:Any) { 123 sound?.play() 124 sound?.currentTime = 0 125 self.label.alpha = 1.0 126 self.label.text = "6" 127 } 128 129 @objc func actionseven(sender:Any) { 130 sound?.play() 131 sound?.currentTime = 0 132 self.label.alpha = 1.0 133 self.label.text = "7" 134 } 135 136 @objc func actioneight(sender:Any) { 137 sound?.play() 138 sound?.currentTime = 0 139 self.label.alpha = 1.0 140 self.label.text = "8" 141 } 142 143 @objc func actionnine(sender:Any) { 144 sound?.play() 145 sound?.currentTime = 0 146 self.label.alpha = 1.0 147 self.label.text = "9" 148 } 149 150 @objc func actionzero(sender:Any) { 151 sound?.play() 152 sound?.currentTime = 0 153 self.label.alpha = 1.0 154 self.label.text = "0" 155 } 156 157 @objc func actionkettei(sender:Any) { 158 sound?.play() 159 sound?.currentTime = 0 160 161 dismiss(animated: true, completion: nil) 162 } 163 164 165 override func didReceiveMemoryWarning() { 166 super.didReceiveMemoryWarning() 167 } 168 169} 170
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/01/21 05:07