iOS实现安卓的Toast消息提示框

#import <UIKit/UIKit.h>

@interface Toast : UIView
+(id)toastWithView:(UIView *)view
             title:(NSString *)title
         timestamp:(CGFloat)time;
- (void)show;
@end


#import "Toast.h"

@interface Toast()

@property (nonatomic, retain)UILabel *label;
@property (nonatomic, copy)NSString *title;
@property (nonatomic, assign)CGFloat time;

@end

@implementation Toast
-(void)dealloc{
    [_label release];
    [_title release];
    [super dealloc];
}


- (UILabel *)label{
    if (!_label) {
        self.label = [[UILabel alloc] initWithFrame:CGRectMake(3, 3, self.frame.size.width - 6, 30)];
        _label.textColor = [UIColor whiteColor];
        _label.font = [UIFont systemFontOfSize:14];
        _label.adjustsFontSizeToFitWidth = YES;
        _label.text = _title;
        _label.textAlignment = NSTextAlignmentCenter;
    
    }
    return _label;
}
/*
    便利构造器的方法
 
 */
+(id)toastWithView:(UIView *)view title:(NSString *)title timestamp:(CGFloat)time{
    Toast *toast = [[[self class] alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width / 2, 40)];
    if (toast) {
        CGPoint center = view.center;
        center.y = view.frame.size.height - view.frame.size.height / 4;
        toast.center = center;
        toast.alpha = 0.6;
        toast.backgroundColor = [UIColor blackColor];
        toast.time = time;
        toast.title = title;
        toast.layer.masksToBounds = YES;
        toast.layer.cornerRadius =  5;
        [toast addSubview:toast.label];
        [view addSubview:toast];
    }
return toast;
}

- (void)show{
   [UIView animateWithDuration:self.time animations:^{
       self.alpha = 0;
   } completion:^(BOOL finished) {
       [self removeFromSuperview];
   }];
}


转载于:https://my.oschina.net/u/2395612/blog/466875

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值