- Create a fruity slot game using
UIPickerView
,UIPickerViewDelegate
, andUIPickerViewDataSource
In this lab you will use UIPickerView
to make a slot game called, 'Pickin Fruit'. Each component in our picker view will represent a reel of fruit within our slot machine. UIPickerView
is similar to a UITableView
in that it requires adopting a delegate and a data source. These adoptions are necessary to provide the picker view with the data it needs to construct itself. Check out Apple's documentation about UIPickerView, UIPickerViewDelegate, and UIPickerViewDataSource. Your storyboard is already set up with a picker view, a results label, and a spin button. The outlets for the label and the button are already included in your ViewController
class, along with an IBAction for the button. Your ViewController
class has an instance property array that contains the fruit (data).
- Control drag to create an outlet property for your picker view called
fruitPicker
(Be sure to uncomment the accessibility label declaration forfruitPicker
inviewDidLoad()
). - Implement the 2 necessary data source delegate methods to provide data to the picker view. The picker view should have 3 components.
- Implement the
pickerView(_:titleForRow:forComponent:)
to set each fruit string from the array to the row title.
- Inside the
spinButton
IBAction method, useselectRow(_:inComponent:animated:)
andselectedRow(inComponent:)
to spin the picker and process the outcome. - Update the text value on
resultLabel
with "TRY AGAIN" or "WINNER!" depending on the outcome of the spin. - Run the test.
- It's not possible to know the duration of the picker view animation because Apple keeps the functionality private. However, it's approximately 0.3 seconds. Play around with the the label's alpha, along with delay and options on
animate(withDuration:delay:options:animations:completion:)
. Try to delay the appearance of the label and make it blink a couple of times before disappearing. - Manipulate the count for the
UIPickerViewDataSource
to be greater than the array in order to make the spin appear more slot-machine-like. You can also start the picker view at different positions so the fruit will cover the view top to bottom.