swift基础学习(十四)

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 = "失败"
}

}

}

复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值