新しくViewの追加
ボタンが押されたら現在のViewの上に新たにViewを追加して表示し、画面がタップされたら元の処理に戻るという動きをswiftで書きたいのですが
新たなViewを表示はできるのですが
画面をタップすると現在のViewのボタンを認識してしまい、新たなViewが消える処理が適応されず困っています。
なにかよい解決方法をご存じの方はいらっしゃいますでしょうか?
@IBOutlet weak var myimage: UIImageView!
@IBOutlet weak var mybutton1: UIButton!
@IBOutlet weak var mybutton2: UIButton!
@IBOutlet weak var mybutton3: UIButton!
@IBOutlet weak var mybutton4: UIButton!
@IBOutlet weak var label2: UILabel!
var oView:UIView!
var onandoff:Bool!
func ansview(bool:Bool){
oView = UIView(frame: CGRectMake(0,0,400,400))
oView.backgroundColor = UIColor.blackColor()
oView.alpha = 0.5
oView.layer.position = CGPointMake(self.view.frame.width/2,self.view.frame.height/2)
oView.hidden = bool
self.view.addSubview(oView)
}
var array = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44]
var names = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44]
var result:[Int] = []
var randomquiz:[Int] = []
var cnt = 4
var quiz = 1
var correctanswer : Int?
var named : [Int] = []
var truecount:Int = 0
var idx:Int? = 0;
var name1:String?
var name2:String?
var name3:String?
var name4:String?
var qcount = 0;
func randomq(){
for (var i=0;i<cnt; i++){
idx! = Int(arc4random()) % (array.count-1)
result.append(array[idx!])
array.removeAtIndex(idx!)
named.append(result[i])
}
name1 = names[named[0]]
name2 = names[named[1]]
name3 = names[named[2]]
name4 = names[named[3]]
mybutton1?.setTitle(name1, forState: UIControlState.Normal)
mybutton2?.setTitle(name2, forState: UIControlState.Normal)
mybutton3?.setTitle(name3, forState: UIControlState.Normal)
mybutton4?.setTitle(name4, forState: UIControlState.Normal)
for (var i=0;i<quiz; i++){
var idx2 : Int? = Int(arc4random()) % result.count
randomquiz.append(result[idx2!])
result.removeAtIndex(idx2!)
array.removeAtIndex(idx2!)
}
var str : String = String(randomquiz[0]) + ".jpg"
myimage?.image = UIImage(named: str)
correctanswer = Int(randomquiz[0])
}
func judge(num:Int){
qcount++;
ansview(false)
if(num == correctanswer){
NSLog("true")
truecount++
}
if(qcount == 5){
next()
}
}
func next(){
var appdel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appdel.conect = truecount
let end : AnyObject! = self.storyboard?.instantiateViewControllerWithIdentifier("end")
self.presentViewController(end as! UIViewController, animated: true, completion: nil)
}
@IBAction func button1(sender: UIButton) {
judge(named[0])
result.removeAll()
randomquiz.removeAll()
named.removeAll()
self.randomq()
}
@IBAction func button2(sender: AnyObject) {
judge(named[1])
result.removeAll()
randomquiz.removeAll()
named.removeAll()
self.randomq()
}
@IBAction func button3(sender: AnyObject) {
judge(named[2])
result.removeAll()
randomquiz.removeAll()
named.removeAll()
self.randomq()
}
@IBAction func button4(sender: AnyObject) {
judge(named[3])
result.removeAll()
randomquiz.removeAll()
named.removeAll()
self.randomq()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
label2.text = "What is my name?"
randomq()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}