首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

iOS 開發實戰:打造 LED 風格數位時鐘

     編輯:Java 相關知識

專案建立

  1. Xcode → File → New → Project → iOS → Application → View-based Application
  2. 專案名稱:LEDClock,儲存路徑預設 ~/Documents。

架構設計

  • AppDelegate:持有 NSTimer,每秒觸發更新。
  • ViewController:持有 UILabel (IBOutlet),負責格式化時間字串並顯示。
  • View (XIB):黑色背景 + 紅色 LED 字體標籤。

核心代碼

1. LEDClockAppDelegate.h

#import <UIKit/UIKit.h>
@class LEDClockViewController;

@interface LEDClockAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    LEDClockViewController *viewController;
    NSTimer *timer;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet LEDClockViewController *viewController;
- (void)onInterval;
@end

2. LEDClockAppDelegate.m

#import "LEDClockAppDelegate.h"
#import "LEDClockViewController.h"

@implementation LEDClockAppDelegate
@synthesize window, viewController;

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                             target:self
                                           selector:@selector(onInterval)
                                           userInfo:nil
                                            repeats:YES];
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
}

- (void)onInterval {
    [viewController interval];
}

- (void)dealloc {
    [timer release];
    [viewController release];
    [window release];
    [super dealloc];
}
@end

3. LEDClockViewController.h

#import <UIKit/UIKit.h>

@interface LEDClockViewController : UIViewController {
    IBOutlet UILabel *timerLabel;
}
@property (nonatomic, retain) UILabel *timerLabel;
- (void)interval;
@end

4. LEDClockViewController.m

#import "LEDClockViewController.h"

@implementation LEDClockViewController
@synthesize timerLabel;

- (void)interval {
    NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *comp = [cal components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit
                                    fromDate:[NSDate date]];
    int h = [comp hour], m = [comp minute], s = [comp second];
    timerLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", h, m, s];
    [cal release];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 設定 LED 字體(需專案包含 DBLCDTempBlack.ttf)
    timerLabel.font = [UIFont fontWithName:@"DBLCDTempBlack" size:50.0];
    timerLabel.text = @"電子時鐘"; // 初始文字
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation {
    return YES; // 支援所有旋轉
}

- (void)dealloc {
    [timerLabel release];
    [super dealloc];
}
@end

介面佈局

  1. 開啟 LEDClockViewController.xib → 雙擊 View → 背景設為黑色。
  2. 從 Library 拖入 Label → 置中、字體紅色、大小適配。
  3. 連接 Outlet:按住 Ctrl 從 File's Owner 拖至 Label → 選 timerLabel。
  4. 若使用自訂字體,需將 DBLCDTempBlack.ttf 加入專案,並在 Info.plist 加入 Fonts provided by application 陣列項。

進階擴充建議

  • 背景模式:Info.plist 加入 UIBackgroundModes = audio 或 location 維持後台計時(需正當理由)。
  • 省電優化:App 進入背景時 [timer invalidate],回前台再重建。
  • 秒針動畫:使用 CABasicAnimation 讓冒號閃爍或數字翻轉。
  • 時區支援:NSTimeZone 切換顯示多城市時間。

總結

本例展示經典的「Timer + ViewController + XIB」模式。關鍵點:NSTimer 在 RunLoop 的 NSRunLoopCommonModes 運行(預設),滑動 ScrollView 時不會暫停;自訂字體需正確嵌入專案;記憶體管理遵循 MRC 規範(本例為舊版代碼),現代專案請改用 ARC + weak 屬性。此架構易於擴展為世界時鐘、碼錶、倒數計時器等應用。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved