淘汰的iPhone还可以做为床头钟-顺便学习swift

学习swift,觉得很多人家里可能有闲置的淘汰的iPhone. 用作床头钟也挺好的。自己动手搞一个,反正想学学swift。
功能很简单,就是显示数字时钟,随屏幕旋转,使用tabview,一个页面显示时间,一个页面显示设置。(设置功能还未实现)
时间的界面就是5张图片,不显示秒。
代码如下,详细工程可访问Github

 

//
//  FirstViewController.swift
//  mobileclock
//
//  Created by chuan on 10/28/15.
//  Copyright © 2015 ccimage. All rights reserved.
//

import UIKit

class FirstViewController: UIViewController {
    @IBOutlet weak var hourImageView1: UIImageView!
    @IBOutlet weak var hourImageView2: UIImageView!
    @IBOutlet weak var minuteImageView1: UIImageView!
    @IBOutlet weak var minuteImageView2: UIImageView!
    @IBOutlet weak var pointImageView: UIImageView!
    @IBOutlet weak var tapView: UIView!;
    
    let tapRec = UITapGestureRecognizer();
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        changeImageSize();
        doTimer();
        
        tapRec.addTarget(self, action: "tappedView");
        tapView.addGestureRecognizer(tapRec);
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated);
        setTabBarVisible(!tabBarIsVisible(), animated: true)
    }

    override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
        changeImageSize();
    }
    
    func changeImageSize(){
        let screenBounds:CGRect = UIScreen.mainScreen().bounds;
        let width = screenBounds.size.width;
        let height = screenBounds.size.height;
        let eachWidth = width / 5;
        
        hourImageView1.frame = CGRectMake(0, 0, eachWidth, height);
        hourImageView2.frame = CGRectMake(eachWidth, 0, eachWidth, height);
        pointImageView.frame = CGRectMake(eachWidth * 2, 0, eachWidth, height);
        minuteImageView1.frame = CGRectMake(eachWidth * 3, 0, eachWidth, height);
        minuteImageView2.frame = CGRectMake(eachWidth * 4, 0, eachWidth, height);
        
    }
    func doTimer(){
        let timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "timerFireMethod:", userInfo: nil, repeats:true);
        timer.fire()
    }
    func timerFireMethod(timer: NSTimer) {
        let now = NSDate();
        
        let cal = NSCalendar(calendarIdentifier: NSChineseCalendar);
        
        let datecomp1:NSDateComponents = cal!.components(NSCalendarUnit.Hour, fromDate: now);
        let datecomp2:NSDateComponents = cal!.components(NSCalendarUnit.Minute, fromDate: now);
        let hour = datecomp1.hour;
        let minute = datecomp2.minute;
        let a:Int = hour/10;
        let b:Int = hour % 10;
        let c:Int = minute/10;
        let d:Int = minute % 10;
        
        hourImageView1.image = UIImage(named: String(format: "number_0%d.png", a));
        hourImageView2.image = UIImage(named: String(format: "number_0%d.png", b));
        minuteImageView1.image = UIImage(named: String(format: "number_0%d.png", c));
        minuteImageView2.image = UIImage(named: String(format: "number_0%d.png", d));
        
        pointImageView.hidden = !pointImageView.hidden;

    }
    func setTabBarVisible(visible:Bool, animated:Bool) {
        if (tabBarIsVisible() == visible) { return }
        let frame = self.tabBarController?.tabBar.frame;
        let offsetY : CGFloat = (visible ? -49.0 : 49.0);
        
        let duration:NSTimeInterval = (animated ? 0.3 : 0.0);
        if frame != nil {
            UIView.animateWithDuration(duration) {
                self.tabBarController?.tabBar.frame = CGRectOffset(frame!, 0, offsetY)
                return
            }
        }
    }
    func tabBarIsVisible() -> Bool{
        let y1 = self.tabBarController?.tabBar.frame.origin.y;
        let y2 = CGRectGetMaxY(self.view.frame);
        return y1 < y2;
    }
    func tappedView(){
        setTabBarVisible(!tabBarIsVisible(), animated: true)
    }
    
}


QQ20151029-0@2x