簡單做了一個swift 使用pickerView功能的範例

必須具備知識

1. storyboard 跟程式碼的連結

2. xib的簡單應用 並實例化

3. 如何使用tableView

4. 如何替button 增加自定義事件 

效果圖如下: 

pickerViewSample123  

程式碼內容如下:

import UIKit
/**
*這是主要的控制程式碼
*
*/
class SampleSelectViewController: UITableViewController  , UIPickerViewDelegate  {
    var showUIPIckerView:ShowUIPickerView!//彈出的選單
    var maskView : UIView!//黑屏
    var testPickerSources = ["White", "Red", "Green", "Blue"];//假的pickerlist
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.initView()
        /*self.view.addSubview(self.showUIPIckerView)
        self.view.addSubview(self.maskView)*/
    }
    /**
    *初始化 必要的pickerView and maskView(背景黑屏)
    *
    */
    func initView()
    {
        var views : NSArray = UINib(nibName: "ShowUIPickerView", bundle: nil).instantiateWithOwner(self, options: nil)
        //從xib建立一個views
        self.showUIPIckerView = views.objectAtIndex(0) as! ShowUIPickerView
        //一個xib通常會有多個view 不過這麼範例只有一個view 所以取0 並as 我們自己建立的class ShowUIPickerView
        self.showUIPIckerView.myPickerView.delegate = self
        self.maskView = UIView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
        //new 一個新的UIView 等於螢幕寬高
        self.maskView.backgroundColor = UIColor.blackColor()
        //設定這個view為全黑
        self.maskView.alpha = 0;
        //設定為全透明
        let select : Selector = "hideMypicker:"
        //設定一個action事件
        self.showUIPIckerView.OK.addTarget(self, action: select, forControlEvents: UIControlEvents.TouchUpInside)
        //替pickerView上面的 一個按鈕  設定一個回應事件
        let gesture : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: select)
        self.maskView.addGestureRecognizer(gesture)
        //替這個view增加一個回應事件
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    /**
    *設定picker要顯示幾欄
    *
    */
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }
    /**
    *設定picker每一欄要顯示的行數
    *
    */
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return testPickerSources.count;
    }
    /**
    *設定picker每一欄的內容
    *
    */
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        return testPickerSources[row]
    }

    // MARK: - Table view data source
    /**
    *設定要顯示的tableView list 數量
    *
    */
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete method implementation.
        // Return the number of rows in the section.
        return 1
    }

    /**
    *tableView 的內容
    *
    */
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        
        var cell :UITableViewCell = UITableViewCell()
        cell.textLabel?.text = "aaaaaa"
        return cell
    }
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        self.showPickerView()
    }
    
    /**
    *顯示pickerView
    *
    */
    func showPickerView()
    {
        self.view.addSubview(self.maskView)
        self.view.addSubview(self.showUIPIckerView)
        //加入view請注意順序 最後加的  在最上層
        self.tableView?.scrollEnabled = false
        //禁止tableView滑動
        self.maskView.alpha = 0;
        //設定黑屏的初始透明度
        self.showUIPIckerView.frame.origin.y = self.view.frame.height
        //設定pickerView的初始位置
        self.showUIPIckerView.bounds  = CGRectMake(0, self.showUIPIckerView.bounds.origin.y, UIScreen.mainScreen().bounds.width, self.showUIPIckerView.bounds.height)
        self.showUIPIckerView.frame.origin.x = 0
        //設定pickerView與螢幕等寬
        UIView.animateWithDuration(0.3){//view移動動畫
            self.maskView.alpha = 0.3
            println(self.showUIPIckerView.frame)
            self.showUIPIckerView.frame.origin.y = self.view.frame.height-self.showUIPIckerView.frame.height
            println(self.showUIPIckerView.frame)
        }
    }
    /**
    *按鈕及背景的事件
    *
    */
    @IBAction func hideMypicker(sender:AnyObject)
    {
        self.hideMypicker()
    }
    /**
    *關閉pickerView
    *
    */
    func hideMypicker()
    {
        UIView.animateWithDuration(0.3,
        animations:
        {
            self.maskView.alpha = 0;
            self.showUIPIckerView.frame.origin.y = self.view.frame.height
        },
        completion:
        {
            (value:Bool) in
            
            self.maskView.removeFromSuperview()
            self.showUIPIckerView.removeFromSuperview()
            //移除view
            self.tableView?.scrollEnabled = true
            //設定tableView可以滑動
        })
    }
}

 

詳細專案:可以到git下載

https://github.com/cwa1022/PickerSample.git

查看原文 >>
相關文章