YLBaseAlertView.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // YLBaseAlertView.m
  3. // mask
  4. //
  5. // Created by 翟玉磊 on 2018/12/27.
  6. // Copyright © 2018 翟玉磊. All rights reserved.
  7. //
  8. #import "YLBaseAlertView.h"
  9. @interface YLBaseAlertView ()
  10. @property (strong, nonatomic) UIView *backgroundView;
  11. @end
  12. @implementation YLBaseAlertView
  13. - (instancetype)init {
  14. if (self = [super initWithFrame:[UIScreen mainScreen].bounds]) {
  15. self.backgroundColor = [UIColor clearColor];
  16. _clickBackgroundHide = YES;
  17. _backgroundView = [[UIView alloc] initWithFrame:self.frame];
  18. _backgroundView.backgroundColor = Color_Black;
  19. [self addSubview:_backgroundView];
  20. }
  21. return self;
  22. }
  23. - (instancetype)initWithFrame:(CGRect)frame {
  24. if (self = [super initWithFrame:frame]) {
  25. self.backgroundColor = [UIColor clearColor];
  26. _clickBackgroundHide = YES;
  27. _backgroundView = [[UIView alloc] initWithFrame:self.frame];
  28. _backgroundView.backgroundColor = Color_Black;
  29. [self addSubview:_backgroundView];
  30. }
  31. return self;
  32. }
  33. - (void)show {
  34. [[UIApplication sharedApplication].delegate.window addSubview:self];
  35. [self showBackground];
  36. [self showAlertAnimation];
  37. }
  38. - (void)dismiss {
  39. _contentView.hidden = YES;
  40. [self hideAlertAnimation];
  41. [self removeFromSuperview];
  42. }
  43. - (void)setContentView:(UIView *)contentView {
  44. _contentView = contentView;
  45. _contentView.center = self.center;
  46. [self addSubview:_contentView];
  47. }
  48. - (void)showBackground
  49. {
  50. _backgroundView.alpha = 0;
  51. [UIView beginAnimations:@"fadeIn" context:nil];
  52. [UIView setAnimationDuration:0.35];
  53. _backgroundView.alpha = 0.4;
  54. [UIView commitAnimations];
  55. }
  56. -(void)showAlertAnimation
  57. {
  58. CAKeyframeAnimation * animation;
  59. animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
  60. animation.duration = 0.30;
  61. animation.removedOnCompletion = YES;
  62. animation.fillMode = kCAFillModeForwards;
  63. NSMutableArray *values = [NSMutableArray array];
  64. [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 1.0)]];
  65. [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1, 1.1, 1.0)]];
  66. [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
  67. animation.values = values;
  68. [_contentView.layer addAnimation:animation forKey:nil];
  69. }
  70. - (void)hideAlertAnimation {
  71. [UIView beginAnimations:@"fadeIn" context:nil];
  72. [UIView setAnimationDuration:0.35];
  73. _backgroundView.alpha = 0.0;
  74. [UIView commitAnimations];
  75. }
  76. //点击屏幕空白处去掉键盘
  77. - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
  78. {
  79. if ([touches anyObject].view != _contentView && self.clickBackgroundHide) {
  80. // 判断点击的区域如果不是菜单按钮_btnMenu, 则关闭菜单
  81. [self dismiss];
  82. }
  83. }
  84. @end