iOS開發系列之常用自訂控制項開發集—自訂狀態列訊息提示控制項開發

來源:互聯網
上載者:User

iOS開發系列之常用自訂控制項開發集—自訂狀態列訊息提示控制項開發

在實際開發中訊息提示時很常見的需求,為了個人化和擁有簡潔的UI狀態列提示是比較好的方案,好處很多如:不遮擋主UI,新意,下面貼出實現代碼。
WHC_StatusBarMessage.h標頭檔如下:

////  WHCStatusBarMessage.m//  WHCStatusBarMessage////  Created by apple on 14-3-28.//  Copyright (c) 2014年 apple. All rights reserved.//#import "WHC_StatusBarMessage.h"#define kPading (5.0)             //邊距#define kLogoWidth (15.0)         //表徵圖logo寬度@interface WHC_StatusBarMessage(){    UILabel     * msgLab;         //訊息標籤    UIImageView * logoImgV;       //logo表徵圖對象    UIImage     * logoImg;        //logo表徵圖    CGFloat       height;         //高度    CGFloat       screenWidth;    //螢幕寬度    CGFloat       screenHeight;   //螢幕高度}@property(nonatomic,retain)UILabel  * statusLab;@property(nonatomic,retain)UIImageView  * logImgView;@property(nonatomic,retain)NSTimer  * runTimer;           //停留時鐘@end@implementation WHC_StatusBarMessagestatic  WHC_StatusBarMessage  * msb;//構建單例+(WHC_StatusBarMessage *)shareStatusBar{    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        msb = [[WHC_StatusBarMessage alloc]init];    });    return msb;}//初始化UI-(id)init{    CGRect statusFrame = [UIApplication sharedApplication].statusBarFrame;    height = statusFrame.size.height;    screenWidth = [UIScreen mainScreen].bounds.size.width;    screenHeight = [UIScreen mainScreen].bounds.size.height;    self = [super initWithFrame:statusFrame];    if(self){        self.frame = statusFrame;        self.autoresizingMask = UIViewAutoresizingFlexibleWidth;        self.windowLevel = UIWindowLevelStatusBar + 1.0;        self.backgroundColor = kWHC_StatusBarMessageBack_Color;        logoImg = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"29x29" ofType:@"png"]];        logoImgV = [[UIImageView alloc]initWithFrame:CGRectMake(kPading, kPading / 2.0, kLogoWidth, kLogoWidth)];        logoImgV.backgroundColor = [UIColor clearColor];        [self addSubview:logoImgV];        msgLab = [[UILabel alloc]initWithFrame:CGRectMake(logoImgV.frame.origin.x + kPading + logoImgV.frame.size.width, 0.0, screenWidth - (logoImgV.frame.origin.x + kPading + logoImgV.frame.size.width), statusFrame.size.height)];        msgLab.backgroundColor = [UIColor clearColor];        msgLab.font = [UIFont systemFontOfSize:14.0];        msgLab.textColor = [UIColor whiteColor];        [self addSubview:msgLab];        //註冊單擊事件        UITapGestureRecognizer  * tapStatusBar = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapTopBar:)];        [self addGestureRecognizer:tapStatusBar];        //註冊狀態列方向監聽事件        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(screenOrientationChange:) name:UIApplicationWillChangeStatusBarFrameNotification object:nil];    }    return self;}//處理單擊狀態列訊息- (void)tapTopBar:(UITapGestureRecognizer *)tapGesture{    if(_whcStatusBardelegate && [_whcStatusBardelegate respondsToSelector:@selector(didTapTouchWHCStatusBarMessageDoSomething)]){        [_whcStatusBardelegate didTapTouchWHCStatusBarMessageDoSomething];    }}//顯示狀態列訊息-(void)showTextMessage:(NSString*)strMessage delayTime:(NSInteger)delay{    [self.runTimer invalidate];    self.runTimer = nil;    if(logoImg == nil){        logoImg = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"29x29" ofType:@"png"]];    }    if(delay == -1) delay = 3;    logoImgV.image = logoImg;    msgLab.text = strMessage;    __block CGRect  stateFrame = self.frame;    stateFrame.origin.y = -20.0;    self.frame = stateFrame;    [UIView animateWithDuration:0.2 animations:^{        stateFrame.origin.y = 0.0;        self.frame = stateFrame;    }];    [self makeKeyAndVisible];    self.runTimer = [NSTimer scheduledTimerWithTimeInterval:delay target:self selector:@selector(dismissTimer) userInfo:nil repeats:NO];}-(void)showMessage:(NSString*)strMessage logImage:(UIImage *)logImage delayTime:(NSInteger)delay{    logoImg = logImage;    [self showTextMessage:strMessage delayTime:delay];}-(void)dismissTimer{    double delayInSeconds = 0.3;    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){        msb.hidden = YES;    });}#pragma mark - screenChange-(void)screenOrientationChange:(NSNotification*)notif{    UIInterfaceOrientation  orientation = [[[notif userInfo] objectForKey:UIApplicationStatusBarOrientationUserInfoKey] integerValue];    switch (orientation) {        case UIInterfaceOrientationPortrait:            self.transform = CGAffineTransformIdentity;            self.frame = CGRectMake(0.0, 0.0, screenWidth, height);            break;        case UIInterfaceOrientationPortraitUpsideDown:            self.transform = CGAffineTransformMakeRotation(M_PI);            self.center = CGPointMake(screenWidth / 2.0, screenHeight - height / 2.0);            self.bounds = CGRectMake(0.0, 0.0, screenWidth, height);            break;        case UIInterfaceOrientationLandscapeLeft:            self.transform = CGAffineTransformMakeRotation(-M_PI_2);            self.center = CGPointMake(height / 2.0, screenHeight / 2.0);            self.bounds = CGRectMake(0.0, 0.0, screenHeight, height);            break;        case UIInterfaceOrientationLandscapeRight:            self.transform = CGAffineTransformMakeRotation(M_PI_2);            self.center = CGPointMake(screenWidth - height / 2.0, screenHeight / 2.0);            self.bounds = CGRectMake(0.0, 0.0, screenHeight, height);            break;        default:            break;    }}@end

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.