实现效果如图
封装工具MyPopTool.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface MyPopTool : NSObject
+ (instancetype)sharedInstance;
- (void)popView:(UIView *)view animated:(BOOL)animated;
- (void)closeAnimated:(BOOL)animated;
@end
MyPopTool.m
#import "MyPopTool.h"
@interface MyPopTool ()
@property (nonatomic, strong) UIView *currentView;
@end
@implementation MyPopTool
+ (instancetype)sharedInstance {
static MyPopTool *_popTool = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_popTool = [[MyPopTool alloc] init];
});
return _popTool;
}
- (void)popView:(UIView *)view animated:(BOOL)animated {
_currentView = view;
CGFloat halfScreenWidth = [[UIScreen mainScreen] bounds].size.width * 0.5;
CGFloat halfScreenHeight = [[UIScreen mainScreen] bounds].size.height * 0.5;
// 屏幕中心
CGPoint screenCenter = CGPointMake(halfScreenWidth, halfScreenHeight);
view.center = screenCenter;
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
[keyWindow addSubview:view];
if (animated) {
// 将view宽高缩至无限小(点)
view.transform = CGAffineTransformScale(CGAffineTransformIdentity, CGFLOAT_MIN, CGFLOAT_MIN);
[UIView animateWithDuration:0.3 animations:^{
// 以动画的形式将view慢慢放大至原始大小的1.2倍
view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.2, 1.2);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.2 animations:^{
// 以动画的形式将view恢复至原始大小
view.transform = CGAffineTransformIdentity;
}];
}];
}
}
- (void)closeAnimated:(BOOL)animated {
if (animated) {
[UIView animateWithDuration:0.2 animations:^{
_currentView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.2, 1.2);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.3 animations:^{
_currentView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
} completion:^(BOOL finished) {
[_currentView removeFromSuperview];
}];
}];
} else {
[_currentView removeFromSuperview];
}
}
具体用法如下:
- (UIView *)popOutView{
if (!_popOutView) {
_popOutView = [[UIView alloc] initWithFrame:self.view.bounds];
_popOutView.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.400];
_popOutView.alpha = 0.9f;
UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH_NO_STATUS * 0.5 - 75, SCREEN_HEIGHT_NO_STATUS * 0.5 - 100, 150, 200)];
bgView.layer.masksToBounds = YES;
bgView.layer.cornerRadius = 5.0 ;
bgView.backgroundColor = [UIColor whiteColor];
[_popOutView addSubview:bgView];
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0 , 0, 150, 150)];
img.image = [UIImage imageNamed:@"ubit_sucess"];
[bgView addSubview:img];
UILabel *prompt = [[UILabel alloc] initWithFrame:CGRectMake(0, 150, 150, 50)];
prompt.numberOfLines = 0;
prompt.textColor = [UIColor orangeColor];
prompt.textAlignment = NSTextAlignmentCenter;
self.promptLabel = prompt;
[bgView addSubview:prompt];
}
return _popOutView;
}
[[MyPopTool sharedInstance] popView:self.popOutView animated:YES];
[[MyPopTool sharedInstance] closeAnimated:NO];