首页 > 其他 > 详细

提示信息功能,显示成功或者失败的弹框,代码简单好用。

时间:2014-11-10 06:26:40      阅读:415      评论:0      收藏:0      [点我收藏+]
 //   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

 

提示信息功能,显示成功或者失败的弹框,代码简单好用。

原文:http://www.cnblogs.com/nbtl/p/4086294.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!