UI按钮,短暂显示一个弹出标签,标签将显示在按钮外的中间位置

时间:2012-07-10 03:22:42

标签: ios uibutton

如何创建一个uibutton,它会显示一些文本的简单字样,就像一个会出现然后消失的语音泡泡。问题是讲话泡泡将是位于按钮框架外面的视图。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

在.xib文件中添加UIButton并给出一些名称。

在代码中添加以下方法。 - (IBAction)bubbleButn:(UIButton *)btn

然后右键单击按钮,在菜单中看到“Touch Up Inside”部分。然后,您可以单击并将最右侧的圆拖动到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];
}

名为“Hi”的标签将在那里停留3秒钟。

继续......