"まだ"の力 [Swift]基礎辞書

学んだことを書いていきます。質問やエラーなどございましたらお気軽にコメントお願いします。

「UIWindow」

これを実装したかった!「UIWindow」これを実装することによって最初の使い方の説明やリザルト画面など実装できると思います。
こちらのサイトを参考にしてサンプルを書いてみました!



026 UIWindowの表示 - Swift Docs

import UIKit

class ViewController: UIViewController {
    
    var iWindow: UIWindow! = UIWindow()
    var iWindowButton: UIButton! = UIButton()
    var iButton: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
        // 背景に画像を設定
        //自分で作成したメソッドを実行
        imageMake("aozora.png",x: 0,y: 0, width: view.layer.frame.width, height: view.layer.frame.height/3)
        imageMake("yuuyake.png",x: 0,y: view.layer.frame.height/3, width: view.layer.frame.width, height: view.layer.frame.height/3)
        imageMake("yoru.png", x: 0, y: view.layer.frame.height*2/3, width: view.layer.frame.width, height: view.layer.frame.height/3)
        buttonMake(1, position_x: view.layer.frame.width/2, position_y: view.layer.frame.height/3)
        buttonMake(2, position_x: view.layer.frame.width/2, position_y: view.layer.frame.height*2/3)
    }
    
    /*
    自作Windowを生成する
    */
    func makeMyWindow(text:String){
        
        // 背景を白に設定する.
        iWindow.backgroundColor = UIColor.whiteColor()
        //大きさ
        iWindow.frame = CGRectMake(0, 0, 200, 250)
        //位置
        iWindow.layer.position = CGPointMake(self.view.frame.width/2, self.view.frame.height/2)
        //透明度
        iWindow.alpha = 0.8
        
        
        // myWindowを最前のwindowにする
        iWindow.makeKeyWindow()
        
        // windowを表示する.
        self.iWindow.makeKeyAndVisible()
        
        // ボタンを作成する.ボタンに関しては後述で記事を書きます。
        iWindowButton.frame = CGRectMake(0, 0, 100, 60)
        iWindowButton.backgroundColor = UIColor.orangeColor()
        iWindowButton.setTitle("Close", forState: .Normal)
        iWindowButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        iWindowButton.layer.masksToBounds = true
        iWindowButton.layer.cornerRadius = 20.0
        iWindowButton.layer.position = CGPointMake(self.iWindow.frame.width/2, self.iWindow.frame.height-50)
        iWindowButton.addTarget(self, action: "ClickiButton:", forControlEvents: .TouchUpInside)
        self.iWindow.addSubview(iWindowButton)
        
        // TextViewを作成する.
        let iTextView: UITextView = UITextView(frame: CGRectMake(10, 10, self.iWindow.frame.width - 20, 150))
        iTextView.backgroundColor = UIColor.clearColor()
        iTextView.text = text
        iTextView.font = UIFont.systemFontOfSize(CGFloat(15))
        iTextView.textColor = UIColor.blackColor()
        iTextView.textAlignment = NSTextAlignment.Left
        iTextView.editable = false
        
        self.iWindow.addSubview(iTextView)
    }
    
    /*
    ボタンイベント
    */
    func ClickiButton(sender: UIButton) {
        //ClickiButtonを呼び出したボタンがiWindowButtonならば、
        if sender == iWindowButton {
            //隠す
            iWindow.hidden = true
        }
            //ClickiButtonを呼び出したボタンがiButtonならば、
        else if sender == iButton{
            if iButton.tag == 1 {makeMyWindow("気象庁によると15時")}
            else if iButton.tag == 2 {makeMyWindow("気象庁によると18時")}
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    
    //ボタン作成関数
    func buttonMake (tag:Int,position_x:CGFloat,position_y:CGFloat) -> UIButton {
        // ボタンを生成する.
        iButton = UIButton(frame: CGRectMake(0, 0, 75, 75))
        iButton.backgroundColor = UIColor.greenColor()
        iButton.setTitle("境目は?", forState: .Normal)
        iButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        iButton.layer.masksToBounds = true
        iButton.layer.cornerRadius = 30.0
        iButton.layer.position = CGPointMake(position_x ,position_y)
        iButton.addTarget(self, action: "ClickiButton:", forControlEvents: .TouchUpInside)
        iButton.tag = tag    //tagでどのボタンか区別できるようにする
        //ボタン追加
        self.view.addSubview(iButton)
        return iButton
    }
    func imageMake(imagenamed:String,x:CGFloat,y:CGFloat,width:CGFloat,height:CGFloat) {
        //UIImageViewの使い方に関しては過去記事をご覧ください。
        let Image1: UIImage = UIImage(named: imagenamed)!
        let ImageView1: UIImageView = UIImageView()
        ImageView1.image = Image1
        ImageView1.frame = CGRectMake(x, y, width, height)
        self.view.addSubview(ImageView1)
    }
}

kichie-com.hatenablog.com

f:id:kichie_com:20151229170024p:plainf:id:kichie_com:20151229170025p:plain

また一つレベルアップした気がします!

ちょっと一言

知識は日々積み重ねることが一番苦も少なく身につきやすい。「ローマは一日にしてならず」