2016年10月31日 星期一

swift3 Day17 隱藏狀態列 ..失敗?

今天看到專案設定裡面就有可以控制是否要顯示狀態列的選項General -> Deployment底下有Hide status bar(隱藏狀態列)Requires full screen(全螢幕)預設都不勾 但是不知道為何我兩個都打勾了之後,在模擬器上面看到的還是有狀態列..&nb...

2016年10月30日 星期日

swift3 Day16 UIAlertController 中放進 UIPickerView

要在 alert 中加進 pickerView 可以直接 add 就好:alertController.view.addSubview(myPickerView) 加進去之後 pickerView 的 x, y 會以 alert 的位置開始算,有點像 margin-left, margin-top 大小、位置要在不同裝置使用很不好控制!  參考來源:https://itisjoe.gitbooks.io/swiftgo/content/uikit/uialertcontroller.htmlhttps://itisjoe.gitbooks.io/swiftgo/content/uikit/uipickerview.htmlhttp://stackoverflow.com/questions/33233894/want-to-show-ui...

2016年10月29日 星期六

swift3 Day15 UIButton setBackgroundImage 圖片自動調整大小

用 setBackgroundImage 的方式,背景圖片會自動符合 UIButton 大小圖片如果是方的,UIButton 是長的,圖片會被拉長! 設定 UIButton 的方法:let image = UIImage(named: "圖片檔名.副檔名") as UIImage?testBtn.setBackgroundImage(image, for: .normal) 我放的圖...

2016年10月28日 星期五

swift3 Day14 UIView 物件調整順序

先寫好的 UI物件 就會先創出來,後寫的會疊在上面。調動順序將物件往上層移要用 bringSubview ,往下層移用 sendSubview: 實際運用範例(往上移)://testBtn 是被蓋住的物件self.view.bringSubview(toFront: testBtn)往下移以此類推 但是我沒有試驗是移往上一層,還是移到最上層!如果之後又寫東西出來,沒有設定,會疊在最上層。 參考網站:https://developer.apple.com/reference/uikit/uiview/1622541-bringsubview &...

2016年10月27日 星期四

swift3 Day13 UIButton hidden 出現與消失

UIButton 、UILabel 都有一個方法叫 isHidden傳入 true 就會隱藏起來,反之。前兩天講的 UILabel 創造與消失,如果用在 UIButton(用拖拉方式連結),那麼在 testBtn.removeFromSuperview()  移除之後,要做 self.view.addSubview(testBtn) 時會出錯會出現錯誤 THREAD 1: EXC_BAD_instruction(code=exc_i386_invop,subcode=0x0) 所以用 isHidden 的方式控制出現與隱藏就不會出錯//隱藏此按鈕testBtn.isHidden...

2016年10月26日 星期三

swift3 Day12 UILabel.text 型態轉換 int

要實現點擊 UILabel ,UILabel.text 就加 1就要用到型態轉換今天只針對 UILabel.text 加數字 10 testLabel.text = String(Int(testLabel.text!)! + 10)text: String ,所以要相加要先轉成 Int加完要顯示,就要轉回 String ,否則會出  !  error   &nb...

2016年10月25日 星期二

swift3 Day11 設定綁住的 function 使用 UILabel

根據昨天的 addGestureRecognizer 連結 click 今天要在連結的 function 中讀到是哪個 UILabel  func changeText(gestureRecognizer: UIGestureRecognizer) {//看你傳了什麼進來,UILabel 就改成什麼    let searchlbl = (gestureRecognizer.view as! UILabel)     searchlbl.text = "25"} 這樣很多 UILabel 用同一個 function...

2016年10月24日 星期一

swift3 Day10 綁 click 事件在 UILabel

自己在 Controller 創出來的 UILabel 無法用內建拖拉的方式來綁定 click 事件但依然有可以實現的方法 //宣告一個 UILabelvar testLabel = UILabel(frame: CGRect(x: 20, y: 20, width: 80, height: 80))//連結到 changeTextlet gestureRecognizer = UITapGestureRecognizer(target: self,  action: #selector(ViewController.changeText ))//加進要被點下的 UILabeltestLabel.addGestureRecognizer(gestureRecognizer)//一定要設定 isUserInteractionEnabled 為true,default...

2016年10月23日 星期日

swift3 Day9 固定螢幕方向

有些狀況下不希望使用者因為轉動手機就讓畫面換方向,例如利用手機傾斜做感應,就只想畫面有動作而不會換方向固定方向的其中一種做法:在 Xcode 中直接設定 Landscape Left、Landscape Right 打勾是可以轉不打勾則是不能轉!三種 Devices 是分開設定的&nb...

2016年10月22日 星期六

swift3 Day8 UILabel 圓角 circle

這是昨天宣告的 UILabel var testLabel = UILabel(frame: CGRect(x: 0, y: 20, width: 80, height: 80)) 設定圓角會影響的 backgroundColortestLabel.layer.backgroundColor = UIColor.orange.cgColor 圓角的寫法testLabel.layer.cornerRadius = 40數字=UILabel 大小的一半,為正圓形 加上框線//框線顏色預設是黑的testLabel.layer.borderWidth...

2016年10月21日 星期五

swift3 Day7 UILabel backgroundColor

將 UILabel 做更多變化,例如:變成圓形 這是宣告 UILabel var testLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 80, height: 80))backgroundColor設 UILabel 底色的方法有沒有圓角都不影響整個 Label 底色testLabel.backgroundColor = UIColor.green  將底色設為橘色如果有圓角,則是會有圓角的底色testLabel.layer.backgroundColor = UIColor.orange.cgColor 如果兩個 backgroundColor 都有設定,testLabel.backgroundColor > testLabel.layer.backgroundColor也就是整個...

2016年10月20日 星期四

swift3 Day6 UILabel 創造與移除 add remove

要用 UILabel ,在 Controller 中寫,不在 storyboard 中直接拉的話,要記得加self.view.addSubview(testLabel)可以寫在 button 中,就會再按下後才看到 UILabel 消失(對於指定的 UIView 都會消失 ):testLabel.removeFromSuperview() Expected declaration error 把出錯的那行加到 function...

2016年10月19日 星期三

swift3 Day5 UIColor textColor

在 swift2 的 UIColor 用法為:testLabel.textColor = UIColor.redColor() 在 swift3 時,不用加後面的 Color()加了會出 error,直接使用即可,例如要紅色:testLabel.textColor = UIColor.red內建顏色有:當然也可以自己設定顏色://alpha是透明度,數字越小越透明testLabel.textColor = UIColor(red: 1, green: 0, blue: 0, alpha: 1)黑白://white越大越白,越小越黑testLabel.textColor =...

2016年10月18日 星期二

swift3 Day4 方塊移動動畫-2

把第三天的方塊改成可以順時針以及逆時針旋轉,我先宣告一個變數記錄現在位置,用以推斷下個要走的位置//起始為0var loc=0先加入兩個按紐,    @IBAction func clockwise(_ sender: UIButton) {   }    @IBAction func counterclockwise(_ sender: UIButton) {    } 現在執行完畢之後畫面:接下來填寫內容,//順時針按鈕@IBAction func clockwise(_ sender: UIButton)...

2016年10月17日 星期一

swift3 Day3 方塊移動動畫

看了幾天官方文件,實作簡單的動畫,比較有趣跟成就感拉好畫面之後,也要把該有的元件連到controller畫面: 程式碼:   @IBOutlet weak var one: UILabel!    @IBOutlet weak var two: UILabel!    @IBOutlet weak var three: UILabel!    @IBOutlet weak var four: UILabel!        override func viewDidLoad()...

2016年10月16日 星期日

swift3 Day2 Enumerations 列舉

Enumerations and Structuresenum 列舉enum Rank: Int {    case ace = 1    case two, three, four, five, six, seven, eight, nine, ten//會自己給rawValue two = 2, three = 3, ...//Use the rawValue property to access the raw value of an enumeration case.     case jack, queen, king    func simpleDescription() -> String {        switch self...

swift3 Day1 Welcome to Swift-A Swift Tour

Simple Values陣列、字典 給予空值https://goo.gl/BYx9nl 宣告變數https://goo.gl/YMzMFbControl Flow??A = B ?? C    B!=nil , A=B;B=nil , A=C跟三元運算式很像,會變成 A = (B!=nil) ? B : Clet nickName: String? = nillet fullName: String = "John Appleseed"let informalGreeting = "Hi \(nickName ?? fullName)" for...

2016年10月1日 星期六

swift3 switch 不一定要加 break

swift 3 的 switch 比較特別的地方在於:case 跑完時會自己 break 不需要自己再加,不過多加了也不會出錯 swift 寫法 java 寫法 加上 fallthrough 就會像 Java 一樣,詳情:swift3 Day23 switch 繼續執行下一個 c...

swift3 參考網站

Apple 官方文件apple developer Guides and Sample Code:https://developer.apple.com/library/content/navigation/ The Swift Programming Language (Swift 3)官方文件:https://goo.gl/JBi2gy Start Developing iOS Apps (Swift) 快速開始:https://goo.gl/lmC59J iOS 9 上課影片(網路上有人分享字幕):https://itunes.apple.com/us/course/developing-ios-9-apps-swift/id1104579961 翻譯官方文件繁體中文翻譯版(swift 3):https://itisjoe.gitbooks.io/swiftgo/content/ 簡體中文(swift...

swift3 陣列、字典給予空值

創建一個空陣列或者字典,使用初始化語法。To create an empty array or dictionary, use the initializer syntax.let emptyArray = [String]()let emptyDictionary = [String: Float]() 如果型態資訊可以被推斷出來,你可以用[]和[:]來創建空陣列和空字典——就像你宣告變數或者給函式傳參數的時候一樣。If type information can be inferred, you can write an empty array as `[]` and an empty dictionary as `[:]`—for example, when you set a new value for a variable or pass an argument to a...

swift3 宣告變數

swift 3.0 要宣告變數有兩個方式:var 跟 letvar 是之後值會變(變數)let 則是常數如果使用 var ,但是值都沒有改變的話會有黃色三角形提示:值都沒改變,要不要用 let 的訊息可以點提示會自動幫你把 var 換成 let 根據 swift 2 教學影片,教授說交出去的作業不能有黃色...