ボタンのインスタンスはUIButtonクラスのbuttonWithTypeメソッドを利用して作成します。引数にはボタンタイプを指定しますが、四角形の角丸が取れたような標準ボタンはUIButtonTypeRoudedRectを指定します。
ボタン名はsetTitleで設定します。ボタン状態はforStateで指定します。forStateの代表値は次の通りです。
・UIControlStateNormal //有効状態
・UIControlStateHighlighted //ハイライト状態
・UIControlStateDisabled //無効状態
ボタンの処理はaddTargetで、ボタン処理が定義されているインスタンスとメソッドを設定します。ボタンのイベントはforControlEventsで指定します。forControlEventsの代表値は次の通りです。
・UIControlEventTouchDown //指が触れた
・UIControlEventTouchDragInside //ドラッグした
・UIControlEventTouchUpInside //指が離れた
プログラム
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
CGRect aFrame = [[UIScreen mainScreen] applicationFrame];
float windowWidth = aFrame.size.width;
float windowHeight = aFrame.size.height;
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
//ボタン作成
UIButton* helloButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
helloButton.frame = CGRectMake((windowWidth / 2) - 55, (windowHeight / 2) - 22, 110, 44);
//ボタン名設定
[helloButton setTitle:@"Hello" forState:UIControlStateNormal];
[helloButton setTitle:@"World" forState:UIControlStateHighlighted];
[helloButton setTitle:@"Error" forState:UIControlStateDisabled];
//ボタンのイベントに応じた処理メソッドの呼び出し
[helloButton addTarget:self action:@selector(touchDown) forControlEvents:UIControlEventTouchDown];
[helloButton addTarget:self action:@selector(touchDragInside) forControlEvents:UIControlEventTouchDragInside];
[helloButton addTarget:self action:@selector(touchUpInside) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:helloButton];
[self.window makeKeyAndVisible];
return YES;
}
- (void)touchDown {
NSLog(@"touchDown");
}
- (void)touchDragInside {
NSLog(@"touchDragInside");
}
- (void)touchUpInside {
NSLog(@"touchUpInside");
}
実行結果
ボタンにタッチ→ドラッグ→ボタンから指を離すで次のようなログが出力されました。
2012-08-05 10:32:24.052 HelloWorld[23345:f803] touchDown 2012-08-05 10:32:26.762 HelloWorld[23345:f803] touchDragInside 2012-08-05 10:32:26.845 HelloWorld[23345:f803] touchDragInside 2012-08-05 10:32:26.878 HelloWorld[23345:f803] touchDragInside 2012-08-05 10:32:26.895 HelloWorld[23345:f803] touchDragInside 2012-08-05 10:32:26.907 HelloWorld[23345:f803] touchDragInside 2012-08-05 10:32:26.924 HelloWorld[23345:f803] touchDragInside 2012-08-05 10:32:28.302 HelloWorld[23345:f803] touchUpInside


0 件のコメント:
コメントを投稿