[iPhone]UIImageViewにUILableで動く時刻を貼り付ける


ぐるぐる動かしているイメージ(UIImageView)に動く時計の文字列を貼り付けるのをどうやって実現するかでなやんだのだが、UIImageViewにUILabelを貼り付けたらどうだろうと思ってやってみたらうまくいった。

InterfaceBuilderだとUIImageViewのSubViewとして貼り付けることができなかったので、このようにUILabel を作成し、UIImageView* bImageのサブビューとして貼り付けた。

UILabel* timeLabel;
CGSize szImage = bImage.bounds.size;
timeLabel = [[[UILabel alloc] initWithFrame: CGRectMake(5,160,szImage.width,170)] autorelease];
timeLabel.font = [UIFont boldSystemFontOfSize:15.0];
[bImage addSubview:timeLabel];
timeLabel.textColor = [UIColor whiteColor];			//	文字色白
timeLabel.backgroundColor = [UIColor clearColor];	//	背景透明
timeLabel.textAlignment = UITextAlignmentCenter;	//	センタリング

UIImageを回転させるとUILabelも同じように回転する。
MFCのCWndクラスとは比べものにならないくらい高機能だな。時代の変化を感じるw。
img_00041

時刻文字列の作成はタイマーのコールバックで以下のようにやった。

NSDate *date = [NSDate date];		//	現在の日付を得る
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY/MM/dd kk:mm:ss"];
NSString* strTime = [dateFormatter stringFromDate:date];
timeLabel.text = strTime;

img_00061

ぐーるぐるまわったり拡大しても時刻もスムースに同期して動くね。

tAkatronixおすすめのiPhone開発本

iPhone デベロッパーズ クックブック
Erica Sadun
ソフトバンククリエイティブ
売り上げランキング: 84906
詳解 Objective-C 2.0
詳解 Objective-C 2.0
posted with amazlet at 10.01.31
荻原 剛志
ソフトバンククリエイティブ
売り上げランキング: 4163
iPhoneプログラミングUIKit詳解リファレンス
所 友太
リックテレコム
売り上げランキング: 3089
iPhone Core Audioプログラミング
永野 哲久
ソフトバンククリエイティブ
売り上げランキング: 22615

関連記事

関連記事

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

*

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>