
Objective-Cで指定した秒数から時・分・秒数を取得する「DCClock」クラスを作成しました。
秒数からデジタル時計形式の文字列取得も行えます。1桁の数字の桁数を増やして文字列として取得する機能も付属しています。
主な機能は下記の3つです。
主な機能
- 指定した秒数から時・分・秒数を数値/文字列で取得
- デジタル時計形式で文字列を取得
- 1桁の数字の桁数を指定した分だけ増やして文字列取得
下記よりソースコードのダウンロードが行えます。
ダウンロード
ソースコード
DCClock.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| #import <Foundation/Foundation.h>
#define MIN_SEC 60
#define TEN_SEC 10
@interface DCClock : NSObject
#pragma mark - public method
+ (NSString *)digitalClockTime:(NSInteger)seconds;
+ (NSInteger)hour:(NSInteger)seconds;
+ (NSInteger)min:(NSInteger)seconds;
+ (NSInteger)sec:(NSInteger)seconds;
+ (NSString *)hourStr:(NSInteger)seconds;
+ (NSString *)minStr:(NSInteger)seconds;
+ (NSString *)secStr:(NSInteger)seconds;
+ (NSString *)increaseDigits:(NSInteger)value digits:(NSUInteger)digits;
@end
|
DCClock.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
| #import "DCClock.h"
@implementation DCClock
#pragma mark - Digital Clock Time
// デジタル時計の文字列取得
+ (NSString *)digitalClockTime:(NSInteger)seconds
{
NSString *hour = [DCClock increaseDigits:[DCClock hour:seconds] digits:1];
NSString *min = [DCClock increaseDigits:[DCClock min:seconds] digits:1];
NSString *sec = [DCClock increaseDigits:[DCClock sec:seconds] digits:1];
return [NSString stringWithFormat:@"%@:%@:%@", hour, min, sec];
}
#pragma mark - Integer Time
// 時間を取得
+ (NSInteger)hour:(NSInteger)seconds
{
return floor((seconds / MIN_SEC) / MIN_SEC);
}
// 分数を取得
+ (NSInteger)min:(NSInteger)seconds
{
return floor((seconds / MIN_SEC) % MIN_SEC);
}
// 病数を取得
+ (NSInteger)sec:(NSInteger)seconds
{
return floor(seconds % MIN_SEC);
}
#pragma mark - String Time
// 時間の文字列を取得
+ (NSString *)hourStr:(NSInteger)seconds
{
return [NSString stringWithFormat:@"%d", [DCClock hour:seconds]];
}
// 分数の文字列を取得
+ (NSString *)minStr:(NSInteger)seconds
{
return [NSString stringWithFormat:@"%d", [DCClock min:seconds]];
}
// 秒数の文字列を取得
+ (NSString *)secStr:(NSInteger)seconds
{
return [NSString stringWithFormat:@"%d", [DCClock sec:seconds]];
}
#pragma mark - Util
// 桁数を増やす
+ (NSString *)increaseDigits:(NSInteger)value digits:(NSUInteger)digits
{
NSString *valueStr = [NSString stringWithFormat:@"%d", value];
if (value < TEN_SEC) {
for (int i = 1; i <= digits; i++) {
valueStr = [NSString stringWithFormat:@"%@%@", @"0", valueStr];
}
}
return [NSString stringWithFormat:@"%@", valueStr];
}
@end
|
サンプルソースコード
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
| #import "ViewController.h"
@interface ViewController ()
@property NSInteger countdownSec;
@property UILabel *countdownLabel;
@property NSTimer *countdownTimer;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//背景指定
self.view.backgroundColor = [UIColor underPageBackgroundColor];
//カウントダウン秒数 (初期値)
_countdownSec = 4000;
//カウントダウンラベル配置
_countdownLabel = [DCLabel planeLabel:CGRectMake(0, 0, 320, 48)
text:[DCClock digitalClockTime:_countdownSec]
font:[UIFont fontWithName:@"Helvetica" size:20]
textColor:[UIColor whiteColor] textAlignment:NSTextAlignmentCenter
backgroundColor:[UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.5f]];
[self.view addSubview:_countdownLabel];
//カウントダウンタイマー定義
[DCTimer setTimer:1.0f delegate:self selector:@selector(countdownTimerEvent:) userInfo:nil];
}
//カウントダウンタイマーイベント
- (void)countdownTimerEvent:(NSTimer *)timer
{
_countdownSec--;
if (_countdownSec == 0) {
[DCTimer clearTimer];
}
[self updateCountdownLabel:[DCClock digitalClockTime:_countdownSec]];
}
//カウントダウンラベル更新
- (void)updateCountdownLabel:(NSString *)remainingTime
{
_countdownLabel.text = remainingTime;
}
@end
|
関連記事
お薦めの参考書
本気ではじめるiPhoneアプリ作り Xcode 7.x+Swift 2.x対応
iOSアプリ開発をこれから始める方に最もお薦めな一冊です。解り辛い点は図解入りで解説されており、プログラミングの動作の仕組みから詳しく知ることができます。基本から通信処理まで押さえられており、アプリ公開の手順についても解説されています。