zoukankan      html  css  js  c++  java
  • swift __1 试写第一个swift程序

    参照:http://swiftist.org/topics/96?page=2#comments

    发现有几个地方一直报错,自己修改了下,有可能是xcode6更新导致

    代码写的比较乱,这是我的硬伤,这次注释还是后面加上的,这也是我的硬伤,需要改。养成良好的代码习惯。

    下面贴上自己的代码

    //
    //  ViewController.swift
    //  SwiftCounter
    //
    //  Created by 三十一 on 14-8-20.
    //  Copyright (c) 2014年 yetuoxun. All rights reserved.
    //
    
    import UIKit
    
    class ViewController: UIViewController {
        
        var timeLable:UILabel?//显示剩余时间
        var timeButtons:[UIButton]? //设置时间的按钮数组
        var startStopButton:UIButton? //启动 停止按钮
        var clearButton :UIButton?  // 复位按钮
        let timeButtonInfos = [("1分",5),("3分",180),("5分",300),("",1),]// 数组 记录选项
        var remainingSeconds: Int = 0{
            //通过willSet 方法实时监控remainingSeconds 变化 改变UI (个人理解  willSet 需要去求证)
            willSet(newSeconds){
                let mins = newSeconds/60
                let seconds = newSeconds%60
                self.timeLable!.text = NSString(format: "%02d:%02d", mins,seconds)
            }
        }
        
        var isCounting :Bool = false{
            //通过willSet 方法实时监控isCounting 变化 改变UI (个人理解  willSet 需要去求证)
            willSet(newValue){
                if newValue{
                    //启动timer
                    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "updateTimer:", userInfo: nil, repeats: true)
                }else{
                    //销毁timer
                    timer?.invalidate()
                    timer = nil
                }
                //改变按钮可点击状态
                 setSettingButtonsEnabled(!newValue)
            }
            
        }
        
        //改变按钮可点击状态
        func setSettingButtonsEnabled(enabled: Bool) {
            for button in self.timeButtons! {
                button.enabled = enabled
                button.alpha = enabled ? 1.0 : 0.3
            }
            clearButton!.enabled = enabled
            clearButton!.alpha = enabled ? 1.0 : 0.3
        }
        
        //声明timer
        var timer:NSTimer?
        
        //override 关键字什么意思需要求证
        override func viewDidLoad() {
            super.viewDidLoad()
            self.view.backgroundColor = UIColor.grayColor()
            //初始化 数据及UI
            setupTimeLable()
            setuptimeButtons()
            setupActionButtons()
            // Do any additional setup after loading the view, typically from a nib.
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
    // 初始化显示时间lable
        func setupTimeLable(){
            timeLable = UILabel();
            timeLable!.textColor = UIColor.whiteColor()
            timeLable!.font = UIFont(name: "", size: 80)
            timeLable!.backgroundColor = UIColor.blackColor()
            timeLable!.textAlignment = NSTextAlignment.Center
            self.view.addSubview(timeLable!)
            
        }
        
    
        //初始化 选择时间按钮
        func setuptimeButtons(){
            var buttons:[UIButton] = []
            for (index,(title, _)) in enumerate(timeButtonInfos){
                // 上行中 index title 均为 变量 title类似与 for(id temp in arr) 中的 temp   
                //初始化button 类似与 alloc] init
                let button : UIButton = UIButton()
                button.tag = index;
                //设置标题
                button.setTitle("(title)", forState: UIControlState.Normal)
                //设置背景
                button.backgroundColor = UIColor.orangeColor()
                //设置title各个状态下的颜色
                button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
                button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Highlighted)
                
                //按钮绑定时间 触发方法
                button.addTarget(self, action: "timeButtonTapped:", forControlEvents: UIControlEvents.TouchUpInside)
                //将button 加入buttons数组中 个人理解 需要求证
                buttons.append(button)
    //            buttons.insert(button, atIndex: index)
                //button 加入当前View
                self.view.addSubview(button)
            }
            //数组传递 赋值
            timeButtons = buttons
        }
        
        
        func setupActionButtons(){
            startStopButton = UIButton()
            startStopButton!.backgroundColor = UIColor.redColor()
            startStopButton!.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
            startStopButton!.setTitleColor(UIColor.blackColor(), forState: UIControlState.Highlighted)
            startStopButton!.setTitle("启动/停止", forState: UIControlState.Normal)
            startStopButton!.addTarget(self, action: "startStopButtonTapped:", forControlEvents: UIControlEvents.TouchUpInside)
            self.view.addSubview(startStopButton!)
            
            
            clearButton = UIButton()
            clearButton!.backgroundColor = UIColor.redColor()
            clearButton!.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
            clearButton!.setTitleColor(UIColor.blackColor(), forState: UIControlState.Highlighted)
            clearButton!.setTitle("复位", forState: UIControlState.Normal)
            clearButton!.addTarget(self, action: "clearButtonTapped:", forControlEvents: UIControlEvents.TouchUpInside)
            self.view.addSubview(clearButton!)
        }
        
        
        func startStopButtonTapped(sender:UIButton){
            isCounting = !isCounting
            if isCounting {
                createAndFireLocalNotificationAfterSeconds(remainingSeconds)
            } else {
                println("111111111111111")
                //敲这段代码时间 发现通知闪一下不见了  怀疑是在后台 程序仍可运行这段代码 输出显示我的判断是对的~~
                //但是如果移除这行代码  通知何时移除????
                //结论 定时器 在app 进入后台时仍可运行  这是与之前的区别,之前程序进入后台时  app 会刮起 现在貌似不会了  需求证
                //另外 本地通知 只要app 位于后台才会显示 这与之前是一样的
                UIApplication.sharedApplication().cancelAllLocalNotifications()
            }
    
        }
        
        
        func clearButtonTapped(sender : UIButton){
            remainingSeconds = 0
        }
        
        
        func timeButtonTapped(sender: UIButton){
            let (_,seconds) = timeButtonInfos[sender.tag]
            remainingSeconds += seconds
        }
        
        
        func updateTimer(timer11: NSTimer) {
            remainingSeconds -= 1
            if remainingSeconds <= 0 {
                let alert = UIAlertView()
                alert.title = "计时完成!"
                alert.message = ""
                alert.addButtonWithTitle("OK")
                alert.show()
                timer?.invalidate()
                timer = nil
                setSettingButtonsEnabled(true)
                startStopButtonTapped(startStopButton!);
            }
        }
        
        override func  viewWillLayoutSubviews() {
            super.viewWillLayoutSubviews()
            timeLable!.frame = CGRectMake(10, 40, self.view.bounds.size.width - 20, 120)
            
            let gap = ( self.view.bounds.size.width - 10*2 - (CGFloat(timeButtons!.count) * 64) ) / CGFloat(timeButtons!.count - 1)
            for (index, button) in enumerate(timeButtons!) {
                let buttonLeft = 10 + (64 + gap) * CGFloat(index)
                button.frame = CGRectMake(buttonLeft, self.view.bounds.size.height-120, 64, 44)
            }
            
            startStopButton!.frame = CGRectMake(10, self.view.bounds.size.height-60, self.view.bounds.size.width-20-100, 44)
            clearButton!.frame = CGRectMake(10+self.view.bounds.size.width-20-100+20, self.view.bounds.size.height-60, 80, 44)
    
        }
        func createAndFireLocalNotificationAfterSeconds(seconds: Int) {
            
            UIApplication.sharedApplication().cancelAllLocalNotifications()
            let notification = UILocalNotification()
            
    //        let timeIntervalSinceNow = seconds.bridgeToObjectiveC().doubleValue
    //        let ssy = seconds
    //        notification.fireDate = NSDate(timeIntervalSinceNow:ssy);
            let ssy:NSTimeInterval = NSTimeInterval(seconds)
            notification.fireDate = NSDate(timeIntervalSinceNow: ssy)
            
            notification.timeZone = NSTimeZone.systemTimeZone();
            notification.alertBody = "计时完成!";
            
            UIApplication.sharedApplication().scheduleLocalNotification(notification);
            
        }
        
    }
  • 相关阅读:
    shell加载配置文件
    Shell四种运行方式(启动方式)
    Linux下Fork与Exec使用
    ln -snf 的个人理解
    利用python3将已有的某几个pptx文件的某些slides来生成新的pptx文件的范例
    如何在centos7中安装python3
    ng-include
    ng-class细说 by破狼
    AngularJS的Filter用法详解
    理解angularjs中的$emit,$broadcast和$on by Four
  • 原文地址:https://www.cnblogs.com/yunis/p/3925062.html
Copyright © 2011-2022 走看看