iOS 简单封装一个可以自定义内容的AlertView的界面

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

使用方法

  1. BTBaseAlertView *alert = [[BTBaseAlertView alloc] init];
  2. UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
  3. [contentView setBackgroundColor:[UIColor greenColor]];
  4. alert.contentView = contentView;
  5. UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
  6. [btn setBackgroundColor:[UIColor redColor]];
  7. [btn addTarget:self action:@selector(btnPress:) forControlEvents:UIControlEventTouchUpInside];
  8. [contentView addSubview:btn];
  9. [alert show];

发表评论

表情:
评论列表 (有 0 条评论,547人围观)

还没有评论,来说两句吧...

相关阅读

    相关 定义友盟分享界面iOS

    提要: 使用代码实现友盟分享弹出的选择界面的自定义方法,不同的按钮实现相应的第三方分享。 1、项目需求        > 1.1.1:下面是我们项目的需求