一個簡單的低仿QQ頂部提示窗

玩手機QQ切換QQ號時,看到頂部的切換提示還不錯疤坝,于是就實現(xiàn)了一下。不足的地方跑揉、太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 左側(cè)的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 右側(cè)的文字提示
- (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 設(shè)置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 創(chuàng)建偽單例具练,確保彈窗的唯一性
+ (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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市哥遮,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌眠饮,老刑警劉巖奥帘,帶你破解...
    沈念sama閱讀 218,525評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件仪召,死亡現(xiàn)場離奇詭異寨蹋,居然都是意外死亡,警方通過查閱死者的電腦和手機已旧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,203評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來运褪,“玉大人,你說我怎么就攤上這事秸讹√戳” “怎么了璃诀?”我有些...
    開封第一講書人閱讀 164,862評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長劣欢。 經(jīng)常有香客問我棕诵,道長氧秘,這世上最難降的妖魔是什么年鸳? 我笑而不...
    開封第一講書人閱讀 58,728評論 1 294
  • 正文 為了忘掉前任丸相,我火速辦了婚禮搔确,結(jié)果婚禮上灭忠,老公的妹妹穿的比我還像新娘膳算。我一直安慰自己弛作,他們只是感情好涕蜂,可當(dāng)我...
    茶點故事閱讀 67,743評論 6 392
  • 文/花漫 我一把揭開白布映琳。 她就那樣靜靜地躺著机隙,像睡著了一般萨西。 火紅的嫁衣襯著肌膚如雪有鹿。 梳的紋絲不亂的頭發(fā)上谎脯,一...
    開封第一講書人閱讀 51,590評論 1 305
  • 那天葱跋,我揣著相機與錄音,去河邊找鬼娱俺。 笑死,一個胖子當(dāng)著我的面吹牛荠卷,可吹牛的內(nèi)容都是我干的模庐。 我是一名探鬼主播僵朗,決...
    沈念sama閱讀 40,330評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼验庙!你這毒婦竟也來了顶吮?” 一聲冷哼從身側(cè)響起粪薛,我...
    開封第一講書人閱讀 39,244評論 0 276
  • 序言:老撾萬榮一對情侶失蹤悴了,失蹤者是張志新(化名)和其女友劉穎违寿,沒想到半個月后湃交,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體藤巢,經(jīng)...
    沈念sama閱讀 45,693評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡搞莺,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,885評論 3 336
  • 正文 我和宋清朗相戀三年掂咒,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片绍刮。...
    茶點故事閱讀 40,001評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡温圆,死狀恐怖孩革,靈堂內(nèi)的尸體忽然破棺而出岁歉,到底是詐尸還是另有隱情膝蜈,我是刑警寧澤锅移,帶...
    沈念sama閱讀 35,723評論 5 346
  • 正文 年R本政府宣布饱搏,位于F島的核電站帆啃,受9級特大地震影響窍帝,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜诽偷,卻給世界環(huán)境...
    茶點故事閱讀 41,343評論 3 330
  • 文/蒙蒙 一疯坤、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧压怠,春花似錦、人聲如沸飞苇。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,919評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至忿等,卻和暖如春栖忠,著一層夾襖步出監(jiān)牢的瞬間贸街,已是汗流浹背庵寞。 一陣腳步聲響...
    開封第一講書人閱讀 33,042評論 1 270
  • 我被黑心中介騙來泰國打工薛匪, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留捐川,地道東北人逸尖。 一個月前我還...
    沈念sama閱讀 48,191評論 3 370
  • 正文 我出身青樓古沥,卻偏偏與公主長得像冷溶,于是被迫代替她去往敵國和親渐白。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,955評論 2 355

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,144評論 25 707
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫纯衍、插件苗胀、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,104評論 4 62
  • 簡介 本文要實現(xiàn)的效果是當(dāng)編輯TextField輸入框的時候襟诸,里面的文字顏色和占位文字顏色不一樣基协,達到一個提醒的效...
    NotFunGuy閱讀 850評論 0 4
  • 這世間本無永遠的感情歌亲,有的只是利益捆綁澜驮。當(dāng)你沒有價值時陷揪,隨時會被人拋棄。與其挖空心思討好他人悍缠,不如完善自我提升價值...
    幽遠致清閱讀 144評論 0 0
  • 17.6.24 周六陰有小雨 這一天又是在忙碌中度過了卦绣,連喝水的功夫感覺都沒有飞蚓,竟有這么忙滤港?。今早想讓孩子在家復(fù)習(xí)...
    gal2017閱讀 153評論 0 0