我几个月前制作了Android应用程序.Toast类对我来说非常有用.我不需要考虑主线程和地方来显示它.我可以在任何地方展示它,然后离开它,它会自动消失.
Toast.makeToast(context, msg, Toast.LENGTH_SHORT).show();
Run Code Online (Sandbox Code Playgroud)
而已.^^
iPhone怎么样?有像Toast这样的东西吗?只是显示消息,不需要关心它.它会自动消失.
SSe*_*hko 72
我已经为Android写了很长时间了,我想念吐司.我实施了一个.需要代码?这个给你:
ToastView.h
#import <UIKit/UIKit.h>
@interface ToastView : UIView
@property (strong, nonatomic) NSString *text;
+ (void)showToastInParentView: (UIView *)parentView withText:(NSString *)text withDuaration:(float)duration;
@end
Run Code Online (Sandbox Code Playgroud)
ToastView.m
#import "ToastView.h"
@interface ToastView ()
@property (strong, nonatomic, readonly) UILabel *textLabel;
@end
@implementation ToastView
@synthesize textLabel = _textLabel;
float const ToastHeight = 50.0f;
float const ToastGap = 10.0f;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(UILabel *)textLabel
{
if (!_textLabel) {
_textLabel = [[UILabel alloc] initWithFrame:CGRectMake(5.0, 5.0, self.frame.size.width - 10.0, self.frame.size.height - 10.0)];
_textLabel.backgroundColor = [UIColor clearColor];
_textLabel.textAlignment = NSTextAlignmentCenter;
_textLabel.textColor = [UIColor whiteColor];
_textLabel.numberOfLines = 2;
_textLabel.font = [UIFont systemFontOfSize:13.0];
_textLabel.lineBreakMode = NSLineBreakByCharWrapping;
[self addSubview:_textLabel];
}
return _textLabel;
}
- (void)setText:(NSString *)text
{
_text = text;
self.textLabel.text = text;
}
+ (void)showToastInParentView: (UIView *)parentView withText:(NSString *)text withDuaration:(float)duration;
{
//Count toast views are already showing on parent. Made to show several toasts one above another
int toastsAlreadyInParent = 0;
for (UIView *subView in [parentView subviews]) {
if ([subView isKindOfClass:[ToastView class]])
{
toastsAlreadyInParent++;
}
}
CGRect parentFrame = parentView.frame;
float yOrigin = parentFrame.size.height - (70.0 + ToastHeight * toastsAlreadyInParent + ToastGap * toastsAlreadyInParent);
CGRect selfFrame = CGRectMake(parentFrame.origin.x + 20.0, yOrigin, parentFrame.size.width - 40.0, ToastHeight);
ToastView *toast = [[ToastView alloc] initWithFrame:selfFrame];
toast.backgroundColor = [UIColor darkGrayColor];
toast.alpha = 0.0f;
toast.layer.cornerRadius = 4.0;
toast.text = text;
[parentView addSubview:toast];
[UIView animateWithDuration:0.4 animations:^{
toast.alpha = 0.9f;
toast.textLabel.alpha = 0.9f;
}completion:^(BOOL finished) {
if(finished){
}
}];
[toast performSelector:@selector(hideSelf) withObject:nil afterDelay:duration];
}
- (void)hideSelf
{
[UIView animateWithDuration:0.4 animations:^{
self.alpha = 0.0;
self.textLabel.alpha = 0.0;
}completion:^(BOOL finished) {
if(finished){
[self removeFromSuperview];
}
}];
}
@end
Run Code Online (Sandbox Code Playgroud)
从ViewController调用
[ToastView showToastInParentView:self.view withText:@"What a toast!" withDuaration:5.0];
Run Code Online (Sandbox Code Playgroud)
UIKit 中没有“开箱即用”的类来执行此操作。但是创建一个提供这种行为的类是很容易的。
你只需要创建一个继承自 UIView 的类。此类将负责 - 创建您想要显示的内容, - 将自身添加到父视图层次结构中 - 使用计时器关闭自身。
您将能够像这样使用它:
[ToastView toastViewInView:myParentView withText:@"what a wonderful text"];
Run Code Online (Sandbox Code Playgroud)
问候,昆汀
归档时间: |
|
查看次数: |
33462 次 |
最近记录: |