//
// BTBaseAlertView.h
// Test
//
// Created by liuyinghui on 2018/8/22.
// Copyright © 2018年 liuyinghui. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface BTBaseAlertView : UIView
@property (strong, nonatomic) UIView *contentView;
- (void)show;
- (void)hide;
@end
//
// BTBaseAlertView.m
// Test
//
// Created by liuyinghui on 2018/8/22.
// Copyright © 2018年 liuyinghui. All rights reserved.
//
#import "BTBaseAlertView.h"
@interface BTBaseAlertView ()
@property (strong, nonatomic) UIView *backgroundView;
@end
@implementation BTBaseAlertView
- (instancetype)init {
if (self = [super initWithFrame:[UIScreen mainScreen].bounds]) {
self.backgroundColor = [UIColor clearColor];
_backgroundView = [[UIView alloc] initWithFrame:self.frame];
_backgroundView.backgroundColor = [UIColor blackColor];
[self addSubview:_backgroundView];
}
return self;
}
- (void)show {
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
NSArray *windowViews = [window subviews];
if(windowViews && [windowViews count] > 0){
UIView *subView = [windowViews objectAtIndex:[windowViews count]-1];
for(UIView *aSubView in subView.subviews)
{
[aSubView.layer removeAllAnimations];
}
[subView addSubview:self];
[self showBackground];
[self showAlertAnimation];
}
}
- (void)hide {
_contentView.hidden = YES;
[self hideAlertAnimation];
[self removeFromSuperview];
}
- (void)setContentView:(UIView *)contentView {
_contentView = contentView;
_contentView.center = self.center;
[self addSubview:_contentView];
}
- (void)showBackground
{
_backgroundView.alpha = 0;
[UIView beginAnimations:@"fadeIn" context:nil];
[UIView setAnimationDuration:0.35];
_backgroundView.alpha = 0.4;
[UIView commitAnimations];
}
-(void)showAlertAnimation
{
CAKeyframeAnimation * animation;
animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
animation.duration = 0.30;
animation.removedOnCompletion = YES;
animation.fillMode = kCAFillModeForwards;
NSMutableArray *values = [NSMutableArray array];
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 1.0)]];
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1, 1.1, 1.0)]];
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
animation.values = values;
[_contentView.layer addAnimation:animation forKey:nil];
}
- (void)hideAlertAnimation {
[UIView beginAnimations:@"fadeIn" context:nil];
[UIView setAnimationDuration:0.35];
_backgroundView.alpha = 0.0;
[UIView commitAnimations];
}
//点击屏幕空白处去掉键盘
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
if ([touches anyObject].view != _contentView) {
// 判断点击的区域如果不是菜单按钮_btnMenu, 则关闭菜单
[self hide];
}
}
@end
使用方法
BTBaseAlertView *alert = [[BTBaseAlertView alloc] init];
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
[contentView setBackgroundColor:[UIColor greenColor]];
alert.contentView = contentView;
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
[btn setBackgroundColor:[UIColor redColor]];
[btn addTarget:self action:@selector(btnPress:) forControlEvents:UIControlEventTouchUpInside];
[contentView addSubview:btn];
[alert show];