Swift 拖拖看小游戏笔记

16. January 2016 Swift 0

func onUpdate(){ //小游戏更新都需要一个方法

tarValue = Int(1 + arc4random()%100) // 随机数产生

sliderTarget.value = Float(tarValue) // 滑块的value值的类型是float


labRound.text = String(self.Round)

mySlider.value = 50



@IBAction func sliderChange(sender: AnyObject) { //滑块位置改变触发此函数

currValue = lroundf( mySlider.value) //lroundf 四舍五入,把浮点数转换成整数


let alert = UIAlertController(title: “看我帅不帅?”, message: message, preferredStyle: UIAlertControllerStyle.Alert) // UIAlertController在9.0以后使用

alert.addAction(UIAlertAction(title: “帅”, style: UIAlertActionStyle.Default, handler: {

ACTION in self.onUpdate() // 添加按钮的方法,使用addAction,在handler中使用必包函数,调用方法 如果是调用类,

//则使用 ACTION in  self.presentViewController(类名(), animated: true, completion: nil)


alert.addAction(UIAlertAction(title: “不帅”, style: UIAlertActionStyle.Cancel, handler: nil))

self.presentViewController(alert, animated: true, completion: nil) // 显示


@IBAction func onShowInfo(sender: AnyObject) { // 界面初始化

let controller = InfoViewController (nibName:”InfoViewController”,bundle:nil)// 参数暂时无意义

controller.modalTransitionStyle = UIModalTransitionStyle.PartialCurl

self.presentViewController(controller, animated: true, completion: nil)



@IBAction func onBack(sender: AnyObject) { // 界面消失

self.dismissViewControllerAnimated(true, completion: nil)


创建界面可以使用 .xib文件

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.