#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