镇楼图
弹窗控件封装,仿照微信的样式进行基础开发,并进行其他样式扩展。
#import
NS_ASSUME_NONNULL_BEGIN
typedef NS_ENUM(NSUInteger, WDPopAnimationType) {
WDPopAnimationTypeDefault,
WDPopAnimationTypeFromLeft,
WDPopAnimationTypeFromRight,
WDPopAnimationTypeFromTop,
WDPopAnimationTypeFromBottom
};
UIKIT_EXTERN const NSTimeInterval WDPopAnmationDurition;
@interface WDPopBaseView : UIControl
+ (instancetype)defaultView;
- (void)showView;
- (void)showViewInSuperView:( UIView * _Nullable)superView;
- (void)showViewWithAnimationType:(WDPopAnimationType)animationType;
- (void)showViewWithAnimationType:(WDPopAnimationType)animationType inSuperView:( UIView * _Nullable)superView;
- (void)hidenView;
@end
NS_ASSUME_NONNULL_END
#import "WDPopBaseView.h"
const NSTimeInterval WDPopAnmationDurition = 0.35;
static inline UIView *wKeyWindow() {
if (UIApplication.sharedApplication.delegate.window != nil) {
return UIApplication.sharedApplication.delegate.window;
} else {
return UIApplication.sharedApplication.keyWindow;
}
}
@interface WDPopBaseView ()
@property (nonatomic, assign) WDPopAnimationType animationType;
@property (nonatomic, assign) CGRect originRect;
@property (nonatomic, assign) CGRect animationRect;
@end
@implementation WDPopBaseView
+ (instancetype)defaultView {
static dispatch_once_t onceToken;
static WDPopBaseView *view = nil;
dispatch_once(&onceToken, ^{
view = [[self alloc] init];
});
return view;
}
- (instancetype)init {
self = [super init];
if (self) {
[self addTarget:self action:@selector(hidenView) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (void)showView {
[self showViewInSuperView:wKeyWindow()];
self.animationType = WDPopAnimationTypeDefault;
}
- (void)showViewInSuperView:(UIView *)superView {
self.animationType = WDPopAnimationTypeDefault;
if (superView != nil) {
[self showViewWithAnimationType:WDPopAnimationTypeDefault inSuperView:superView];
} else {
[self showViewWithAnimationType:WDPopAnimationTypeDefault inSuperView:wKeyWindow()];
}
}
- (void)showViewWithAnimationType:(WDPopAnimationType)animationType {
self.animationType = animationType;
[self showViewWithAnimationType:animationType inSuperView:wKeyWindow()];
}
- (void)showViewWithAnimationType:(WDPopAnimationType)animationType inSuperView:(UIView *)superView {
if (superView != nil) {
[superView addSubview:self];
} else {
[wKeyWindow() addSubview:self];
}
self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.0];
UIView *view = self.subviews.firstObject;
CGRect origin = view.frame;
self.originRect = view.frame;
CGRect screen_bounds = [[UIScreen mainScreen] bounds];
CGFloat screen_width = screen_bounds.size.width;
CGFloat screen_height = screen_bounds.size.height;
CGFloat view_width = CGRectGetWidth(view.frame);
CGFloat view_height = CGRectGetHeight(view.frame);
CGFloat view_x = CGRectGetMinX(view.frame);
CGFloat view_y = CGRectGetMinY(view.frame);
self.frame = screen_bounds;
if (animationType == WDPopAnimationTypeDefault) {
view.transform = CGAffineTransformMakeScale(0, 0);
[UIView animateWithDuration:WDPopAnmationDurition animations:^{
self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
view.transform = CGAffineTransformIdentity;
}];
} else {
switch (animationType) {
case WDPopAnimationTypeFromTop:
{
view.frame = CGRectMake(view_x, view_y - screen_height, view_width, view_height);
}
break;
case WDPopAnimationTypeFromBottom:
{
view.frame = CGRectMake(view_x, view_y + screen_height, view_width, view_height);
}
break;
case WDPopAnimationTypeFromLeft:
{
view.frame = CGRectMake(-screen_width, view_y, view_width, view_height);
}
break;
case WDPopAnimationTypeFromRight:
{
view.frame = CGRectMake(screen_width, view_y, view_width, view_height);
}
break;
default:
break;
}
[self handerViewFrame:view originFrame:origin];
}
}
- (void)handerViewFrame:(UIView *)view originFrame:(CGRect)originFrame {
self.animationRect = view.frame;
[UIView animateWithDuration:WDPopAnmationDurition animations:^{
view.frame = originFrame;
self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
}];
}
- (void)hidenView {
UIView *view = self.subviews.firstObject;
[UIView animateWithDuration:WDPopAnmationDurition delay:0 options:(UIViewAnimationOptionCurveEaseInOut) animations:^{
if (self.animationType != WDPopAnimationTypeDefault) {
view.frame = self.animationRect;
} else {
view.transform = CGAffineTransformMakeScale(0.2, 0.2);
}
self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.0];
} completion:^(BOOL finished) {
[self removeFromSuperview];
view.transform = CGAffineTransformIdentity;
view.frame = self.originRect;
}];
}
@end
#import "WDPopBaseView.h"
NS_ASSUME_NONNULL_BEGIN
typedef void(^TestViewBlock)(NSString *obj);
@interface TestView : WDPopBaseView
@property (nonatomic, strong) UIView *view;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, copy) TestViewBlock testViewBlock;
- (void)showView:(TestViewBlock)finished;
@end
NS_ASSUME_NONNULL_END
#import "TestView.h"
@implementation TestView
- (instancetype)init
{
self = [super init];
if (self) {
_view = [[UIView alloc] init];
_view.backgroundColor = [UIColor whiteColor];
_view.layer.cornerRadius = 10;
_view.frame = CGRectMake(30, (CGRectGetHeight(UIScreen.mainScreen.bounds) - 200) / 2, CGRectGetWidth(UIScreen.mainScreen.bounds) - 60, 200);
[self addSubview:_view];
_titleLabel = [[UILabel alloc] init];
_titleLabel.text = @"标题";
_titleLabel.textAlignment = NSTextAlignmentCenter;
_titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightBold];
_titleLabel.frame = CGRectMake(0, 10, CGRectGetWidth(_view.frame), 30);
[_view addSubview:_titleLabel];
_textField = [[UITextField alloc] init];
_textField.placeholder = @"1111";
_textField.borderStyle = UITextBorderStyleRoundedRect;
_textField.frame = CGRectMake(15, 50, CGRectGetWidth(UIScreen.mainScreen.bounds) - 90, 40);
[_view addSubview:_textField];
_button = [[UIButton alloc] init];
_button.backgroundColor = [UIColor redColor];
_button.layer.cornerRadius = 10;
[_button setTitle:@"Sure" forState:UIControlStateNormal];
[_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_button.titleLabel.font = [UIFont systemFontOfSize:15];
_button.frame = CGRectMake(15, CGRectGetMaxY(_textField.frame) + 10, CGRectGetWidth(UIScreen.mainScreen.bounds) - 90, 40);
[_button addTarget:self action:@selector(buttonTargetAction:) forControlEvents:UIControlEventTouchUpInside];
[_view addSubview:_button];
}
return self;
}
- (void)buttonTargetAction:(id)sender {
[self hidenView];
}
- (void)showView:(TestViewBlock)finished {
[self showView];
[self.textField becomeFirstResponder];
self.testViewBlock = finished;
}
- (void)hidenView {
[super hidenView];
if (self.testViewBlock) {
[self.textField resignFirstResponder];
self.testViewBlock(_textField.text);
_textField.text = @"";
}
}
@end
#import "ViewController.h"
#import "TestView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[[TestView defaultView] showView:^(NSString * _Nonnull obj) {
NSLog(@"%@", obj);
}];
}
@end