2017-01-13 28 views
1

當我點擊一個文本字段時,我的當前應用程序會運行,它會調出UIPickerView,但是如果我自己點擊圖像(手勢放在圖像上)怎麼辦?在手勢上顯示UIPickerView Tapped

class SomeVC: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate { 
    @IBOutlet weak var inputLabel: UITextField! 
    @IBOutlet weak var laImageGestureTapped: UITapGestureRecognizer! 

    let demoPicker = UIPickerView() 

    // viewDidLoad() 
    inputLabel.inputView = demoPicker // All is well with this. 

    // How to open the UIPickerView with laImageGestureTapped? 

    // I have omitted the required functions for: numberOfRowsInComponent, viewForRow, didSelectRow, numberOfComponents etc 
} 

我是searching with the correct words

我想要的只是當圖像被點擊時顯示選擇器。我不擔心didSelectRow,因爲會有一個隱藏的標籤來做x,y和z。

如果這個問題已經被詢問和回答,請指導我。謝謝。

+0

而不是有一個gestureRecognizer附加到UIImageView,爲什麼不只是有一個圖像的按鈕? – toddg

+0

@託德一個放置在圖像上的按鈕?這可以工作,但我沒有這樣做過。讓我看看... – Sylar

+0

是的,這將工作。或者你可以使用一個按鈕並設置按鈕圖像。 – toddg

回答

1

下面是一個可供選擇的方法,以把一個明確文本框在你的圖像:

  1. 創建具有分配給它的背景圖像屬性
  2. 圖像初始化與其幀您pickerView關閉屏幕上的按鈕
  3. 爲您的按鈕IBAction爲調用pickerView到屏幕上,並創建一個輕敲手勢,並補充說,你的看法
  4. 創建發射時點擊手勢將調用該方法,將發送您的pickerView背過的屏幕

下面是相關代碼:

class VC: UIViewController { 
    var pickerView = UIPickerView() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    ... 
     pickerView = UIPickerView(frame: CGRect(x: 0, y: self.view.bounds.height, width: self.view.bounds.width, height: 100)) //Step 2 

    } 

    @IBAction func buttonPressed(sender: UIButton){ //Step 3 

     UIView.animate(withDuration: 0.3, animations: { 
      self.pickerView.frame = CGRect(x: 0, y: self.view.bounds.size.height - self.pickerView.bounds.size.height, width: self.pickerView.bounds.size.width, height: self.pickerView.bounds.size.height) 
    }) 
     let tapGesture = UITapGestureRecognizer(target: self, action: #selector(doneWithPickerView)) 
     view.addGestureRecognizer(tapGesture) 

    } 

    func doneWithPickerView() { //Step 4 

     UIView.animate(withDuration: 0.3, animations: { 
      self.pickerView.frame = CGRect(x: 0, y: self.view.bounds.size.height, width: self.pickerView.bounds.size.width, height: self.pickerView.bounds.size.height) 
     }) 
    } 
} 

我認爲這是一般更好的做法是不要使用隱形的意見,因爲他們能爲你以後造成麻煩。希望這可以幫助。

+0

這也可以工作。謝謝。 – Sylar

+0

@Sylar沒問題:) –