Custom Control Development set for iOS development series-custom status bar message prompt control development
In actual development, message prompts are common requirements. In order to personalize and have simple UI status bar prompts, it is a good solution, with many benefits, such as: Do not block the main UI, new ideas, the following is the implementation code.
The WHC_StatusBarMessage.h header file is as follows:
//// 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) // margin # define kLogoWidth (15.0) // icon logo width @ interface WHC_StatusBarMessage () {UILabel * msgLab; // message tag UIImageView * logoImgV; // UIImage * logoImg of the logo icon object; // CGFloat height of the logo icon; // height CGFloat screenWidth; // screen width CGFloat screenHeight; // screen height} @ property (nonatomic, retain) UILabel * statusLab; @ property (nonatomic, retain) UIImageView * logImgView; @ property (nonatomic, retain) NSTimer * runTimer; // stop clock @ end @ implementation WHC_StatusBarMessagestatic WHC_StatusBarMessage * msb; // construct Singleton + (WHC_StatusBarMessage *) Declare statusbar {static dispatch_once_t onceToken; dispatch_once (& onceToken, ^ {msb = [[WHC_StatusBarMessage alloc] init] ;}); return msb ;}// initialize 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]; // register and click event details * tapStatusBar = [[using alloc] initWithTarget: self action: @ selector (tapTopBar :)]; [self addGestureRecognizer: tapStatusBar]; // registers the status bar direction listener event [[nsicationicationcenter defaultCenter] addObserver: self selector: @ selector (screenOrientationChange :) name: Your object: nil];} return self;} // process the message of clicking the status bar-(void) tapTopBar :( handle *) tapGesture {if (_ whcStatusBardelegate & [_ whcStatusBardelegate respondsToSelector: @ selector (timer)]) {[_ whcStatusBardelegate didTapTouchWHCStatusBarMessageDoSomething] ;}// display the status bar message-(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 (dismissttor) 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, interval (), ^ (void) {msb. hidden = YES;}) ;}# pragma mark-screenChange-(void) screenOrientationChange :( NSNotification *) notif {UIInterfaceOrientation orientation = [[[notif userInfo] objectForKey: Required] 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