Skywalker
Skywalker

Reputation: 5194

animating a UIPickerView ( slide up )

I am trying to implement a UIPickerView which is animated. What I mean by animated is that when the view first loads up no picker view is visible. The screen shows buttons. On tapping the button the UIPickerView slides up from the bottom.

Please note that there will be multiple picker views on the same view.The itself would contain 10 buttons. On tapping each button a picker view ill slide up with a set of options. Each button will have picker view with different kinds of options.

What I have tried so far:

import UIKit

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

@IBOutlet var picker: UIPickerView!

var pickerData = [String]()

override func viewDidLoad() {
    super.viewDidLoad()

    // Connect data:
    self.picker.delegate = self
    self.picker.dataSource = self

    pickerData = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6"]

    self.picker.hidden = true
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// The number of columns of data
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}

// The number of rows of data
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerData.count
}

// The data to return for the row and component (column) that's being passed in
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return pickerData[row]
}

@IBAction func showPicker(sender: AnyObject) {

    self.picker.hidden = false

   }

 }

I implemented a single UIPickerView and used the hidden command to hide the picker view on viewDidLoad. And on tapping of the button I set the picker.hidden = false. This made the picker view visible again.

My concern & question:

My concern is that whether using the hidden method is the proper way to hide and show multiple picker views on button tap?

My question is whats the proper way of doing that and how can I animate it as I explained above.

Thanks in advance.

Upvotes: 1

Views: 2588

Answers (1)

Islam
Islam

Reputation: 3733

I would suggest using autolayout and adding the pickers at the bottom of the screen with its y starting from the bottom of the screen. then when a button is pressed I would set it's y to

yConstraint.constant = self.view.frame.size.height-picker.frame.size.height

then create animation and add self.view.layoutIfNeeded() (to animate the constraint changes)

Upvotes: 1

Related Questions