๐ ๐ ๐ ๐ ๐ ๐ ๐ ๐ ๐
- 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 the picker view will represent a reel of fruit within the slot machine. UIPickerView
is similar to a UITableView
because it requires adopting protocols (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. The storyboard is already set up with a picker view, a result label, and a spin button. The outlets for the label and the button are already included in the ViewController
class, along with an IBAction for the button. The ViewController
class has an instance property array that contains the data (fruit).
- Create an outlet property for the picker view called
fruitPicker
. - Implement the 2 required data source delegate methods to provide data to the picker view. The picker view should have 3 components.
- Implement the
pickerView(_:titleForRow:forComponent:)
method to set the title from the strings within the array.
- Inside the
spinButton
IBAction method, use theselectRow(_:inComponent:animated:)
andselectedRow(inComponent:)
methods 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.
- Apple keeps the functionality of the picker view animation private but the duration is approximately 0.3 seconds. Animate the appearance of the label and make it blink a couple of times before disappearing.
- Change the count for the
UIPickerViewDataSource
to be greater than the array in order to make the spin appear more like a slot machine. You can also start the picker view at different positions so the fruit will cover the view top to bottom.