玩手机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.效果