ErrorTipView.h
#import <UIKit/UIKit.h>
@interface ErrorTipView : UIView
-(id)initWithshowTipsOn:(UIView *)view andText:(NSString *)text andImagg:(UIImage *)image;
@end
ErrorTipView.m
#import "ErrorTipView.h"
@implementation ErrorTipView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
-(id)initWithshowTipsOn:(UIView *)view andText:(NSString *)text andImagg:(UIImage *)image{
if (self = [super init]) {
//对该对象进行赋初始值
self.layer.masksToBounds = YES;
self.layer.cornerRadius = 5 * M_width;
self.backgroundColor = RGBA(48, 48, 48, 1.0);
[view addSubview:self];
UIImageView * imageV = [[UIImageView alloc]initWithFrame:CGRectZero];
imageV.image =image;
[self addSubview:imageV];
UILabel * tipsL = [[UILabel alloc]initWithFrame:CGRectZero];
tipsL.font = [UIFont fontWithName:WENBENZITI size:14 * M_width];
tipsL.textColor = [UIColor whiteColor];
tipsL.numberOfLines = 0;
tipsL.text = text;
[tipsL sizeToFit];
tipsL.textAlignment = NSTextAlignmentCenter;
[self addSubview:tipsL];
CGRect rect = [tipsL.text boundingRectWithSize:CGSizeMake(KWIDTH - 100 * M_width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: tipsL.font} context:nil];
if (image) {
tipsL.frame = FRAME(33 * M_width, 52 * M_height,KWIDTH - 100 * M_width, rect.size.height);
self.frame = FRAME(0, 0, tipsL.frame.size.width + 66 * M_width, rect.size.height + 72 * M_height);
}else{
tipsL.frame = FRAME(33 * M_width, 20 * M_height,KWIDTH - 100 * M_width, rect.size.height);
self.frame = FRAME(0, 0, tipsL.frame.size.width + 66 * M_width, rect.size.height + 40 * M_height);
}
self.center = view.center;
imageV.frame = FRAME(self.frame.size.width / 2 - 12 * M_width, 20 * M_height, 24 * M_width, 24 * M_width);
[self performSelector:@selector(hide) withObject:nil afterDelay:2.0];
}
return self;
}
-(void)hide{
[UIView animateWithDuration:0.8 animations:^{
self.alpha = 0;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
@end