// NBLoadSuccessView.h// 一个打钩的弹框和一个打叉的弹框,后者继承前者,一共四个文件,拷贝创建即可 #import <UIKit/UIKit.h> @interface NBLoadSuccessView : UIView // the message showed in the view @property (nonatomic, copy) NSString *message; // combined property ‘message‘ to use - (void)showInView:(UIView *)view; // suggested that afference a controller‘s view + (void)showMessage:(NSString *)message InView:(UIView *)view; - (void)showMessage:(NSString *)message InView:(UIView *)view; @end
// // NBLoadSuccessView.m #import "NBLoadSuccessView.h" @interface NBLoadSuccessView () @property (nonatomic, weak) UILabel *label; @end @implementation NBLoadSuccessView - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self setup]; } return self; } - (id)initWithCoder:(NSCoder *)aDecoder { if (self = [super initWithCoder:aDecoder]) { [self setup]; } return self; } - (void)setup { self.alpha = 0; UILabel *label = [[UILabel alloc] init]; label.textAlignment = NSTextAlignmentCenter; label.backgroundColor = [UIColor clearColor]; label.textColor = [UIColor whiteColor]; label.font = [UIFont systemFontOfSize:16]; [self addSubview:label]; self.label = label; } - (void)layoutSubviews { [super layoutSubviews]; CGFloat padding = 10; CGFloat labelW = self.bounds.size.width; CGFloat labelH = 16; CGFloat labelX = 0; CGFloat labelY =self.bounds.size.height - labelH - padding; self.label.frame = CGRectMake(labelX, labelY, labelW, labelH); } - (void)drawRect:(CGRect)rect { CGContextRef ctx = UIGraphicsGetCurrentContext(); CGFloat centerX = rect.size.width * 0.5; CGFloat centerY = rect.size.height * 0.5; CGContextMoveToPoint(ctx, centerX - 15, centerY - 15); CGContextAddLineToPoint(ctx, centerX, centerY); CGContextAddLineToPoint(ctx, centerX + 25, centerY - 25); [[UIColor whiteColor] set]; CGContextSetLineWidth(ctx, 8); CGContextSetLineCap(ctx, kCGLineCapRound); CGContextSetLineJoin(ctx, kCGLineJoinRound); CGContextStrokePath(ctx); } - (void)setMessage:(NSString *)message { _message = message; self.label.text = message; } - (void)showMessage:(NSString *)message InView:(UIView *)view { self.message = message; [self showInView:view]; // [view addSubview:self]; // self.frame = [self fixFrame]; // [self dismissView]; } - (void)showInView:(UIView *)view { [view addSubview:self]; self.frame = [self fixFrame]; [self dismissView]; } + (void)showMessage:(NSString *)message InView:(UIView *)view { NBLoadSuccessView *loadSuccessView = [[self alloc] init]; // loadSuccessView.message = message; [loadSuccessView showMessage:message InView:view]; // [view addSubview:loadSuccessView]; // loadSuccessView.frame = [loadSuccessView fixFrame]; // [loadSuccessView dismissView]; } - (CGRect)fixFrame { CGFloat frameW = 120; CGFloat frameH = 100; CGFloat frameX = self.superview.bounds.size.width * 0.5 - frameW * 0.5; CGFloat frameY = self.superview.bounds.size.height * 0.4 - frameH * 0.5; return CGRectMake(frameX, frameY, frameW, frameH); } - (void)dismissView { [UIView animateWithDuration:0.5 animations:^{ self.alpha = 0.3; }]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [UIView animateWithDuration:0.5 animations:^{ self.alpha = 0; } completion:^(BOOL finished) { [self removeFromSuperview]; }]; }); } @end
// // NBLoadFailView.h #import "NBLoadSuccessView.h" @interface NBLoadFailView : NBLoadSuccessView @end
// // NBLoadFailView.m #import "NBLoadFailView.h" @implementation NBLoadFailView - (void)drawRect:(CGRect)rect { CGContextRef ctx = UIGraphicsGetCurrentContext(); CGFloat centerX = rect.size.width * 0.5; CGFloat centerY = rect.size.height * 0.4; CGContextMoveToPoint(ctx, centerX - 15, centerY - 15); CGContextAddLineToPoint(ctx, centerX + 15, centerY + 15); CGContextMoveToPoint(ctx, centerX + 15, centerY - 15); CGContextAddLineToPoint(ctx, centerX - 15, centerY + 15); [[UIColor whiteColor] set]; CGContextSetLineWidth(ctx, 8); CGContextSetLineCap(ctx, kCGLineCapRound); CGContextSetLineJoin(ctx, kCGLineJoinRound); CGContextStrokePath(ctx); } @end
时间: 2024-08-01 10:46:19