提示信息View自动收回

#import <UIKit/UIKit.h>

@interface NotifyView : UIView
/*  param 1 父view  param2  将要用来提示的内容  */
+(void)showNotifyInSuperView:(UIView *)superView context:(NSString *)aString;

@end
//
//  NotifyView.m
//  NotifyViewText
//
//  Created by admin on 16/1/15.
//  Copyright © 2016年 123. All rights reserved.
//

#import "NotifyView.h"

@implementation NotifyView
+(void)showNotifyInSuperView:(UIView *)superView context:(NSString *)aString
{
    NotifyView *ntfv = (NotifyView *)[superView viewWithTag:12345];
    if (!ntfv)
    {
        ntfv = [[NotifyView alloc]initWithFrame:CGRectMake(0, 0, 280, 120)];
        ntfv.center = superView.center;
        ntfv.tag = 12345;
        ntfv.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        ntfv.layer.cornerRadius = 12.0f;
        ntfv.clipsToBounds = YES;
        ntfv.layer.masksToBounds = YES;
        [superView addSubview:ntfv];
        
        UILabel *label  = [UILabel new];
        label.frame = CGRectMake(8, 8, ntfv.bounds.size.width - 16, ntfv.bounds.size.height - 16);
        label.text = aString;
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor whiteColor];
        label.numberOfLines = 3;
        label.lineBreakMode = NSLineBreakByWordWrapping;
        label.backgroundColor = [UIColor clearColor];
        [ntfv addSubview:label];

    }else
    {
       ntfv.alpha = 0.9;
    }
    [UIView animateWithDuration:1.0 animations:^{
        [ntfv.superview bringSubviewToFront:ntfv];
    }];
    [NSTimer scheduledTimerWithTimeInterval:2.0f target:ntfv selector:@selector(timerFired:) userInfo:@{@"view":ntfv} repeats:NO];
}
-(void)timerFired:(NSTimer *)sender
{
    NotifyView *ntfv = sender.userInfo[@"view"];
    [UIView animateWithDuration:1.0 animations:^{
        [ntfv.superview sendSubviewToBack:ntfv];
        ntfv.alpha = 0.0;
        [sender invalidate];
    }];
}
@end

调用方式

1.导入头文件

#import "NotifyView.h"

2.通过类提供的接口调用----(输入需要展示的参数内容!!!)

- (IBAction)showNotify:(UIButton *)sender
{
    [NotifyView showNotifyInSuperView:self.view context:@"微卡京东方货架上的高房价华盛顿号开发商的积分卡水电费水电费更好的房价考核和哪个部分"];
}
原文地址:https://www.cnblogs.com/Mgs1991/p/5138894.html