iOS - SWift3 & XCode8

來源:互聯網
上載者:User

標籤:msu   object   調用   gray   pre   工作   round   quit   timer   

1. 使用資源檔夾匯入並管理圖片素材

/*

 *資源檔夾可以方便您進行圖片管理,在讀取圖片時,不需要加片名的尾碼.同時還可以提高軟體的安全性,它會講圖片都加密壓縮,

 *並儲存到   Assets.car檔案中.

 */

//建立一個映像類,用於載入和繪製映像.

        let img = UIImage(named: "Pic2.png")  //待測(XCode):let img = UIImage(named: "Pic2")

        let imgView = UIImageView(image: img)

        self.view.addSubview(imgView)

 

2. 應用程式生命週期

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        // Override point for customization after application launch.

        //將程式載入後需要執行的代碼,寫在程式完成載入的方法裡面,常用方法

        print(">>>didFinishLaunchingWithOptions")  

        return true

    }

    func applicationWillResignActive(application: UIApplication) {

        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

        //非使用中,期間,程式不接受訊息和事件

        print(">>>applicationWillResignActive")

    }

    func applicationDidEnterBackground(application: UIApplication) {

        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

        //程式被推送到背景時候,調用次方法,若設定後台繼續某些動作,則在這個方法裡面添加代碼即可

        print(">>> applicationDidEnterBackground") 

    }

    func applicationWillEnterForeground(application: UIApplication) {

        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

        //後台 -> 前台

    }

    func applicationDidBecomeActive(application: UIApplication) {

        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

        

        //進入活動狀態時,執行此方法

    }

    func applicationWillTerminate(application: UIApplication) {

        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

        //程式將要退出時,調用此方法. 通常是用來儲存資料,和一些退出前的清理工作

    }

 

 

3 快速尋找並開啟檔案

        //1. 選擇[快速開啟]子功能表,快速尋找項目文檔,當你工作一個龐大的項目中時,這個功能會為你提供很大便利 

        //2. 點擊鍵盤上的按鈕,輸入關鍵字來過濾文檔

        //快速鍵方式: Command + Shift + O

 

4. 快速更改同名變數

    //在代碼的編輯地區,點擊定位需要更改名稱的變數

    //開啟功能表列中的編輯菜單 -> 選擇重構此菜單 -> 選擇重新命名命令,此命令允許統一修改文檔中的某個變數. -> 在名稱框輸入新的名稱

 

5. 代碼的尋找和替換

 

6. 視圖的基本使用

        let rect1 = CGRect(x: 30, y: 50, width: 200, height: 200)

        let view1 = UIView(frame: rect1)

        view1.backgroundColor = UIColor.brownColor()

        

        let rect2 = CGRect(x: 90, y: 120, width: 200, height: 200)

        let view2 = UIView(frame: rect2)

        view2.backgroundColor = UIColor.purpleColor()

        

        self.view.addSubview(view1)

        self.view.addSubview(view2)

 

7. 視圖的層次關係   

        let view1 = UIView(frame: CGRect(x: 20, y: 80, width: 280, height: 280))

        view1.backgroundColor = UIColor.redColor()

        self.view.addSubview(view1)

        let view2 = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))

        //設定視圖本地座標系統中的位置和大小,它會影響子視圖的位置和顯示

        view2.bounds = CGRect(x: -40, y: -20, width: 200, height: 200)

        view2.backgroundColor = UIColor.yellowColor()

        self.view.addSubview(view2)

        let viewSub = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))

        viewSub.backgroundColor = UIColor.blueColor()

        view2.addSubview(viewSub)

 

 

8. 視圖的基本操作

 override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        

        //視圖的添加與刪除、切換視圖在父視圖中的層次

        let rect = CGRect(x: 30, y: 50, width: 200, height: 200)

        let view = UIView(frame: rect)

        view.backgroundColor = UIColor.brownColor()

        self.view.addSubview(view)

        

        

        let btAdd = UIButton(frame: CGRect(x: 30, y: 350, width: 80, height: 30))

        btAdd.backgroundColor = UIColor.grayColor()

        btAdd.setTitle("Add", forState: UIControlState())

        btAdd.addTarget(self, action: #selector(ViewController.addView(_:)), forControlEvents: UIControlEvents.TouchUpInside)

        self.view.addSubview(btAdd);

 

        

        

        let btBack = UIButton(frame: CGRect(x: 120, y: 350, width: 80, height: 30))

        btBack.backgroundColor = UIColor.grayColor()

        btBack.setTitle("Switch", forState: UIControlState())

        btBack.addTarget(self, action: #selector(ViewController.bringViewBack(_:)), forControlEvents:UIControlEventTouchDragExit)

        self.view.addSubview(btBack);

        

        

        let btRemove = UIButton(frame: CGRect(x: 210, y: 350, width: 80, height: 30))

        btRemove.backgroundColor = UIColor.grayColor()

        btRemove.setTitle("Remove", forState: UIControlState())

        btRemove.addTarget(self, action: #selector(ViewController.removeView(_:)), forControlEvents:UIControlEventTouchDragExit)

        self.view.addSubview(btRemove);

        

    }

    

    func addView(_ sender:UIButton)

    {

        

        let rect = CGRect(x: 60, y: 90, width: 200, height: 200)

        let view = UIView(frame: rect)

        view.backgroundColor = UIColor.purpleColor()

        view.tag = 1;

        

        self.view.addSubview(view)

    }

    

    func bringViewBack(_sender:UIButton)

    {

        

        let view = self.view.viewWithTag(1)

        self.view.sendSubviewToBack(view!)

    }

    

    func removeFromView(_sender:UIButton)

    {

        let view = self.view.viewWithTag(1)

        view?.removeFromSuperview();

    }

 

iOS - SWift3 & XCode8

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.