UIPickerView did pass by row - ios

I have this demo app:
As you can see, the alpha of the background is changing to black according to the value.
But the problem is that there is no smooth transition:
As you can see from the GIF, the background is only changing after the scrolling is over. And I don't want it to be like that.
this is my code:
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet weak var pickerView: UIPickerView!
#IBOutlet weak var backView: UIView!
let max = 100
override func viewDidLoad() {
super.viewDidLoad()
pickerView.delegate = self
pickerView.dataSource = self
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return max + 1 // To include '0'
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let l = UILabel(frame: .zero)
l.text = String(max - row)
l.textColor = .white
l.font = UIFont.preferredFont(forTextStyle: .title3)
l.textAlignment = .center
return l
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
backView.alpha = CGFloat(max - row) / 100
}
}
I am giving the delegate UIView instead of String, because I had an idea: to test every time the location of each row is changing, and when the row is in the middle of the screen I should update the background. But unfortunately I don't know how to do that.
Maybe you can help? or perhaps suggest any other ideas?
Thank you!

The delegate method didSelectRow is only called when the rolling stops, so this is not the place you should update your alpha. UIPickerView has no delegate method which will notify you about the change during the rolling, however the UIPickerView will call your data source and delegate methods to get the title or in your case the view for a given row so it can be displayed as the user scrolls. So what you should do is just move your alpa changing logic there:
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
backView.alpha = CGFloat(max - row) / 100
}
Note that this delegate method will be called when the UIPickerView is loaded, so maybe you should disable the alpha changing until view is not layout out correctly(maybe viewDidAppear will do it).
As the delegate method can sometimes behave unexpectedly(calling not just the next lines, but any line from the picker), we should store and also check if the row is just one step ahead or behind the last saved value, otherwise we should just ignore that. I made a simple demo to demonstrate how it works:
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
private let pickerView = UIPickerView()
private var isPickerReady = false
private var lastValue = 0
private let max = 100
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(pickerView)
pickerView.translatesAutoresizingMaskIntoConstraints = false
pickerView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
pickerView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
pickerView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
pickerView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
pickerView.delegate = self
pickerView.dataSource = self
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
isPickerReady = true
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return max
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
// Do not update the value until the view is not loaded
// Only consider delegate methods which are one step ahead or behind the last value
if row + 1 == lastValue && isPickerReady || row - 1 == lastValue && isPickerReady {
lastValue = row
view.alpha = CGFloat(max - lastValue ) / 100
}
return "Tiltle \(row)"
}
}

Related

Picker view overwrites the value of blur effect in swift 4.2

I want to increase or decrease the level of blur effect in the photo using the pickerview. Even if I decrease the value, it adds a continuous blur effect to it. What should I do?
Working on latest Swift and Xcode
class ViewController: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource {
#IBOutlet var bg: UIImageView!
#IBOutlet var pickView: UIPickerView!
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return arr.count
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
switch arr[row] {
case "1":
Blur(blur: 0.3)
case "2":
Blur(blur: 0.6)
case "3":
Blur(blur: 0.0)
default:
print("not change")
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return arr[row]
}
let arr = ["1","2","3"]
override func viewDidLoad() {
super.viewDidLoad()
pickView.dataSource = self
pickView.delegate = self
// Do any additional setup after loading the view, typically from a nib.
}
func Blur(blur:CGFloat)
{
let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.light)
let blurView = UIVisualEffectView(effect: blurEffect)
blurView.frame = bg.bounds
blurView.alpha = blur
self.view.addSubview(blurView)
}
}
Your Blur(blur: 0.3) is a function not an object class, it will run not change properties. And your func added a Subview to your parent View but none code line to remove it before add another. So it will be stack up.
Remove old blur subView then add new.
Create a default blur view first then change it properties.

UIPICKERVIEW with different data to be displayed according to the row selected

I have attached my code for your reference. I can able to display the data in Pickerview as i change the row the data changes accordingly but the data which selected from the picker is not getting displayed in the UITextfield. if I select any option in Pickerview I could not see any text inside the uitextfield if I select it still showing blank text field.
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {
#IBOutlet weak var textfield1: UITextField!
#IBOutlet weak var textfield2: UITextField!
var x = 0
let leaguepickerview_destination = UIPickerView()
let clubpickerview_denomination = UIPickerView()
let league_destination = ["India","Europe","Australia","New Zealand","USA & Rest of the World"]
let club_denomination = [[" Rs.500","Rs.1000","Rs.1500","Rs.2000","Rs.2500","Rs.3000","Rs.3500","Rs.4000","Rs.4500","Rs.5000"],["100","150","200","250","300","350","400","450","500"],["100","150","200","250","300","350","400","450","500"],["100","150","200","250","300","350","400","450","500"],["100","150","200","250","300","350","400","450","500"]]
override func viewDidLoad() {
super.viewDidLoad()
leaguepickerview_destination.delegate = self
leaguepickerview_destination.dataSource = self
clubpickerview_denomination.delegate = self
clubpickerview_denomination.dataSource = self
textfield1.inputView = leaguepickerview_destination
textfield2.inputView = clubpickerview_denomination
textfield1.delegate = self
textfield2.delegate = self
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
switch (pickerView){
case leaguepickerview_destination:
return league_destination.count
case clubpickerview_denomination:
return club_denomination[x].count
default:
return 0
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
switch (pickerView){
case leaguepickerview_destination:
return league_destination[row]
case clubpickerview_denomination:
return club_denomination[x][row]
default:
return "an error occurred"
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if (pickerView == leaguepickerview_destination) {
x = row
clubpickerview_denomination.reloadAllComponents()
}
}
}
You need to set the textfield text
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if (pickerView == leaguepickerview_destination) {
x = row
clubpickerview_denomination.reloadAllComponents()
textfield1.text = league_destination[row]
}
else {
textfield2.text = club_denomination[x][row]
}
}

textFieldDidEndEditing not firing when pickerviiew used in swift3

The text field delegate method textFieldDidEndEditing is not firing when picker view is used in Swift 3. How can I call it? There is no button to dynamically call the delegate.
You can see my code below.
import UIKit
class ThroewViewController: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource,UITextFieldDelegate {
#IBOutlet weak var pickerText: UITextField!
let thePicker = UIPickerView()
var dashTitle_Arr = ["One","Two","Three","Four","Five","Six"]
override func viewDidLoad() {
super.viewDidLoad()
pickerText.delegate = self
thePicker.dataSource = self
thePicker.delegate = self
pickerText.inputView = thePicker
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return dashTitle_Arr .count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return dashTitle_Arr [row] }
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
pickerText.text = dashTitle_Arr [row]
}
func textFieldDidEndEditing(_ textField: UITextField) {
addIntoArray(text: textField.text)
}
func addIntoArray(text: String?) {
guard let text = text, text.characters.count > 0 else {
return
}
dashTitle_Arr.append(text)
thePicker.reloadAllComponents()
}
}
There is a property of picker-view is accessoriesView. Create a tool bar with done or cancel button and set it in the picker view's assessoriesView.
In the toolbar button action write the textfield.resignFirstResponder()
After that the textFieldDidEndEditing delegate method fired
It works fine for me. Hope it will helps you. Thank you

add UIPickerView as subview, very weired

recently I need a solution to show a present-like pickerView for choosing data for some property, like profile.
what I did is
1, create a empty project
2, create a AViewController which will be used as subview. And I add a pickerView and a navigation bar into that view and adjust the view's height to around 260.
3, write code in that AViewController's class.
class APickerViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
#IBOutlet weak var pickerView:UIPickerView!
let dataList = [["a","b","c","d"]]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//pickerView.dataSource = self
//pickerView.delegate = self
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return dataList.count
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return dataList[component].count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return dataList[component][row]
}
4, write code in Main ViewController to add the AViewController's View as subview.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let pickerViewController = storyboard?.instantiateViewController(withIdentifier: String(APickerViewController.self)) as! APickerViewController
self.view.addSubview(pickerViewController.view)
self.view.bringSubview(toFront: pickerViewController.view)
pickerViewController.view.frame = CGRect(x: 0, y: self.view.frame.height - 300, width: self.view.frame.width, height: 300)
}
and also one question here actually I'm not that sure the difference.
A:
let pickerViewController = storyboard?.instantiateViewController(withIdentifier: String(APickerViewController.self)) as! APickerViewController
B:
let pickerViewController = APickerViewController()
What's the different of A and B ? If I do some #IBOutlet settings by Interface Builder will be affected or what ?
5, go and ... get weired ...
the function
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return dataList[component].count
}
sometime's get called many times and sometimes not get called.
and the data was not show in pickerView and sometimes only show row 1,2 and no others and if I flick up or down those 1,2 row data disappeared.
if anyone have a better solution of using pickerView as option popup window ?
Help.
Thanks
class EditViewController: UIViewController,UITextFieldDelegate,UIPickerViewDelegate{
var picker : UIPickerView = UIPickerView()
var toolBar : UIToolbar = UIToolbar()
#IBOutlet var txtFieldState: UITextField!
var stateArray = ["Andaman & Nicobar Island","Andhra Pradesh","Arunachal Pradesh","Assam","Bihar","Chandigarh","Chhattisgarh","Dadra & Nagar"]
override func viewDidLoad() {
super.viewDidLoad()
txtFieldState.delegate = self
}
func textFieldDidBeginEditing(textField: UITextField) {
if textField == txtFieldState{
txtFieldState.inputView = picker
txtFieldState.inputAccessoryView = toolBar
self.configurePicker()
}
}
func configurePicker()
{
picker.alpha = 1.0
picker.backgroundColor = UIColor(red:241/255.0, green:241/255.0, blue:241/255.0, alpha: 1.0)
picker.showsSelectionIndicator = true
picker.delegate = self
picker.selectedRowInComponent(0)
let screenRect = self.view.frame
let pickerSize = picker.sizeThatFits(CGSizeZero)
let x = screenRect.origin.x + (screenRect.size.width / 2) - (pickerSize.width / 2)
let pickerRect = CGRectMake(x,
screenRect.origin.y + (screenRect.size.height) - (pickerSize.height),
pickerSize.width,
pickerSize.height)
picker.frame = pickerRect
let toolbarSize = toolBar.sizeThatFits(CGSizeZero)
toolBar.frame = CGRectMake(x,pickerRect.origin.y, // right under the picker
pickerSize.width, // make them the same width
toolbarSize.height)
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
dropState = row
txtFieldState.text = "\(stateArray[row])"
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
return stateArray[row]
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
return stateArray.count
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
{
return 1
}
}

UIPickerView showing empty grey box Swift

I haven't a whole lot of code, so I might as well copy it here.
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource{
var buildings = ["BankBuilding", "Cinema" , "CornerShop", "Greg's House"]
#IBOutlet weak var buildText: UITextField!
var buildPickers:UIPickerView = UIPickerView()
override func viewDidLoad() {
super.viewDidLoad()
buildPickers = UIPickerView()
buildPickers.delegate = self
buildPickers.hidden = true;
buildText.inputView = buildPickers
buildText.text = buildings[0]
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
println("Count: \(buildings.count)")
return buildings.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
println("creating title: \(buildings[row])")
return buildings[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
println("Selected: \(buildings[row])")
buildText.text = buildings[row]
buildPickers.hidden = true;
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
buildPickers.hidden = false
return false
}
}
The print statements are correct. For numberOfRows... and titleForRow it is printing the correct Strings.
But there is no prints for didSelectRow because, well, I can't select a row.
This is what I get:
You can ignore the Google Map in the background, that shouldn't interfere with the Picker View and is just set up in the StoryBoard.
The Grey window appears when I click on the textField but never shows any content. But the print statements say otherwise.
Does anyone know why this is the case?
Just add this line in your viewDidLoad method:
buildPickers.dataSource = self
And your code will be:
override func viewDidLoad() {
super.viewDidLoad()
buildPickers = UIPickerView()
buildPickers.delegate = self
buildPickers.dataSource = self
buildPickers.hidden = true;
buildText.inputView = buildPickers
buildText.text = buildings[0]
}
And it will show your data.
UPDATE:
It is not showing because you set it hidden in your viewDidLoad.
Just remove this line from your code:
buildPickers.hidden = true
Here is your working code:
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate{
var buildings = ["BankBuilding", "Cinema" , "CornerShop", "Greg's House"]
#IBOutlet weak var buildText: UITextField!
var buildPickers:UIPickerView = UIPickerView()
override func viewDidLoad() {
super.viewDidLoad()
buildPickers = UIPickerView()
buildPickers.delegate = self
buildPickers.hidden = true
buildText.delegate = self //set delegate for textField
buildText.inputView = buildPickers
buildText.text = buildings[0]
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
println("Count: \(buildings.count)")
return buildings.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
println("creating title: \(buildings[row])")
return buildings[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
println("Selected: \(buildings[row])")
buildText.text = buildings[row]
buildPickers.hidden = true;
}
//this method will call when you click on textField
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
buildPickers.hidden = false
return true
}
}
Is the delegate set on the text field? Are you getting the callback which you use to un-hide the picker?

Resources