NoticeView.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // NoticeView.m
  3. // 米兰港
  4. //
  5. // Created by 隋林栋 on 15/3/5.
  6. // Copyright (c) 2015年 Sl. All rights reserved.
  7. //
  8. #import "NoticeView.h"
  9. @implementation NoticeView
  10. #pragma mark 懒加载
  11. - (UILabel *)labelNotice{
  12. if (!_labelNotice) {
  13. _labelNotice = [UILabel new];
  14. [GlobalMethod setLabel:_labelNotice widthLimit:SCREEN_WIDTH - W(40) numLines:0 fontNum:F(13) textColor:[UIColor whiteColor] text:@"" ];
  15. }
  16. return _labelNotice;
  17. }
  18. - (UIControl *)control{
  19. if (!_control) {
  20. _control = [UIControl new];
  21. _control.backgroundColor = [UIColor clearColor];
  22. [_control addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  23. }
  24. return _control;
  25. }
  26. - (UIView *)viewBG{
  27. if (!_viewBG) {
  28. _viewBG = [UIView new];
  29. _viewBG.backgroundColor = COLOR_BLACK_ALPHA_PER60;
  30. [GlobalMethod setRoundView:_viewBG color:[UIColor clearColor]];
  31. [_viewBG addSubview:self.labelNotice];
  32. }
  33. return _viewBG;
  34. }
  35. #pragma mark 初始化
  36. - (instancetype)init{
  37. self = [super init];
  38. if (self) {
  39. [self addSubview:self.viewBG];
  40. // [self addSubview:self.control];
  41. self.userInteractionEnabled = false;
  42. }
  43. return self;
  44. }
  45. #pragma mark 显示提示框
  46. - (void)showNotice:(NSString *)strNotice time:(CGFloat)timeShow frame:(CGRect)frame viewShow:(UIView *)viewShow {
  47. //去掉空格
  48. NSArray * aryStr = [strNotice componentsValidSeparatedByString:@" "];
  49. strNotice = [aryStr componentsJoinedByString:@""];
  50. //停止定时器
  51. [self timerStop];
  52. self.frame = frame;
  53. self.control.frame = self.bounds;
  54. //设置label
  55. [self.labelNotice fitTitle:strNotice variable:SCREEN_WIDTH - W(40)];
  56. self.viewBG.height = self.labelNotice.height + W(16);
  57. self.viewBG.width = self.labelNotice.width + W(20);
  58. self.viewBG.bottom = self.height-W(60);
  59. self.viewBG.centerX = self.width/2.0;
  60. self.labelNotice.center = CGPointMake(self.viewBG.width/2.0, self.viewBG.height/2.0);
  61. [viewShow addSubview:self];
  62. //启动定时器
  63. self.numTime = timeShow;
  64. [self timerStart];
  65. }
  66. #pragma mark 定时器相关
  67. - (void)timerStart{
  68. //开启定时器
  69. if (_timer == nil) {
  70. _timer =[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerRun) userInfo:nil repeats:YES];
  71. }
  72. }
  73. - (void)timerRun{
  74. //每秒的动作
  75. if (_numTime <=0) {
  76. //刷新按钮 开始
  77. [self timerStop];
  78. return;
  79. }
  80. _numTime --;
  81. }
  82. - (void)timerStop{
  83. //停止定时器
  84. if (_timer != nil) {
  85. [_timer invalidate];
  86. self.timer = nil;
  87. }
  88. [self removeFromSuperview];
  89. }
  90. - (void)dealloc{
  91. }
  92. #pragma mark 点击事件
  93. - (void)btnClick:(UIButton *)sender{
  94. [self timerStop];
  95. }
  96. @end