OC 实现类似QQ的自上向下的本地通知消息

1.创建RemindView类,代码如下:

typedef NS_ENUM(NSUInteger, MessageType){
    MessageTypeSuccess,//成功
    MessageTypeError,//错误
    MessageTypeWarning//警告
};
@interface RemindView : UIView
+ (RemindView *)shareRemindView;
- (void)setMessageType:(MessageType )messageType andMessage:(NSString *)message;
- (void)show;
@end
#import "RemindView.h"
@interface RemindView ()
@property (nonatomic, strong)NSArray *imageArray;
@property (nonatomic, strong)UIImageView *imageV;
@property (nonatomic, strong)UILabel *messageL;
@end
@implementation RemindView
+ (RemindView *)shareRemindView {
    static RemindView *rv = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        rv = [[RemindView alloc]init];
    });
    return rv;
}
/**图片数组懒加载*/
- (NSArray *)imageArray {
    if (!_imageArray) {
        _imageArray = @[@"pc_success",@"pc_error",@"pc_warning"];
    }
    return _imageArray;
}
/**初始化*/
- (instancetype)init {
    if (self = [super init]) {
        self.frame = CGRectMake(0, - HeightForNagivationBarAndStatusBar, UI_SCREEN_WIDTH, HeightForNagivationBarAndStatusBar);
        self.backgroundColor = [UIColor whiteColor];
        
        self.layer.shadowOffset = CGSizeMake(1, 3);
        self.layer.shadowOpacity = 0.3;
        self.layer.shadowColor = App_Main_Color.CGColor;
        
        [[self lastWidow] addSubview:self];
        
        self.imageV = [[UIImageView alloc]initWithFrame:CGRectMake(20, 20, 40, 40)];
        [self addSubview:self.imageV];
        
        self.messageL = [[UILabel alloc]initWithFrame:CGRectMake(80, 0, UI_SCREEN_WIDTH - 100, HeightForNagivationBarAndStatusBar)];
        self.messageL.numberOfLines = 0;
        self.messageL.lineBreakMode = NSLineBreakByWordWrapping;
        self.messageL.font = [UIFont systemFontOfSize:15];
        self.messageL.textColor = [UIColor blackColor];
        [self addSubview:self.messageL];
        
    }
    return self;
}
/**设置消息类型及消息内容*/
- (void)setMessageType:(MessageType)messageType andMessage:(NSString *)message {
    self.imageV.image = [UIImage imageNamed:self.imageArray[messageType]];
    self.messageL.text = message;
}
/**弹窗弹出*/
- (void)show {
    self.frame = CGRectMake(0, -HeightForNagivationBarAndStatusBar, UI_SCREEN_WIDTH, HeightForNagivationBarAndStatusBar);
    [self.layer removeAllAnimations];
    [UIView animateWithDuration:0.3 animations:^{
        self.frame = CGRectMake(0, 0, UI_SCREEN_WIDTH, HeightForNagivationBarAndStatusBar);
    } completion:^(BOOL finished) {
        [self dismiss];
    }];
}
/**弹窗隐藏*/
- (void)dismiss {
    [UIView animateWithDuration:0.3 delay:2 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.frame = CGRectMake(0, -HeightForNagivationBarAndStatusBar, UI_SCREEN_WIDTH, HeightForNagivationBarAndStatusBar);
    } completion:nil];
}
#pragma mark - 获取最上层window
- (UIWindow *)lastWidow{
    NSArray *windows = [UIApplication sharedApplication].windows;
    for (UIWindow *window in [windows reverseObjectEnumerator]) {
        if ([window isKindOfClass:[UIWindow class]] && CGRectEqualToRect(window.bounds, [UIScreen mainScreen].bounds)) {
            return window;
        }
    }
    return [UIApplication sharedApplication].keyWindow;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

2.使用方法:

RemindView *rv = [RemindView shareRemindView];
            [rv setMessageType:MessageTypeSuccess andMessage:@"测试一下"];
            [rv show];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值