ios UIButtonの作成と使用の入門
4984 ワード
UIButton *button ( ) = [UIButton buttonWithType:UIButtonTypeRoundedRect]; // button
button.frame = CGRectMake(35, 70, 300, 50); //button
[button setTitle:@" " forState:UIControlStateNormal];
// button title
button.titleLabel.font = [UIFont systemFontOfSize: 20.0];
//
button.backgroundColor = [UIColor whiteColor]; //button
[button.layer setCornerRadius:10.0];
// button
[button.layer setBorderWidth:0.5];// button
[button.layer setBorderColor:[UIColor grayColor].CGColor];// button
[self.view addSubview:button];
// button
定義できるbuttonタイプは以下の6種類あり、
UIButtonTypeCustom=0、カスタムスタイル
UIButtonTypeRoundedRect,フィレット長方形
UIButtonTypeDetailDisclosure,青い小さな矢印ボタン,主に詳細な説明用
UIButtonType InfoLight、明るい色の感嘆符
UIButtonTypeInfoDark、暗色感嘆符
UIButtonType ContactAdd、十字プラスボタン
forState:このパラメータの役割は、ボタンの文字や画像がどのような状態で現れるかを定義することです.
UIcontrolStateNormal=0、通常の状態が現れる
UIcontrolStateHighlighted=1<<0、ハイライト表示
UIcontrolStateDisabled=1<<1、無効な状態が表示されます
UIcontrolStateSelected=1<<2、選択状態
UIcontrolStateApplication=0 x 00 FF 0000アプリケーションフラグの場合
UIcontrolStateReserved=0 xFF 00000は内部フレームワークの予約であり、
buttonの塗りつぶし画像と背景画像の設定
[buttonsetImage:[UIIImageName:@"ピクチャの名前"]forState:UIcontrolStateNormal;
[buttonsetBackgroundImage:[UIIImageName:@「ピクチャの名前」forState:UIcontrolStateNormal];
ボタンの定義が完了したら、ボタンにイベントを追加する必要があります.
[button addTarget:self action:@selector(btnPressed:) // forControlEvents:UIControlEventTouchUpInside];
//
(target selector event )
-(void)btnPressed:(id)sender
{
HelpViewController *helpView = [[HelpViewController alloc]init];
[self.navigationController pushViewController:helpView animated:YES];
// helpView
}
{}
forControlEventsパラメータタイプ
UIControlEventTouchDown
ワンタッチ押下イベント:ユーザが画面にタッチしたり、新しい指が落ちたりしたとき.
UIControlEventTouchDownRepeat
マルチタッチ押下イベントは、タッチカウントが1より大きい:ユーザが2番目、3番目、または4番目の指を押下したとき.
UIControlEventTouchDragInside
1回のタッチでコントロールウィンドウ内をドラッグすると、
UIControlEventTouchDragOutside
一度タッチしてコントロールウィンドウの外をドラッグすると.
UIControlEventTouchDragEnter
1回のタッチでコントロールウィンドウの外から内部にドラッグすると、
UIControlEventTouchDragExit
1回のタッチでコントロールウィンドウの内部から外部にドラッグすると、
UIControlEventTouchUpInside
コントロール内のすべてのタッチリフトイベント.
UIControlEventTouchUpOutside
コントロールの外でリフトイベントをすべてタッチします(ポイントタッチはコントロールの内部で通知を送信するために開始する必要があります).
UIControlEventTouchCancel
すべてのタッチキャンセルイベント、すなわち、指が多すぎるためにタッチがキャンセルされたり、鍵がかかったり、電話の呼び出しが中断されたりします.
UIControlEventTouchChanged
コントロールの値が変更されると、通知が送信されます.スライダ、セグメントコントロール、その他の値を取るコントロールに使用します.スライダコントロールが通知を送信するタイミングを設定したり、スライダが置かれたときに送信したり、ドラッグされたときに送信したりすることができます.
UIControlEventEditingDidBegin
テキストコントロールで編集を開始すると、通知が送信されます.
UIControlEventEditingChanged
テキストコントロールのテキストが変更されると、通知が送信されます.
UIControlEventEditingDidEnd
テキストコントロールの編集が終了すると、通知が送信されます.
UIControlEventEditingDidOnExit
テキストコントロール内でリターンキー(または等価動作)を押して編集を終了すると、通知が送信されます.
UIControlEventAlltouchEvents
すべてのタッチイベントを通知します.
UIControlEventAllEditingEvents
テキスト編集に関するすべてのイベントを通知します.
UIControlEventAllEvents
すべてのイベントを通知します.
一括作成ボタン作成ボタンをクラスに書く
- (UIButton *)create_button_with_title :( ) (NSString *)title :(CGRect)frame
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];//button
[button setFrame:frame];//button
[button setTitle:title forState:UIControlStateNormal];//button
[button addTarget:self action:@selector(btnPressedPersonName:) forControlEvents:UIControlEventTouchUpInside];//button
button.layer.cornerRadius = 10.0;
//
button.layer.borderColor = [UIColor grayColor].CGColor;
//
button.layer.borderWidth = 1.0;
//
[self.view addSubview:button];
return button;
}
viewDidloadで呼び出す
[self create_button_with_title:@" " :CGRectMake(35, 100, 300,50)];
[self create_button_with_title:@" " :CGRectMake(35, 150, 300,50)];
ボタン動作の実現この例のボタンの動作は同じなので1つ書けば動作が異なるので別々に書く必要があります
-(void)btnPressedPersonName:(id)sender
{
UIButton *button = (UIButton *)sender;
[self.navigationController popViewControllerAnimated:YES];
[[NSNotificationCenter defaultCenter]postNotificationName:@"persRset" object:button.titleLabel.text userInfo:nil];
}