Xcode Version 6.0.1로 확인 완료!Hello World 다음에 해보고 싶어요!UIWindow 표시/숨기기

16813 단어 Xcode6.0.1SwiftiOS8

사이트 축소판 그림


[최신 버전] Xcode 6 Version 6.0.1 확인 완료!Swift Hello World를 만드는 방법
Xcode Version 6.0.1로 확인 완료!Hello World 다음에 해보고 싶어요!웹 뷰를 간단히 표시하기

1·SingleView를 사용한 새 프로젝트 만들기


UIWindow Sample 프로젝트 이름으로 제작되었습니다.



2. view Controller는 다음과 같이 수정되었습니다.(화면 단추는 원본 파일에서 생성됩니다.)

//
//  ViewController.swift
//  UIWindowSample
//
//  Created by g08m11 on 2014/09/21.
//  Copyright (c) 2014年 Bloc. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

  // 初期化(他のメソッドで汎用的に使うためにここに記述)
  let myWindow = UIWindow()
  let myWindowButton = UIButton()
  let myButton = UIButton()


  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    // 背景に画像を設定する
    // 画像は自分で用意。
    let myImage = UIImage(named: "syuri.jpg")
    var myImageView = UIImageView()
    myImageView.image = myImage
    myImageView.frame = CGRectMake(0, 0, myImage.size.width, myImage.size.height)
    self.view.addSubview(myImageView)

    // ボタン生成
    myButton.frame = CGRectMake(0, 0, 60, 60)
    myButton.backgroundColor = UIColor.redColor()
    myButton.setTitle("Dialog", forState: .Normal)
    myButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
    myButton.layer.masksToBounds = true
    myButton.layer.cornerRadius = 30.0
    myButton.layer.position = CGPointMake(self.view.frame.width/2, self.view.frame.height-100)
    myButton.addTarget(self, action: "onClickMyButton:", forControlEvents: .TouchUpInside)

    self.view.addSubview(myButton)

  }

  // Windowの自作

  func makeMyWindow(){

    // 背景を白に設定
    myWindow.backgroundColor = UIColor.whiteColor()
    myWindow.frame = CGRectMake(0, 0, 200, 250)
    myWindow.layer.position = CGPointMake(self.view.frame.width/2, self.view.frame.height/2)
    myWindow.alpha = 0.8
    myWindow.layer.cornerRadius = 20

    // myWindowをkeyWindowにする
    myWindow.makeKeyWindow()

    // windowを表示する
    self.myWindow.makeKeyAndVisible()

    // ボタン生成
    myWindowButton.frame = CGRectMake(0, 0, 100, 60)
    myWindowButton.backgroundColor = UIColor.orangeColor()
    myWindowButton.setTitle("Close", forState: .Normal)
    myWindowButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
    myWindowButton.layer.masksToBounds = true
    myWindowButton.layer.cornerRadius = 20.0
    myWindowButton.layer.position = CGPointMake(self.myWindow.frame.width/2, self.myWindow.frame.height-50)
    myWindowButton.addTarget(self, action: "onClickMyButton:", forControlEvents: .TouchUpInside)
    self.myWindow.addSubview(myWindowButton)

    // TextView生成
    // 表示したい文言の変更は「myTextView.text」を変更
    let myTextView: UITextView = UITextView(frame: CGRectMake(10, 10, self.myWindow.frame.width - 20, 150))
    myTextView.backgroundColor = UIColor.clearColor()
    myTextView.text = "首里城は、琉球王国の政治・外交・文化の中心として栄華を誇りました。"
    myTextView.font = UIFont.systemFontOfSize(CGFloat(15))
    myTextView.textColor = UIColor.blackColor()
    myTextView.textAlignment = NSTextAlignment.Left
    myTextView.editable = false

    self.myWindow.addSubview(myTextView)
  }

  /*
  ボタンイベント
  */
  func onClickMyButton(sender: UIButton) {

    if sender == myWindowButton {
      myWindow.hidden = true
    } else if sender == myButton {
      makeMyWindow()
    }
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
  }


}

point

  • 코드에 모든 버튼 등 생성
  • 버튼의 동작은 "onClickMyButon"방법을 통해 수행됩니다.
  • 3. UIWindow를 표시하고 숨길 수 있다면 성공



    좋은 웹페이지 즐겨찾기