XYShowAlertView.m 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // XYShowAlertView.m
  3. // 未来家庭
  4. //
  5. // Created by 杨翔 on 16/1/12.
  6. // Copyright © 2016年 杨翔. All rights reserved.
  7. //
  8. #import "XYShowAlertView.h"
  9. #import <Masonry/Masonry.h>
  10. #import "AppDelegate.h"
  11. #import "Header.h"
  12. #define DefaultHeight 70
  13. // 屏幕的宽度 和 高度
  14. #define ScreenWidth [[UIScreen mainScreen] bounds].size.width
  15. #define APP_STATUSBAR_HEIGHT (CGRectGetHeight([UIApplication sharedApplication].statusBarFrame))
  16. #define ScreenHeight ([[UIScreen mainScreen] bounds].size.height+20-APP_STATUSBAR_HEIGHT)
  17. #define NavBarBGColor GET_COLOR(30,190,165,1)
  18. #define GET_COLOR(r,g,b,a) [UIColor colorWithRed:r / 255.0 green:g / 255.0 blue:b / 255.0 alpha:a]
  19. @interface XYShowAlertView ()
  20. @property (nonatomic,strong) UILabel *lbTitle;
  21. @property (nonatomic,strong) UILabel *lbContent;
  22. @property (nonatomic,strong) UIImageView *imageV;
  23. @property (nonatomic,strong) UIImageView *imageView;
  24. @property (nonatomic,strong) UILabel *timeLabel;
  25. @end
  26. @implementation XYShowAlertView
  27. -(instancetype)init
  28. {
  29. self = [super init];
  30. if (self) {
  31. self.frame = CGRectMake(0, -100, ScreenWidth, DefaultHeight);
  32. self.backgroundColor = [UIColor colorWithRed:39/255.0 green:39/255.0 blue:39/255.0 alpha:0.7];
  33. self.backgroundColor = [UIColor blackColor];
  34. self.windowLevel = UIWindowLevelAlert;
  35. self.hidden = NO;
  36. //
  37. AppDelegate *delegate = (AppDelegate *)([UIApplication sharedApplication].delegate);
  38. [self mas_makeConstraints:^(MASConstraintMaker *make) {
  39. make.edges.equalTo(delegate.window);
  40. }];
  41. self.imageV = [[UIImageView alloc] initWithFrame:CGRectMake(15, 10, 20, 20)];
  42. self.imageV.image = [UIImage imageNamed:@"AppIcon.png"];
  43. self.imageV.backgroundColor = [UIColor clearColor];
  44. self.imageV.layer.cornerRadius = 3;
  45. self.imageV.layer.masksToBounds = YES;
  46. [self addSubview:self.imageV];
  47. self.lbTitle = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.imageV.frame) + 10, self.imageV.center.y - 7, 56, 14)];
  48. self.lbTitle.textAlignment = NSTextAlignmentLeft;
  49. self.lbTitle.textColor = [UIColor whiteColor];
  50. self.lbTitle.font = [UIFont systemFontOfSize:14];
  51. self.lbTitle.backgroundColor = [UIColor clearColor];
  52. [self addSubview:self.lbTitle];
  53. self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.lbTitle.frame) + 10, self.imageV.center.y - 7, 50, 12)];
  54. self.timeLabel.textAlignment = NSTextAlignmentLeft;
  55. self.timeLabel.text = TS("Now");
  56. self.timeLabel.textColor = NavBarBGColor;
  57. self.timeLabel.font = [UIFont systemFontOfSize:12];
  58. self.timeLabel.backgroundColor = [UIColor clearColor];
  59. [self addSubview:self.timeLabel];
  60. self.lbContent = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.imageV.frame) + 10, CGRectGetMaxY(self.lbTitle.frame) + 5, ScreenWidth - 60, 25)];
  61. self.lbContent.textColor = [UIColor whiteColor];
  62. self.lbContent.font = [UIFont systemFontOfSize:13];
  63. self.lbContent.backgroundColor = [UIColor clearColor];
  64. [self addSubview:self.lbContent];
  65. UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionHappened)];
  66. recognizer.numberOfTapsRequired = 1;
  67. recognizer.numberOfTouchesRequired = 1;
  68. [self addGestureRecognizer:recognizer];
  69. self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 5)];
  70. self.imageView.layer.cornerRadius = 2.5;
  71. self.imageView.layer.masksToBounds = YES;
  72. self.imageView.backgroundColor = [UIColor whiteColor];
  73. self.imageView.center = CGPointMake(self.frame.size.width/2.0, DefaultHeight - 7);
  74. [self addSubview:self.imageView];
  75. }
  76. return self;
  77. }
  78. static XYShowAlertView *alertView;
  79. +(instancetype)shareInstance
  80. {
  81. static dispatch_once_t onceToken;
  82. dispatch_once(&onceToken, ^{
  83. alertView = [[XYShowAlertView alloc] init];
  84. });
  85. return alertView;
  86. }
  87. -(void)showAlertViewWithTitle:(NSString *)title andDetailText:(NSString *)detailText{
  88. self.hidden = NO;
  89. self.lbTitle.text = title;
  90. CGSize size1 = CGSizeMake(MAXFLOAT, 14);
  91. CGRect rect1 = [title boundingRectWithSize:size1 options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil];
  92. self.lbTitle.frame = CGRectMake(CGRectGetMaxX(self.imageV.frame) + 10, self.imageV.center.y - 7, rect1.size.width, 14);
  93. self.timeLabel.frame = CGRectMake(CGRectGetMaxX(self.lbTitle.frame) + 10, self.imageV.center.y - 7, 50, 12);
  94. self.lbContent.font = [UIFont systemFontOfSize:13];
  95. self.lbContent.numberOfLines = 5;
  96. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:detailText];
  97. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  98. [paragraphStyle setLineSpacing:6];
  99. [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [detailText length])];
  100. self.lbContent.attributedText = attributedString;
  101. CGFloat screenWidth = ScreenWidth < ScreenHeight ? ScreenWidth : ScreenHeight;
  102. CGSize size = CGSizeMake(screenWidth - 60, MAXFLOAT);
  103. CGRect rect = [detailText boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil];
  104. if (rect.size.height < DefaultHeight) {
  105. self.frame = CGRectMake(0, -100, screenWidth, DefaultHeight);
  106. self.imageView.center = CGPointMake(self.frame.size.width/2.0, DefaultHeight - 7);
  107. self.lbContent.frame = CGRectMake(CGRectGetMaxX(self.imageV.frame) + 10, CGRectGetMaxY(self.lbTitle.frame) + 5, screenWidth - 60, 25 + 25);
  108. }
  109. else{
  110. self.frame = CGRectMake(0, -100, screenWidth, rect.size.height + 10);
  111. self.imageView.center = CGPointMake(self.frame.size.width/2.0, CGRectGetMaxY(self.lbContent.frame) + 10 - 7);
  112. self.lbContent.frame = CGRectMake(CGRectGetMaxX(self.imageV.frame) + 10, CGRectGetMaxY(self.lbTitle.frame) + 5, screenWidth - 60, rect.size.height);
  113. }
  114. [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^(){
  115. if (CGRectGetMaxY(self.lbContent.frame) < DefaultHeight) {
  116. self.frame = CGRectMake(0, 0, screenWidth, DefaultHeight);
  117. }
  118. else{
  119. self.frame = CGRectMake(0, 0, screenWidth, CGRectGetMaxY(self.lbContent.frame) + 10);
  120. self.imageView.center = CGPointMake(self.frame.size.width/2.0, CGRectGetMaxY(self.frame) - 7);
  121. }
  122. }completion:nil];
  123. __weak typeof(self) blockSelf = self;
  124. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  125. if (alertView.hidden == NO) {
  126. alertView.hidden = YES;
  127. }
  128. //[blockSelf tapActionHappened];
  129. if (CGRectGetMaxY(self.lbContent.frame) < DefaultHeight) {
  130. blockSelf.frame = CGRectMake(0, -100, screenWidth, DefaultHeight);
  131. }
  132. else{
  133. blockSelf.frame = CGRectMake(0, -100, screenWidth, CGRectGetMaxY(self.lbContent.frame) + 10);
  134. }
  135. });
  136. }
  137. -(void)tapActionHappened
  138. {
  139. if (self.showAlertViewClicked) {
  140. self.showAlertViewClicked(1);
  141. }
  142. if (alertView.hidden == NO) {
  143. alertView.hidden = YES;
  144. }
  145. }
  146. @end