美文网首页
iOS简单实现顶部横幅通知

iOS简单实现顶部横幅通知

作者: Chase_Eleven | 来源:发表于2018-04-13 09:46 被阅读0次

在当前ViewController弹出顶部通知栏


效果图.gif
思路

没有思路, 画一个View,加两个动画一个延时

实现

.h文件

//
//  ELTopBannerNotificationsUtils.h
//  homework
//
//  Created by eleven on 2018/4/12.
//  Copyright © 2018年 homework. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface ELTopBannerNotificationsUtils : NSObject

/**
 顶部横幅弹窗

 @param message NSString 弹窗内容
 */
+ (void)alertWithMessage:(NSString *)message, ...;

@end

.m文件

//
//  ELTopBannerNotificationsUtils.m
//  homework
//
//  Created by eleven on 2018/4/12.
//  Copyright © 2018年 homework. All rights reserved.
//

#import "ELTopBannerNotificationsUtils.h"
#import "ELControllerUtils.h"

#define kDefaultLastTime 1.4
#define kDefaultAnimationTime 0.3


@implementation HWTopBannerNotificationsUtils

+ (void)alertWithMessage:(NSString *)message
{
    if ([message isEqualToString:@""] || message == nil) {
        return;
    }

    CGFloat statusBarHeight = 10;
    if ([UIApplication sharedApplication].isStatusBarHidden || IS_IPHONE_X) {
        statusBarHeight = 0;
    }
    UIViewController *vc = [ELControllerUtils topViewController];
    __block UIView *view = [[UIView alloc] init];
    view.frame = CGRectMake(0, - 56 - HEIGHT_IPHONEX_BOTTOM_WHITE, ScreenWidth, 56 + HEIGHT_IPHONEX_BOTTOM_WHITE);
    view.backgroundColor = [UIColor whiteColor];
    if (vc.navigationController.navigationBarHidden) {
        [vc.view addSubview:view];
    } else {
        [vc.navigationController.navigationBar.superview addSubview:view];
    }
    
    UILabel *lab = [[UILabel alloc] init];
    lab.frame = CGRectMake(0, statusBarHeight + HEIGHT_IPHONEX_BOTTOM_WHITE, ScreenWidth, 56);
    lab.backgroundColor = [UIColor whiteColor];
    lab.text = message;
    lab.textAlignment = NSTextAlignmentCenter;
    lab.textColor = UIColorFromRGB(0x666666);
    lab.font = [UIFont systemFontOfSize:13];
    [view addSubview:lab];
    
    [UIView animateWithDuration:kDefaultAnimationTime delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        view.frame = CGRectMake(0, 0, ScreenWidth, 56 + HEIGHT_IPHONEX_BOTTOM_WHITE);
    } completion:^(BOOL finished) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kDefaultLastTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [UIView animateWithDuration:kDefaultAnimationTime delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                view.frame = CGRectMake(0, - 56 - HEIGHT_IPHONEX_BOTTOM_WHITE, ScreenWidth, 56 + HEIGHT_IPHONEX_BOTTOM_WHITE);
            } completion:^(BOOL finished) {
                [view removeFromSuperview];
                view = nil;
            }];
        });
    }];
}

@end

[ELControllerUtils topViewController]获取当前ViewController 参考链接点击

HEIGHT_IPHONEX_BOTTOM_WHITE为了适配iPhone X

相关文章

  • iOS简单实现顶部横幅通知

    在当前ViewController弹出顶部通知栏 思路 没有思路, 画一个View,加两个动画一个延时 实现 .h...

  • iOS 推送通知及通知扩展

    概述 iOS中的通知包括本地推送通知和远程推送通知,两者在iOS系统中都可以通过弹出横幅的形式来提醒用户,点击横幅...

  • iOS 本地推送

    iOS 本地推送 概念 1.推送通知有5种不同的呈现效果 在屏幕顶部显示一块横幅(显示具体内容)在屏幕中间弹出一个...

  • iOS14 Universal Link隐藏顶部横幅方法

    配置好Universal Link之后顶部都会有一个打开app的横幅,有时候我们不想显示这个横幅,iOS14之后苹...

  • iOS 推送问题全解答《十万个为啥吖》

    Q 1:为啥收不到推送(1)? 如果收到推送时,App 在前台运行,那么: iOS 10 before 顶部横幅不...

  • 推送、套接字 Socket

    1.推送 iOS 中的通知包括本地推送通知和远程推送通知,两者在 iOS 系统中都可以通过弹出横幅的形式来提醒用户...

  • iOS 远程推送通知

    iOS 远程推送通知 分分钟搞定IOS远程消息推送 iOS推送通知的实现步骤 推送通知iOS客户端编写实现及推送服...

  • 消息推送

    推送通知的呈现效果总结 推送通知有5种不同的呈现效果1、在屏幕顶部显示一块横幅(显示具体内容)2、在屏幕中间弹出一...

  • iOS本地推送通知的简单封装(定时提醒、重复提醒)

    实现快捷创建简单的定时重复提醒推送功能。 主要实现原理 iOS10及以上 1.获取通知权限 iOS10及以上要先请...

  • android Notification 弹出横幅

    setFullScreenIntent(pendingIntent, true) 会在顶部弹出一个横幅

网友评论

      本文标题:iOS简单实现顶部横幅通知

      本文链接:https://www.haomeiwen.com/subject/vzwfkftx.html