) //时间按钮的布局:按钮大小:64x44,按钮之间的间隔
let cnt = timeButtons.count - 1 let width = view.bounds.width - 10*2.0 - CGFloat(timeButtons.count) * 64.0 let gap = width / CGFloat(cnt) for (index, button) in enumerate(timeButtons) { let buttonLeft = 10.0 + CGFloat(index) * (64.0 + gap) button.frame = CGRectMake(CGFloat(buttonLeft), view.bounds.height-120.0, 64, 44) } //启动复位按钮的布局
startButton.frame = CGRectMake(10, view.bounds.height - 60, view.bounds.width - 20 - 100, 44) resetButton.frame = CGRectMake(10 + startButton.frame.width+20, view.bounds.height - 60, 80, 44) } } 运行程序完成的界面如图-9所示: 图-9 步骤三:实现计时功能 首先设置一个记录当前剩余秒数的属性remainingSeconds,该属性是一个整型的计算属性,当remainingSeconds的值发生改变就更新timeLabel的显示内容,因此给该属性添加一个属性监视器,通过newValue计算出timeLabel显示的内容,代码如下所示: //计算剩余时间
var remainingSeconds:Int = 0 { //属性监视器
willSet { let min = newValue/60 let sec = newValue%60 timeLabel.text = String(NSString(format: "%02d:%02d", min,sec)) } } 其次给预设时间按钮添加点击事件timeButtonTapped:,该方法将用户选择的时间秒数赋值给remainingSeconds,代码如下所示: //预设时间按钮的信息
let timeButtonsInfo = [("1min",60),("3min",180),("5min",300),("sec",1)] func setupTimeButtons(){ timeButtons = [] for (title,sec) in timeButtonsInfo { let button = UIButton() button.backgroundColor = UIColor.orangeColor() button.setTitle(title, forState:UIControlState.Normal) button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal) button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Highlighted) //记录对应的时间给tag
button.tag = sec //给按钮添加点击事件
button.addTarget(self, action: Selector("timeButtonTapped:"), forControlEvents: UIControlEvents.TouchUpInside) view.addSubview(button) timeButtons.append(button) } } func timeButtonTapped (button:UIButton){ remainingSeconds += button.tag } 然后给启动和复位按钮添加点击事件startButtonTapped:和resetButtonTapped:,代码如下所示: //设置启动,复位按钮
func setupActionButtons() { startButton = UIButton() startButton.backgroundColor = UIColor.redColor() startButton.setTitleColor(UIColor.whiteColor(), forState: .Normal) startButton.setTitleColor(UIColor.blackColor(), forState: .Highlighted) startButton.setTitle("Start", forState: .Normal) view.addSubview(startButton) //添加事件
startButton.addTarget(self, action: "startButtonTapped:", forControlEvents: .TouchUpInside) resetButton = UIButton() resetButton.backgroundColor = UIColor.redColor() resetButton.setTitleColor(UIColor.whiteColor(), forState: .Normal) resetButton.setTitleColor(UIColor.blackColor(), forState: .Highlighted) resetButton.setTitle("Reset", forState: .Normal) view.addSubview(resetButton) resetButton.addTarget(self, action: "resetButtonTapped:", forControlEvents: .TouchUpInside) } 接下来需要实现startButtonTapped:和resetButtonTapped:方法,当点击启动按钮时计时器开始计时,因此需要开启一个timer。 在实现startButtonTapped:和resetButtonTapped:方法之前需要在ViewController类中定义一个NSTimer类型的存储属性timer,以及一个用于记录当前计时状态的Bool类型的计算属性isCounting,并带有一个属性监视器。当isCounting属性值为true则表示当前处于计时状态,对timer进行初始化并开始计时,当isCounting属性值为false则表示当前停止计时,timer停止计时并清空,代码如下所示: //计时器计算属性
var timer:NSTimer? var isCounting:Bool = false { //添加属性监视器,当计时开始创建timer否则停止
willSet { if newValue { timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("update |