1.知识点
- UIPickerView的运用
2.效果图
####3.代码如下
import UIKit
let screenWidth = UIScreen.main.bounds.width
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
let array: [String] = ["?", "?", "☺️", "?", "?", "?", "?", "?", "?", "?"]
var dataArray1 = [Int]()
var dataArray2 = [Int]()
var dataArray3 = [Int]()
@IBOutlet weak var resultLabel: UILabel!
lazy var picker: UIPickerView = UIPickerView()
override func viewDidLoad() {
super.viewDidLoad()
picker.frame.size = CGSize(width:screenWidth , height: 200)
picker.center = self.view.center
picker.delegate = self
picker.dataSource = self
self.view.addSubview(picker)
for _ in 0..<100 {
dataArray1.append((Int)(arc4random() % 10))
dataArray2.append((Int)(arc4random() % 10))
dataArray3.append((Int)(arc4random() % 10))
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
public func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 3
}
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return 100
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return array[row]
}
func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return 100
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let label: UILabel = UILabel()
label.font = UIFont(name: "Apple Color Emoji", size: 80)
if component == 0 {
label.text = array[(Int)(dataArray1[row])]
} else if component == 1 {
label.text = array[(Int)(dataArray2[row])]
} else if component == 2 {
label.text = array[(Int)(dataArray3[row])]
}
label.textAlignment = .center
return label
}
@IBAction func start() {
picker.selectRow(Int(arc4random()) % 90 + 3, inComponent: 0, animated: true)
picker.selectRow(Int(arc4random()) % 90 + 3, inComponent: 1, animated: true)
picker.selectRow(Int(arc4random()) % 90 + 3, inComponent: 2, animated: true)
if (array[dataArray1[picker.selectedRow(inComponent: 0)]] == array[dataArray2[picker.selectedRow(inComponent: 1)]]) && (array[dataArray1[picker.selectedRow(inComponent: 0)]] == array[dataArray3[picker.selectedRow(inComponent: 2)]]) {
self.resultLabel.text = "恭喜你!!!!"
} else {
self.resultLabel.text = "失败"
}
}
}
复制代码