最终效果
全中实在太难了!!!......
最终效果1、UI布局
1、拖入pickerView,设置水平垂直居中,,
2、拖入Label设置左右边距20,下边距20
3、拖入button,设置上下左右边距20,高度40
5、设置label上边距20
6、选中三个控件,更新约束
2、把三个控件都拖入到ViewController中
创建数据源
private lazy var data : NSArray = {
let imageArray = ["👻","👸","💩","😘","🍔","🤖","🍟","🐼","🚖","😭","🎹","😴"]
var data1 = [String]()
var data2 = [String]()
var data3 = [String]()
for var i = 0 ; i <= 100 ; i += 1
{
data1.append(imageArray[NSInteger(arc4random() % 10)])
data2.append(imageArray[NSInteger(arc4random() % 10)])
data3.append(imageArray[NSInteger(arc4random() % 10)])
// data1.append(imageArray[0])
// data2.append(imageArray[0])
// data3.append(imageArray[0])
}
let array = [data1,data2,data3]
return array
}()
<br />
设置代理,圆角
startBtn.layer.cornerRadius = 5
//设置代理
pickerView.delegate = self
pickerView.dataSource = self
<br />
实现代理方法
// returns the number of 'columns' to display.
// 有多少列
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
return data.count
}
// returns the # of rows in each component..
// 每一列有多少个
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return data[component].count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?{
let ss : String = (data[component] as! NSArray)[row] as! String
return ss
}
<br />
实现按钮点击方法
@IBAction func startBtnClick(sender: UIButton) {
//随机选中
pickerView.selectRow(Int(arc4random()) % 90 + 3, inComponent: 0, animated: true)
pickerView.selectRow(Int(arc4random()) % 90 + 3, inComponent: 1, animated: true)
pickerView.selectRow(Int(arc4random()) % 90 + 3, inComponent: 2, animated: true)
//把值取出来,
let d1 = (data[0][pickerView.selectedRowInComponent(0)]) as! String
let d2 = (data[1][pickerView.selectedRowInComponent(1)]) as! String
let d3 = (data[2][pickerView.selectedRowInComponent(2)]) as! String
//打印取出的值
print(d1,d2,d3)
//判断值是否相等
if d1 == d2 && d1 == d3{
messageLabel.text = "Bingo!"
}
else{
messageLabel.text = "💔"
}
//按钮动画
UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.35, initialSpringVelocity: 0, options: .CurveEaseOut, animations: {
self.startBtn.bounds = CGRect(x: self.startBtn.bounds.origin.x, y: self.startBtn.bounds.origin.y, width: self.startBtn.bounds.size.width + 20, height: self.startBtn.bounds.size.height)
}) { (_) in
UIView.animateWithDuration(0.15, animations: {
self.startBtn.bounds = CGRect(x: self.startBtn.bounds.origin.x, y: self.startBtn.bounds.origin.y, width: self.startBtn.bounds.size.width - 20, height: self.startBtn.bounds.size.height)
})
}
}
<br />
网友评论