LiveRoomAlertView.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // LiveRoomAlertView.m
  3. // TeacherDaya
  4. //
  5. // Created by 王智 on 2022/6/16.
  6. // Copyright © 2022 DayaMusic. All rights reserved.
  7. //
  8. #import "LiveRoomAlertView.h"
  9. #import "UIView+Animation.h"
  10. typedef enum : NSUInteger {
  11. LiveRoomAlertViewCancel,
  12. LiveRoomAlertViewConfirm,
  13. } LiveRoomAlertViewActionTag;
  14. #define ContainerWidth 320
  15. #define ContainerHeight 180
  16. @interface LiveRoomAlertView ()
  17. @property (nonatomic, strong) UILabel *tipsLabel;
  18. @property (nonatomic, strong) UILabel *titleLable;
  19. @property (nonatomic, strong) UIButton *cancelButton;
  20. @property (nonatomic, strong) UIButton *sureButton;
  21. @property (nonatomic, strong) NSString *title;
  22. @property (nonatomic, strong) NSString *leftTitle;
  23. @property (nonatomic, strong) NSString *rightTitle;
  24. @property (nonatomic, copy) LiveAlertCallback cancel;
  25. @property (nonatomic, copy) LiveAlertCallback confirm;
  26. @end
  27. @implementation LiveRoomAlertView
  28. + (LiveRoomAlertView *)liveroomAlertWithTitle:(NSString *)title leftButtonTitle:(NSString *)leftTitle rightTitle:(NSString *)rightTitle inView:(UIView *)displayView cancel:(LiveAlertCallback)cancel confirm:(LiveAlertCallback)confirm {
  29. LiveRoomAlertView *alertView = [[LiveRoomAlertView alloc] initWithFrame:CGRectMake(0, 0, KPortraitWidth, KPortraitHeight)];
  30. alertView.backgroundColor = HexRGBAlpha(0x000000, 0.5f);
  31. alertView.title = title;
  32. alertView.leftTitle = leftTitle;
  33. alertView.rightTitle = rightTitle;
  34. alertView.cancel = cancel;
  35. alertView.confirm = confirm;
  36. [alertView addSubviews];
  37. [alertView showAlertInView:displayView];
  38. return alertView;
  39. }
  40. - (void)addSubviews {
  41. UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake((kScreenWidth - ContainerWidth) / 2, (CGRectGetHeight(self.bounds) - ContainerHeight) / 2, ContainerWidth, ContainerHeight)];
  42. contentView.backgroundColor = [UIColor whiteColor];
  43. contentView.layer.cornerRadius = 8.0f;
  44. contentView.layer.masksToBounds = YES;
  45. [self addSubview:contentView];
  46. [contentView addSubview:self.titleLable];
  47. [contentView addSubview:self.cancelButton];
  48. [contentView addSubview:self.sureButton];
  49. CGFloat width = (ContainerWidth - 1) / 2.0f;
  50. [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
  51. make.left.mas_equalTo(contentView.mas_left);
  52. make.bottom.equalTo(contentView.mas_bottom);
  53. make.height.mas_equalTo(47);
  54. make.width.mas_equalTo(width);
  55. }];
  56. [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
  57. make.left.mas_equalTo(self.cancelButton.mas_right);
  58. make.bottom.mas_equalTo(contentView.mas_bottom);
  59. make.height.mas_equalTo(47);
  60. make.width.mas_equalTo(width);
  61. }];
  62. UIView *buttonSpaceLineView = [[UIView alloc] initWithFrame:CGRectZero];
  63. buttonSpaceLineView.backgroundColor = HexRGB(0xDEDEDE);
  64. [contentView addSubview:buttonSpaceLineView];
  65. [buttonSpaceLineView mas_makeConstraints:^(MASConstraintMaker *make) {
  66. make.left.mas_equalTo(self.cancelButton.mas_right);
  67. make.top.bottom.mas_equalTo(self.cancelButton);
  68. make.width.mas_equalTo(1);
  69. }];
  70. UIView *headLine = [[UIView alloc] initWithFrame:CGRectZero];
  71. headLine.backgroundColor = HexRGB(0xDEDEDE);
  72. [contentView addSubview:headLine];
  73. [headLine mas_makeConstraints:^(MASConstraintMaker *make) {
  74. make.left.right.mas_equalTo(contentView);
  75. make.height.mas_equalTo(1);
  76. make.bottom.mas_equalTo(self.cancelButton.mas_top);
  77. }];
  78. [self.titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
  79. make.top.equalTo(contentView.mas_top).offset(30);
  80. make.left.equalTo(contentView.mas_left).offset(36);
  81. make.right.equalTo(contentView.mas_right).offset(-36);
  82. make.bottom.mas_greaterThanOrEqualTo(self.sureButton.mas_top).offset(-15);
  83. }];
  84. }
  85. - (void)showAlertInView:(UIView *)displayView {
  86. _isShow = YES;
  87. [displayView addSubview:self];
  88. [displayView bringSubviewToFront:self];
  89. [self setPopAnimation];
  90. }
  91. - (void)dismissAlertView {
  92. _isShow = NO;
  93. [self removeFromSuperview];
  94. }
  95. - (void)buttonAction:(UIButton *)button {
  96. if (button.tag == LiveRoomAlertViewCancel) {
  97. if (self.cancel) {
  98. self.cancel();
  99. }
  100. }else {
  101. if (self.confirm) {
  102. self.confirm();
  103. }
  104. }
  105. [self dismissAlertView];
  106. }
  107. - (UILabel *)titleLable {
  108. if (!_titleLable) {
  109. _titleLable = [[UILabel alloc] init];
  110. _titleLable.font = [UIFont systemFontOfSize:14.0f];
  111. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  112. [paragraphStyle setLineSpacing:4];//调整行间距
  113. [paragraphStyle setAlignment:NSTextAlignmentCenter];
  114. NSMutableAttributedString *attrs = [[NSMutableAttributedString alloc] initWithString:self.title attributes:@{NSParagraphStyleAttributeName:paragraphStyle,NSFontAttributeName:[UIFont systemFontOfSize:14.0f],NSForegroundColorAttributeName:HexRGB(0x666666)}];
  115. _titleLable.attributedText = attrs;
  116. _titleLable.numberOfLines = 0;
  117. _titleLable.lineBreakMode = NSLineBreakByTruncatingMiddle;
  118. }
  119. return _titleLable;
  120. }
  121. - (UIButton *)cancelButton {
  122. if(!_cancelButton) {
  123. _cancelButton = [[UIButton alloc] init];
  124. _cancelButton.backgroundColor = [UIColor clearColor];
  125. [_cancelButton.titleLabel setFont:[UIFont systemFontOfSize:16.0f]];
  126. [_cancelButton setTitleColor:HexRGB(0x666666) forState:UIControlStateNormal];
  127. [_cancelButton setTitle:self.leftTitle forState:UIControlStateNormal];
  128. _cancelButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
  129. _cancelButton.tag = LiveRoomAlertViewCancel;
  130. [_cancelButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
  131. }
  132. return _cancelButton;
  133. }
  134. - (UIButton *)sureButton {
  135. if (!_sureButton) {
  136. _sureButton = [[UIButton alloc] init];
  137. _sureButton.backgroundColor = [UIColor clearColor];
  138. [_sureButton.titleLabel setFont:[UIFont systemFontOfSize:16.0f weight:UIFontWeightMedium]];
  139. [_sureButton setTitleColor:HexRGB(0x333333) forState:UIControlStateNormal];
  140. [_sureButton setTitle:self.rightTitle forState:UIControlStateNormal];
  141. _sureButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
  142. _sureButton.tag = LiveRoomAlertViewConfirm;
  143. [_sureButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
  144. }
  145. return _sureButton;
  146. }
  147. /*
  148. // Only override drawRect: if you perform custom drawing.
  149. // An empty implementation adversely affects performance during animation.
  150. - (void)drawRect:(CGRect)rect {
  151. // Drawing code
  152. }
  153. */
  154. @end