【转】iOS类似Android上toast效果
2016-06-20 15:53
363 查看
原文网址:http://m.blog.csdn.net/article/details?id=50478737
做过Android开发的人都知道toast,它会在界面上显示一排黑色背景的文字,用于提示用户信息。但iOS上并没有类似的控件,so,自己写一个吧。
原理:
说白了,Android中的toast可以理解成iOS中的一个黑色背景的UILabel。。。
效果图:
是不是还可以,什么背景颜色,字体大小,位置,统统都是可以自己设置的。
代码:
使用方法:
做过Android开发的人都知道toast,它会在界面上显示一排黑色背景的文字,用于提示用户信息。但iOS上并没有类似的控件,so,自己写一个吧。
原理:
说白了,Android中的toast可以理解成iOS中的一个黑色背景的UILabel。。。
效果图:
是不是还可以,什么背景颜色,字体大小,位置,统统都是可以自己设置的。
代码:
//尺寸设置 #define aiScreenWidth [UIScreen mainScreen].bounds.size.width #define aiScreenHeight [UIScreen mainScreen].bounds.size.height #define STATUS_BAR_HEIGHT [[UIApplication sharedApplication] statusBarFrame].size.height #define NAVIGATION_BAR_HEIGHT self.navigationController.navigationBar.frame.size.height #define TAB_BAR_HEIGHT self.tabBarController.tabBar.frame.size.height
- (void) addToastWithString:(NSString *)string inView:(UIView *)view { CGRect initRect = CGRectMake(0, STATUS_BAR_HEIGHT + 44, aiScreenWidth, 0); CGRect rect = CGRectMake(0, STATUS_BAR_HEIGHT + 44, aiScreenWidth, 22); UILabel* label = [[UILabel alloc] initWithFrame:initRect]; label.text = string; label.textAlignment = NSTextAlignmentCenter; label.textColor = [UIColor whiteColor]; label.font = [UIFont systemFontOfSize:14]; label.backgroundColor = [UIColor colorWithRed:0 green:0.6 blue:0.9 alpha:0.6]; [view addSubview:label]; //弹出label [UIView animateWithDuration:0.5 animations:^{ label.frame = rect; } completion:^ (BOOL finished){ //弹出后持续1s [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(removeToastWithView:) userInfo:label repeats:NO]; }]; } - (void) removeToastWithView:(NSTimer *)timer { UILabel* label = [timer userInfo]; CGRect initRect = CGRectMake(0, STATUS_BAR_HEIGHT + 44, aiScreenWidth, 0); // label消失 [UIView animateWithDuration:0.5 animations:^{ label.frame = initRect; } completion:^(BOOL finished){ [label removeFromSuperview]; }]; }
使用方法:
[self addToastWithString:@"更新到最新数据啦~" inView:self.view];
相关文章推荐
- Android开发基础复习zhi1
- 看懂Android学习最佳路线
- android权限相关的知识
- 【Android】在任何View上添加红点★★★★★★★
- Android Studio 连接提交Git
- 1.2、Android Studio为新设备创建一个模块
- 1.2、Android Studio为新设备创建一个模块
- Android:layout_weight详解
- android studio匪夷所思的问题
- 在Android M及更高版本中使用 Settings.System 抛出异常"You cannot keep your settings in the secure settings. "
- Android-启动模式task-lunchmodle-intent flag 总结
- Android 设计模式—单例模式
- Android中常见的正则表达式工具类
- Android NDK
- Android之Socket简单使用
- Android学习笔记之AndroidManifest.xml文件解析
- 培训讲解--activity生命周期
- android事件拦截处理机制---详解 onInterceptTouchEven onTouchEvent
- Android开发EditText属性
- Android layer-list(1)