一个简单的低仿QQ顶部提示窗

玩手机QQ切换QQ号时,看到顶部的切换提示还不错,于是就实现了一下。不足的地方、太low的地方,欢迎各位指出。
代码如下:
1.ZAlertView部分。

#import <UIKit/UIKit.h>
typedef NS_OPTIONS (NSInteger ,AlertViewType){
    AlertViewTypeSuccess = 0,
    AlertViewTypeError   = 1,
    AlertViewTypeMessage = 2
};

@interface ZAlertView : UIView

@property (nonatomic,assign) AlertViewType alertViewType;
@property (nonatomic,strong) UIImageView *imageView;
@property (nonatomic,strong) UILabel     *tipsLabel;
- (instancetype)init;
- (void)topAlertViewTypewWithType:(AlertViewType)type title:(NSString *)title;
- (void)show;
- (void)dismiss;
@end


#import "ZAlertView.h"
#import "UIColor+Hexadecimal.h"

#define Start_Height -64
#define Height 64
#define Screen_Width [UIScreen mainScreen].bounds.size.width
#define Left_Offset 45
#define Font_Size 14.0f
#define Image_Center_X 25
#define Image_Center_Y 40
#define Image_Width 20
@implementation ZAlertView

#pragma mark 左侧的icon
- (UIImageView *)imageView
{
    if (_imageView == nil)
    {
        _imageView = [[UIImageView alloc]init];
        _imageView.frame = CGRectMake(0, 0, Image_Width, Image_Width);
        _imageView.center = CGPointMake(Image_Center_X, Image_Center_Y);
        [self addSubview:_imageView];
    }
    return _imageView;
}

#pragma mark 右侧的文字提示
- (UILabel *)tipsLabel
{
    if (_tipsLabel == nil)
    {
        _tipsLabel = [[UILabel alloc]init];
        _tipsLabel.numberOfLines = 0;
        _tipsLabel.frame = CGRectMake(Left_Offset, 20, Screen_Width - Left_Offset, 40);
        _tipsLabel.textAlignment = NSTextAlignmentLeft;
        _tipsLabel.font = [UIFont systemFontOfSize:Font_Size];
        [self addSubview:_tipsLabel];
    }
    return _tipsLabel;
}

#pragma mark 初始化
- (instancetype)init
{
    self = [super init];
    if (self)
    {
//        [[UIApplication sharedApplication].keyWindow addSubview:self];
    }
    return self;
}

#pragma mark 设置type
- (void)topAlertViewTypewWithType:(AlertViewType)type title:(NSString *)title
{
    switch (type)
    {
        case AlertViewTypeSuccess:
        {
            self.frame = CGRectMake(0, Start_Height, Screen_Width, Height);
            self.backgroundColor = [UIColor colorWithHexString:@"#E5E5E5"];
            self.imageView.image = [UIImage imageNamed:@"success"];
            self.tipsLabel.text = title;
            self.tipsLabel.textColor = [UIColor darkTextColor];
        }
            break;
        case AlertViewTypeError:
        {
            self.frame = CGRectMake(0, Start_Height, Screen_Width, Height);
            self.backgroundColor = [UIColor colorWithHexString:@"#EE7942"];
            self.imageView.image = [UIImage imageNamed:@"error"];
            self.tipsLabel.text = title;
            self.tipsLabel.textColor = [UIColor groupTableViewBackgroundColor];
        }
            break;
        case AlertViewTypeMessage:
        {
            self.frame = CGRectMake(0, Start_Height, Screen_Width, Height);
            self.backgroundColor = [UIColor colorWithHexString:@"#bfbfbf"];
            self.imageView.image = [UIImage imageNamed:@"message"];
            self.tipsLabel.text = title;
            self.tipsLabel.textColor = [UIColor darkTextColor];
        }
            break;
        default:
            break;
    }
}

#pragma mark 显示
- (void)show
{
    [UIView animateWithDuration:1.0f
                          delay:0
         usingSpringWithDamping:0.3f
          initialSpringVelocity:6.0f
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
                         self.center = CGPointMake(self.center.x, 32);
                     }
                     completion:^(BOOL finished) {
                     }];
}

#pragma mark 移除
- (void)dismiss
{
    [UIView animateWithDuration:1.0f
                          delay:0
         usingSpringWithDamping:0.99f
          initialSpringVelocity:6.0f
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         self.center = CGPointMake(self.center.x, -32);
                     }
                     completion:^(BOOL finished) {
                     }];
}
@end

2.ZAlertViewManager部分

#import <Foundation/Foundation.h>
#import "ZAlertView.h"
@interface ZAlertViewManager : NSObject
@property (nonatomic,strong)ZAlertView *alertView;
+ (ZAlertViewManager *)shareManager;

- (void)showWithType:(AlertViewType)type title:(NSString *)title;
- (void)dismissWithTime:(NSInteger)time;
@end
#import "ZAlertViewManager.h"

@implementation ZAlertViewManager

#pragma mark 创建伪单例,确保弹窗的唯一性
+ (ZAlertViewManager *)shareManager
{
    static ZAlertViewManager *shareManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareManager = [[ZAlertViewManager alloc]init];
        shareManager.alertView = [[ZAlertView alloc]init];
        [[UIApplication sharedApplication].keyWindow addSubview:shareManager.alertView];

    });
    return shareManager;
}
#pragma mark 显示弹窗
- (void)showWithType:(AlertViewType)type title:(NSString *)title
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.alertView topAlertViewTypewWithType:type title:title];
        [self.alertView show];
    });
}

#pragma mark 移除弹窗
- (void)dismissWithTime:(NSInteger)time
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.alertView dismiss];
        });
    });
}

3.使用

- (IBAction)show:(id)sender
{
    [[ZAlertViewManager shareManager] showWithType:AlertViewTypeSuccess title:@"Success!"];
}

- (IBAction)dismiss:(id)sender
{
    [[ZAlertViewManager shareManager] dismissWithTime:0];
}
- (IBAction)show2:(id)sender
{
    [[ZAlertViewManager shareManager] showWithType:AlertViewTypeError title:@"Error!"];
}

- (IBAction)dismiss2:(id)sender
{
    [[ZAlertViewManager shareManager] dismissWithTime:0];
}
- (IBAction)showMessage:(id)sender
{
    [[ZAlertViewManager shareManager] showWithType:AlertViewTypeMessage title:@"Message Message Message Message Message Message Message Message Message Message "];
}

4.效果


QQ20170531-094524.gif
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,020评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,198评论 4 61
  • 简介 本文要实现的效果是当编辑TextField输入框的时候,里面的文字颜色和占位文字颜色不一样,达到一个提醒的效...
    NotFunGuy阅读 864评论 0 4
  • 这世间本无永远的感情,有的只是利益捆绑。当你没有价值时,随时会被人抛弃。与其挖空心思讨好他人,不如完善自我提升价值...
    幽远致清阅读 148评论 0 0
  • 17.6.24 周六阴有小雨 这一天又是在忙碌中度过了,连喝水的功夫感觉都没有,竟有这么忙?。今早想让孩子在家复习...
    gal2017阅读 160评论 0 0