表示されてから消える吹き出しのようなテキストの uilabel を簡単に表示する uibutton を作成するにはどうすればよいですか。問題は、吹き出しがボタン フレームの半分の外側にあるビューになることです。
これについて助けてくれてありがとう。
.xibファイルにUIButtonを追加し、名前を付けます。
コードに次のメソッドを追加します。
- (IBAction)bubbleButn:(UIButton *)btn
次に、ボタンを右クリックして、メニューの「TouchUpInside」の部分を確認します。次に、右端の円をクリックして、Xocdeの[プレースホルダー]セクションにあるファイルの所有者にドラッグします。次に、メソッド名がメニューに表示されるのを確認できます。そこから私たちの方法を選択することができます。
次に、コードを実装します。
最初に、.hファイルでUILabelを宣言する必要があります。
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
UILabel *bubbleLabel_;
}
@end
次に、.mファイルにボタンアクションとラベル設定を実装します。
- (IBAction)bubbleButn:(UIButton *)btn {
float half = btn.frame.size.width/2;
float yVal = btn.frame.size.height/2;
if(bubbleLabel_) {
/*
If you are using 'arc' no need of this statement
add this in the dealloc method also.
*/
[bubbleLabel_ release];
bubbleLabel_ = nil;
}
bubbleLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(btn.frame.origin.x+half, btn.frame.origin.y+yVal, 40, 50)];
bubbleLabel_.text = @"Hi";
[self.view addSubview:bubbleLabel_];
[self performSelector:@selector(hideBubble) withObject:self afterDelay:3];
}
- (void) hideBubble {
[bubbleLabel_ removeFromSuperview];
}
「こんにちは」という名前のラベルが3秒間表示されます。
続ける...