2016年12月26日 星期一

[IOS] Progress View



  1. @IBOutlet weak var btnStart: UIButton!
  2. @IBOutlet weak var progress: UIProgressView!
  3. @IBOutlet weak var labelMsg: UILabel!
  4. //計時器
  5. var timer: Timer?
  6. //計數
  7. var count : Int = 0
  8. override func viewDidLoad() {
  9. super.viewDidLoad()
  10. progress.frame.size.width = 250
  11. //可以自訂progress顏色
  12. progress.progressTintColor = UIColor.red
  13. progress.trackTintColor = UIColor.darkGray
  14. progress.progress = 0
  15. }
  16.  
  17. @IBAction func donloadClick(_ sender: UIButton) {
  18. //按鈕失效 避免重複點擊
  19. btnStart.isEnabled = false
  20. count = 0
  21. //參數順序亂掉會報錯
  22. //The runTimedCode selector means that the timer will call a method named runTimedCode() every 0.5 seconds until the timer is terminated
  23. timer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector:#selector(runTimedCode), userInfo: nil, repeats: true)
  24. }
  25. func runTimedCode() {
  26. progress.progress = Float(count) / 100
  27. labelMsg.text = "process: \(count)%"
  28. count += 1
  29. if count > 100 {
  30. timer!.invalidate() // stop
  31. timer = nil
  32. btnStart.isEnabled = true
  33. }
  34. }
  35.  
參考資料:hackingwithswift.com

2016年12月15日 星期四

[IOS] ImageView 翻圖片


裡面有用到兩種不同Array寫法來存圖片





  1. class ViewController: UIViewController {
  2. @IBOutlet weak var btnPre: UIButton!
  3. @IBOutlet weak var btnNext: UIButton!
  4. @IBOutlet weak var labelName: UILabel!
  5. @IBOutlet weak var image: UIImageView!
  6. //兩種array方法
  7. var arrayImage = ["水上威尼斯","史特拉斯堡-2","科瑪", "新天鵝堡_繽紛","春露"]
  8. var pic: [UIImage] = [
  9. UIImage(named: "水上威尼斯")!,
  10. UIImage(named: "史特拉斯堡-2")!,
  11. UIImage(named: "科瑪")!,
  12. UIImage(named: "新天鵝堡_繽紛")!,
  13. UIImage(named: "春露")!
  14. ]
  15. var current:Int = 0
  16. var count:Int = 0
  17. override func viewDidLoad() {
  18. super.viewDidLoad()
  19. count = arrayImage.count
  20. //image.image = UIImage(named: "水上威尼斯")
  21. image.image = pic[0]
  22. labelName.text = arrayImage[0]
  23. }
  24.  
  25. @IBAction func preClick(_ sender: UIButton) {
  26. current -= 1
  27. if current < 0 {
  28. current = count - 1
  29. }
  30. //image.image = UIImage(named:String(arrayImage[current]))
  31. image.image = pic[current]
  32. labelName.text = arrayImage[current]
  33. }
  34. @IBAction func nextClick(_ sender: UIButton) {
  35. current += 1
  36. if current == count {
  37. current = 0
  38. }
  39. //image.image = UIImage(named:String(arrayImage[current]))
  40. image.image = pic[current]
  41.  
  42. labelName.text = arrayImage[current]
  43.  
  44. }
  45. override func didReceiveMemoryWarning() {
  46. super.didReceiveMemoryWarning()
  47. }
  48. }










[IOS] 自訂生成按鈕(CustomButton)


範例是在書上看到的,不過程式碼我有改過,因為Swift 3 出來了,而市面書上的範例程式碼... 你懂的

這篇主要是講後端生成程式碼 以Button為例

  1. yourButtonName.addTarget(self, action:#selector(functionName(sender:)), for: .touchUpInside)
  2. fun functionName(sender:UIButton) {
  3. //....
  4. }

action 這邊 如果沒有回傳值 (sender:) 就不用加了


View 的截圖 自訂12個按鈕


Controller全部程式碼

  1. class ViewController: UIViewController {
  2.  
  3. @IBOutlet weak var labelTel: UITextField!
  4. @IBOutlet weak var labelMsg: UILabel!
  5. override func viewDidLoad() {
  6. super.viewDidLoad()
  7. for i in 0...11 {
  8. let x:Int = 100 + (i % 4) * 60
  9. let y:Int = 140 + (i / 4) * 60
  10. let buttonNumber:UIButton = UIButton(type: UIButtonType.system) as UIButton
  11. //按鈕位置 大小
  12. buttonNumber.frame = CGRect(x: x,y: y,width: 40, height: 35)
  13. //文字顏色
  14. buttonNumber.setTitleColor(UIColor.white, for: UIControlState.normal)
  15. //按鈕背景
  16. buttonNumber.backgroundColor = UIColor.black
  17. //字型大小
  18. buttonNumber.titleLabel?.font = UIFont(name: "System", size: 22.0)
  19. if i == 10 {
  20. buttonNumber.setTitle("X", for: UIControlState.normal)
  21. //加入事件 #selector(funcName(sender:)) 函數是有參數的
  22. buttonNumber.addTarget(self, action:#selector(clearClick(sender:)), for: .touchUpInside)
  23. } else if i == 11 {
  24. buttonNumber.setTitle("OK", for: UIControlState.normal)
  25. buttonNumber.addTarget(self, action:#selector(sureClick(sender:)), for: UIControlEvents.touchUpInside)
  26. }else {
  27. buttonNumber.setTitle("\(i)", for: UIControlState.normal)
  28. buttonNumber.addTarget(self, action:#selector(numberClick(sender:)), for: UIControlEvents.touchUpInside)
  29. }
  30. //加入按鈕
  31. view.addSubview(buttonNumber)
  32. }
  33. }
  34.  
  35. func sureClick(sender:UIButton) {
  36. if labelTel.text?.lengthOfBytes(using: String.Encoding.utf8) == 10 {
  37. labelMsg.text = "Call " + labelTel.text!
  38. } else if labelTel.text == "" {
  39. labelMsg.text = "Please enter your phone number."
  40. } else if (labelTel.text?.lengthOfBytes(using: String.Encoding.utf8))! > 10{
  41. labelMsg.text = "Error"
  42. }
  43. }
  44. func clearClick(sender:UIButton){
  45. labelTel.text = ""
  46. labelMsg.text = ""
  47. }
  48. func numberClick(sender:UIButton) {
  49. labelTel.text = labelTel.text! + sender.currentTitle!
  50. }
  51. override func didReceiveMemoryWarning() {
  52. super.didReceiveMemoryWarning()
  53. // Dispose of any resources that can be recreated.
  54. }
  55.  
  56.  
  57. }