Joe*_*tti 16
cocoacontrols.com上有一个用户库,可以模拟Android风格的Toast弹出窗口.可能就是你要找的东西.
http://www.cocoacontrols.com/platforms/ios/controls/altoastview
还有一个遵循相同想法的那个.
http://www.cocoacontrols.com/platforms/ios/controls/itoast
Mar*_*pic 15
创建一个继承自的类UIAlertView.在您的构造函数中,只需调用[super init]然后添加您想要的任何视图作为子视图.您甚至可以在Interface Builder中设计此视图.像这样的东西:
- (id)initWithMessage:(NSString *)message dismissAfter:(NSTimeInterval)interval
{
if ((self = [super init]))
{
CustomView * customView = [[[CustomView alloc] init] autorelease]; // or load from NIB
[self addSubview:customView];
[self performSelector:@selector(dismissAfterDelay) withObject:nil afterDelay:interval];
}
return self;
}
- (void)dismissAfterDelay
{
[self dismissWithClickedButtonIndex:0 animated:YES];
}
Run Code Online (Sandbox Code Playgroud)
要显示您的自定义警报视图,只需初始化它,并show像常规一样调用UIAlertView.
CustomAlertView * cav = [[CustomAlertView alloc] initWithMessage:@"Doing Something];
[cav show];
[cav release];
Run Code Online (Sandbox Code Playgroud)
作为一个很好的副作用,当你呈现这个视图时,背景会变暗,你会得到任何警报视图的漂亮摇晃动画.
使用UIAlertView.这是一个简单示例的快速链接:
编辑:对不起,没看到它自己消失了.我认为您需要对用户收到的消息进行一些确认.UIAlertView几乎完成了这一点.不确定你是否逐渐消失,除非你在一个具有定时器或基于事件的延迟视图的应用程序中显示视图,然后最终删除它.
第二次编辑:找到实现它的方法.您可以在指定的某个设定时间后手动调用解除功能.(抱歉凌乱的帖子)看起来像这样:
//Create UIAlertView alert
alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Some message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil];
//After some time
[alert dismissWithClickedButtonIndex:0 animated:TRUE];
Run Code Online (Sandbox Code Playgroud)
我创建了一个Android-Kind toast,非常简单,因为我现在不需要更多的功能.
当显示它时,它被添加到父视图的底部,所以如果该视图是VC的视图,那么它将位于设备的底部中心.
框架自动调整为文本长度.
您使用它: [self.view addSubview: [[ToastAlert alloc] initWithText: @"Sent"]];它将被自动删除,因此不需要引用.
我没有实现这个,但你可以创建一个静态方法来缩短和澄清指令,类似于:[ToastAlert showText: @"Sent" inView: self.view];.
班级:
ToastAlert.h
@interface ToastAlert : UILabel {
}
- (id)initWithText: (NSString*) msg;
@end
Run Code Online (Sandbox Code Playgroud)
ToastAlert.m
#import "ToastAlert.h"
#import <QuartzCore/QuartzCore.h>
@implementation ToastAlert
#define POPUP_DELAY 1.5
- (id)initWithText: (NSString*) msg
{
self = [super init];
if (self) {
self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
self.textColor = [UIColor colorWithWhite:1 alpha: 0.95];
self.font = [UIFont fontWithName: @"Helvetica-Bold" size: 13];
self.text = msg;
self.numberOfLines = 0;
self.textAlignment = UITextAlignmentCenter;
self.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
}
return self;
}
- (void)didMoveToSuperview {
UIView* parent = self.superview;
if(parent) {
CGSize maximumLabelSize = CGSizeMake(300, 200);
CGSize expectedLabelSize = [self.text sizeWithFont: self.font constrainedToSize:maximumLabelSize lineBreakMode: NSLineBreakByTruncatingTail];
expectedLabelSize = CGSizeMake(expectedLabelSize.width + 20, expectedLabelSize.height + 10);
self.frame = CGRectMake(parent.center.x - expectedLabelSize.width/2,
parent.bounds.size.height-expectedLabelSize.height - 10,
expectedLabelSize.width,
expectedLabelSize.height);
CALayer *layer = self.layer;
layer.cornerRadius = 4.0f;
[self performSelector:@selector(dismiss:) withObject:nil afterDelay:POPUP_DELAY];
}
}
- (void)dismiss:(id)sender {
// Fade out the message and destroy self
[UIView animateWithDuration:0.6 delay:0 options: UIViewAnimationOptionAllowUserInteraction
animations:^ { self.alpha = 0; }
completion:^ (BOOL finished) { [self removeFromSuperview]; }];
}
@end
Run Code Online (Sandbox Code Playgroud)
使用带有nil title和nil按钮的UIAlertView,然后在需要时将其关闭.这是我如何做到这一点:
在.h文件中为警报视图创建实例变量:
@interface StatusMessageController : UIViewController {
UIAlertView *statusAlert;
}
Run Code Online (Sandbox Code Playgroud)
在.m文件中,创建一个显示警报视图并启动计时器的方法,另一个用于处理计时器到期时解除警报的方法:
- (void)showStatus:(NSString *)message timeout:(double)timeout {
statusAlert = [[UIAlertView alloc] initWithTitle:nil
message:message
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:nil];
[statusAlert show];
[NSTimer scheduledTimerWithTimeInterval:timeout
target:self
selector:@selector(timerExpired:)
userInfo:nil
repeats:NO];
}
- (void)timerExpired:(NSTimer *)timer {
[statusAlert dismissWithClickedButtonIndex:0 animated:YES];
}
Run Code Online (Sandbox Code Playgroud)
每当您想要显示状态消息时,请调用它:
[self showStatus:@"Computing" timeout:4.5];
Run Code Online (Sandbox Code Playgroud)
您可以随时通过以下方式解除警报:
[statusAlert dismissWithClickedButtonIndex:0 animated:YES];
Run Code Online (Sandbox Code Playgroud)
您还可以使用新状态即时更改消息:
statusAlert.message = @"Looking up user";
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
84283 次 |
| 最近记录: |