@IBOutlet weak var btnStart: UIButton! @IBOutlet weak var progress: UIProgressView! @IBOutlet weak var labelMsg: UILabel! //計時器 var timer: Timer? //計數 var count : Int = 0 override func viewDidLoad() { super.viewDidLoad() progress.frame.size.width = 250 //可以自訂progress顏色 progress.progressTintColor = UIColor.red progress.trackTintColor = UIColor.darkGray progress.progress = 0 } @IBAction func donloadClick(_ sender: UIButton) { //按鈕失效 避免重複點擊 btnStart.isEnabled = false count = 0 //參數順序亂掉會報錯 //The runTimedCode selector means that the timer will call a method named runTimedCode() every 0.5 seconds until the timer is terminated timer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector:#selector(runTimedCode), userInfo: nil, repeats: true) } func runTimedCode() { progress.progress = Float(count) / 100 labelMsg.text = "process: \(count)%" count += 1 if count > 100 { timer!.invalidate() // stop timer = nil btnStart.isEnabled = true } }參考資料:hackingwithswift.com
2016年12月26日 星期一
[IOS] Progress View
2016年12月15日 星期四
[IOS] ImageView 翻圖片
裡面有用到兩種不同Array寫法來存圖片
class ViewController: UIViewController { @IBOutlet weak var btnPre: UIButton! @IBOutlet weak var btnNext: UIButton! @IBOutlet weak var labelName: UILabel! @IBOutlet weak var image: UIImageView! //兩種array方法 var arrayImage = ["水上威尼斯","史特拉斯堡-2","科瑪", "新天鵝堡_繽紛","春露"] var pic: [UIImage] = [ UIImage(named: "水上威尼斯")!, UIImage(named: "史特拉斯堡-2")!, UIImage(named: "科瑪")!, UIImage(named: "新天鵝堡_繽紛")!, UIImage(named: "春露")! ] var current:Int = 0 var count:Int = 0 override func viewDidLoad() { super.viewDidLoad() count = arrayImage.count //image.image = UIImage(named: "水上威尼斯") image.image = pic[0] labelName.text = arrayImage[0] } @IBAction func preClick(_ sender: UIButton) { current -= 1 if current < 0 { current = count - 1 } //image.image = UIImage(named:String(arrayImage[current])) image.image = pic[current] labelName.text = arrayImage[current] } @IBAction func nextClick(_ sender: UIButton) { current += 1 if current == count { current = 0 } //image.image = UIImage(named:String(arrayImage[current])) image.image = pic[current] labelName.text = arrayImage[current] } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
[IOS] 自訂生成按鈕(CustomButton)
範例是在書上看到的,不過程式碼我有改過,因為Swift 3 出來了,而市面書上的範例程式碼... 你懂的
這篇主要是講後端生成程式碼 以Button為例
yourButtonName.addTarget(self, action:#selector(functionName(sender:)), for: .touchUpInside) fun functionName(sender:UIButton) { //.... }
action 這邊 如果沒有回傳值 (sender:) 就不用加了
View 的截圖 自訂12個按鈕
Controller全部程式碼
class ViewController: UIViewController { @IBOutlet weak var labelTel: UITextField! @IBOutlet weak var labelMsg: UILabel! override func viewDidLoad() { super.viewDidLoad() for i in 0...11 { let x:Int = 100 + (i % 4) * 60 let y:Int = 140 + (i / 4) * 60 let buttonNumber:UIButton = UIButton(type: UIButtonType.system) as UIButton //按鈕位置 大小 buttonNumber.frame = CGRect(x: x,y: y,width: 40, height: 35) //文字顏色 buttonNumber.setTitleColor(UIColor.white, for: UIControlState.normal) //按鈕背景 buttonNumber.backgroundColor = UIColor.black //字型大小 buttonNumber.titleLabel?.font = UIFont(name: "System", size: 22.0) if i == 10 { buttonNumber.setTitle("X", for: UIControlState.normal) //加入事件 #selector(funcName(sender:)) 函數是有參數的 buttonNumber.addTarget(self, action:#selector(clearClick(sender:)), for: .touchUpInside) } else if i == 11 { buttonNumber.setTitle("OK", for: UIControlState.normal) buttonNumber.addTarget(self, action:#selector(sureClick(sender:)), for: UIControlEvents.touchUpInside) }else { buttonNumber.setTitle("\(i)", for: UIControlState.normal) buttonNumber.addTarget(self, action:#selector(numberClick(sender:)), for: UIControlEvents.touchUpInside) } //加入按鈕 view.addSubview(buttonNumber) } } func sureClick(sender:UIButton) { if labelTel.text?.lengthOfBytes(using: String.Encoding.utf8) == 10 { labelMsg.text = "Call " + labelTel.text! } else if labelTel.text == "" { labelMsg.text = "Please enter your phone number." } else if (labelTel.text?.lengthOfBytes(using: String.Encoding.utf8))! > 10{ labelMsg.text = "Error" } } func clearClick(sender:UIButton){ labelTel.text = "" labelMsg.text = "" } func numberClick(sender:UIButton) { labelTel.text = labelTel.text! + sender.currentTitle! } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
訂閱:
文章 (Atom)