iOS开发系列之常用自定义控件开发集—自定义状态栏消息提示控件开发
来源:互联网 发布:python处理json字符串 编辑:程序博客网 时间:2024/05/18 17:04
在实际开发中消息提示时很常见的需求,为了个性化和拥有简洁的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
0 0
- iOS开发系列之常用自定义控件开发集—自定义状态栏消息提示控件开发
- iOS开发系列之常用自定义控件开发集—自定义数字键盘控件开发
- iOS开发系列之常用自定义控件开发集—自定义对话框(UIAlertView)控件开发
- iOS开发系列之常用自定义控件开发集—自定义UIPickerView控件开发2
- iOS开发系列之常用自定义控件开发集—自定义UIDatePicker控件开发
- iOS开发系列之常用自定义控件开发集—自定义组按钮控件开发
- iOS开发系列之常用自定义控件开发集—自定义下拉列表按钮控件开发
- iOS开发系列之常用自定义控件开发集—Android的Toast控件开发
- iOS开发系列之常用自定义控件开发集—进度条Loading控件开发
- iOS开发系列之常用自定义控件开发集—自动广告墙图片控件开发
- iOS开发系列之常用自定义控件开发集—自定义单选按钮或者多选按钮控件开发
- iOS开发系列之常用自定义控件开发集—自定义UITableViewCell侧滑菜单控件开发
- iOS开发系列之常用自定义控件开发集—自动3D广告墙图片控件开发
- iOS开发系列之常用自定义控件开发集—继承UIButton控件代码快速创建按钮开发
- IOS开发--自定义控件
- IOS开发--自定义控件
- IOS开发 - 自定义CheckBox控件
- iOS基础开发-自定义控件
- Scala讲座:类型系统和相关功能
- ArrayAdapter.add方法UnsupportedOperationException
- iOS 获取横竖屏的方法和问题
- Eclipse更新ADT较慢的解决方法
- DFS的时间戳应用
- iOS开发系列之常用自定义控件开发集—自定义状态栏消息提示控件开发
- myeclipse修改默认启动空间
- POJ 2533 Longest Ordered Subsequence (模版LIS)
- C++ 11 Lambda表达式
- HTK
- iOS并发编程
- firefly memcached 和 MySQL 同步问题。。。
- Dijkstra算法
- 语义化