您的位置:首页 > 移动开发 > Android开发

iOS开发系列之常用自定义控件开发集—Android的Toast控件开发

2015-04-19 13:32 706 查看
这篇文章演示编写android系统toast控件,toast控件实用于项目中用来提示简单文字信息,实现代码如下:

头文件内容:

//
//  UIView+WHC_Toast.h
//  UIView+WHC_Toast
//
//  Created by 吴海超 on 15/3/24.
//  Copyright (c) 2015年 吴海超. All rights reserved.
//

#import <UIKit/UIKit.h>

typedef enum{
TOP,                 //上部
MIDDLE,              //中间
BOTTOM               //下部
}WHC_TOAST_POSTION;      //toast  显示的位子

typedef enum {
WHITE_FONT,         //黑背景白字
BLACK_FONT,         //白背景黑字
}WHC_TOAST_TYPE;        //toast 类型

@interface UIView (WHC_Toast)

- (void)toast:(NSString *)msg;

- (void)toast:(NSString *)msg postion:(WHC_TOAST_POSTION)postion;

- (void)toast:(NSString *)msg type:(WHC_TOAST_TYPE)type;

- (void)toast:(NSString *)msg postion:(WHC_TOAST_POSTION)postion type:(WHC_TOAST_TYPE)type;

- (void)toast:(NSString *)msg during:(NSTimeInterval)during;

- (void)toast:(NSString *)msg during:(NSTimeInterval)during postion:(WHC_TOAST_POSTION)postion;

- (void)toast:(NSString *)msg during:(NSTimeInterval)during postion:(WHC_TOAST_POSTION)postion type:(WHC_TOAST_TYPE)type;
@end

源文件实现:
//
//  UIView+WHC_Toast.m
//  UIView+WHC_Toast
//
//  Created by 吴海超 on 15/3/24.
//  Copyright (c) 2015年 吴海超. All rights reserved.
//

#import "UIView+WHC_Toast.h"
#define  KWHC_FONT_SIZE (14.0)     //默认字体大小
#define  KWHC_ANIMATION_TIME (0.5) //显示时动画时间
#define  KWHC_DURING (2.0)         //显示周期
@implementation UIView (WHC_Toast)

//视差效果,toast远近效果
- (CATransform3D)loadTransform3D:(CGFloat)z{

CATransform3D scale = CATransform3DIdentity;
scale.m34 = -1.0 / 1000.0;
CATransform3D transform = CATransform3DMakeTranslation(0.0,0.0, z);
return CATransform3DConcat(transform,scale);
}

/*
显示位子默认下部,
显示时间默认2s
显示类型默认白色黑字
*/
- (void)toast:(NSString *)msg{
[self toast:msg during:KWHC_DURING];
}

/*
显示位子自定义
显示时间默认2s
显示类型默认白色黑字
*/
- (void)toast:(NSString *)msg postion:(WHC_TOAST_POSTION)postion{
[self toast:msg during:KWHC_DURING postion:postion];
}

/*
显示位子自定义
显示时间默认2s
显示类型默认白色黑字
*/

- (void)toast:(NSString *)msg type:(WHC_TOAST_TYPE)type{
[self toast:msg during:KWHC_DURING postion:BOTTOM type:type];
}

/*
显示位子自定义
显示时间自定义
显示类型自定义
*/

- (void)toast:(NSString *)msg postion:(WHC_TOAST_POSTION)postion type:(WHC_TOAST_TYPE)type{
[self toast:msg during:KWHC_DURING postion:postion type:type];
}

/*
显示位子自定义
显示时间自定义
显示类型自定义
*/

- (void)toast:(NSString *)msg during:(NSTimeInterval)during{
[self toast:msg during:KWHC_DURING postion:BOTTOM];
}

- (void)toast:(NSString *)msg during:(NSTimeInterval)during postion:(WHC_TOAST_POSTION)postion{
[selftoast:msg during:KWHC_DURINGpostion:postion type:WHITE_FONT];
}

- (void)toast:(NSString *)msg during:(NSTimeInterval)during postion:(WHC_TOAST_POSTION)postion type:(WHC_TOAST_TYPE)type{
[selfcreateContentLabWithMessage:msg during:duringpostion:postion type:type];
}

/*
创建主控件
*/
- (UILabel*)createContentLabWithMessage:(NSString*)msg during:(NSTimeInterval)during postion:(WHC_TOAST_POSTION)postion type:(WHC_TOAST_TYPE)type{
self.userInteractionEnabled =NO;    //禁用主view的触摸交互
CGSize      screenSize = [UIScreenmainScreen].bounds.size;
CGFloat     contentLabY = 0.0;
UIColor   * fontColor = nil;
UIColor   * backColor = nil;

switch (postion) {
case TOP:
contentLabY = 100.0;   //默认顶部位子100个像素
break;
case MIDDLE:
contentLabY = screenSize.height /2.0;
break;
case BOTTOM:
contentLabY = screenSize.height -100.0;    //默认离底部100个像素
break;
default:
break;
}

switch (type) {
case WHITE_FONT:
fontColor = [UIColor whiteColor];
backColor = [UIColor blackColor];
break;
case BLACK_FONT:
fontColor = [UIColor blackColor];
backColor = [UIColor colorWithRed:240 /255.0 green:240 / 255.0 blue:240 / 255.0 alpha:1.0];
default:
break;
}

CGFloat     pading = 10.0;
// 兼容低版本
#pragma clang diagnostic push
#pragma clang diagnostic ignored"-Wdeprecated-declarations"
CGSize      msgSize = [msg sizeWithFont:[UIFont systemFontOfSize:KWHC_FONT_SIZE]
constrainedToSize:CGSizeMake(MAXFLOAT, 0)];
#pragma clang diagnostic pop
CGFloat     contentLabWidth = msgSize.width + pading * 2.0;
NSInteger   multiple = (NSInteger)(msgSize.width / (screenSize.width - pading *2.0)) + 1;
if(multiple > 1){
contentLabWidth = screenSize.width - pading * 2.0;
}
UILabel * contentLab = [[UILabel alloc]initWithFrame:CGRectMake((screenSize.width - contentLabWidth) /2.0,contentLabY,contentLabWidth,multiple * msgSize.height + pading)];
contentLab.numberOfLines = 0;
contentLab.backgroundColor = backColor;
contentLab.textColor = fontColor;
contentLab.font = [UIFont systemFontOfSize:KWHC_FONT_SIZE];
contentLab.textAlignment = NSTextAlignmentCenter;
contentLab.center = CGPointMake(screenSize.width / 2.0, contentLabY);
contentLab.text = msg;
contentLab.layer.cornerRadius = 8.0;
contentLab.layer.masksToBounds = YES;
contentLab.layer.transform = [self loadTransform3D:-10000.0];
[self addSubview:contentLab];
[UIView animateWithDuration:KWHC_ANIMATION_TIME animations:^{
self.layer.opacity = 0.5;
contentLab.layer.transform = [self loadTransform3D:0.0];
}completion:^(BOOL finished) {
[self performSelector:@selector(clearContentLab:) withObject:contentLab afterDelay:during];
}];
return contentLab;
}

- (void)clearContentLab:(UILabel *)contentLab{

[UIView animateWithDuration:KWHC_ANIMATION_TIME animations:^{
contentLab.layer.transform = [self loadTransform3D:-10000.0];
self.layer.opacity = 1.0;
} completion:^(BOOL finished) {
[contentLab removeFromSuperview];
self.userInteractionEnabled = YES;    //恢复主view的用户交互控制
}];
}
@end




WHC_ToastDemo下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐