1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//
// GMPopupBgView.m
// Gengmei
//
// Created by wangyang on 2018/3/2.
// Copyright © 2018年 更美互动信息科技有限公司. All rights reserved.
//
#import "GMPopupBgView.h"
#import <GMKit/Constant.h>
#import "UIView+Layout.h"
//#import <GMKit/GMView.h>
//@import GMKit;
//#import <GMKit/GMki>
@interface GMPopupBgView () <UIGestureRecognizerDelegate>
@end
@implementation GMPopupBgView
- (void)setup {
[super setup];
self.frame = CGRectMake(0, 0, MAINSCREEN_WIDTH, MAINSCREEN_HEIGHT);
self.animationDuration = 0.3;
self.animationType = GMPopupAnimationTypeFlipFromBottom;
self.bgAlphaColor = [UIColor colorWithWhite:0 alpha:0.6];
self.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapView)];
tap.delegate = self;
[self addGestureRecognizer:tap];
tap.delegate = self;
self.userInteractionEnabled = YES;
_container = [[UIView alloc] initWithFrame:CGRectMake(0, MAINSCREEN_HEIGHT, MAINSCREEN_WIDTH, 322)];
_container.backgroundColor = [UIColor whiteColor];
[self addSubview:_container];
}
- (void)didTapView {
[self hide];
if ([self.delegate respondsToSelector:@selector(popupBgViewDidDismiss)]) {
[self.delegate popupBgViewDidDismiss];
}
}
- (void)willMoveToSuperview:(UIView *)newSuperview {
if (newSuperview == nil) {
return;
}
switch (self.animationType) {
case GMPopupAnimationTypeNone: {
self.backgroundColor = self.bgAlphaColor;
break;
}
case GMPopupAnimationTypeFade: {
self.backgroundColor = self.bgAlphaColor;
self.container.alpha = 0;
[self animation:^{
self.container.alpha = 1;
}];
break;
}
case GMPopupAnimationTypeFlipFromBottom: {
[self animation:^{
self.container.bottom = MAINSCREEN_HEIGHT;
self.backgroundColor = self.bgAlphaColor;
}];
break;
}
case GMPopupAnimationTypeSpringScaleFromCenter: {
[self animation:^{
self.backgroundColor = self.bgAlphaColor;
}];
self.container.transform = CGAffineTransformMakeScale(0.7, 0.7);
[UIView animateWithDuration:self.animationDuration delay:0 usingSpringWithDamping:0.3 initialSpringVelocity:0 options:0 animations:^{
// 放大
self.container.transform = CGAffineTransformMakeScale(1, 1);
} completion:nil];
break;
}
case GMPopupAnimationTypeCustom: {
[self animation:^{
self.backgroundColor = self.bgAlphaColor;
}];
self.containerShowAnimation(self, self.animationDuration);
break;
}
default:
break;
}
}
- (void)hide {
[UIView animateWithDuration:self.animationDuration animations:^{
self.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
switch (self.animationType) {
case GMPopupAnimationTypeNone:
case GMPopupAnimationTypeFade: {
[self animation:^{
self.container.alpha = 0;
}];
break;
}
case GMPopupAnimationTypeFlipFromBottom: {
[self animation:^{
self.container.top = MAINSCREEN_HEIGHT;
}];
break;
}
case GMPopupAnimationTypeSpringScaleFromCenter: {
[self animation:^{
self.container.alpha = 0;
}];
break;
}
case GMPopupAnimationTypeCustom: {
self.containerHideAnimation(self, self.animationDuration);
break;
}
default:
break;
}
}
- (void)animation:(void (^)(void))block {
[UIView animateWithDuration:self.animationDuration animations:^{
block();
} completion:^(BOOL finished) {
if ([self.delegate respondsToSelector:@selector(didCompletedAnimation)]) {
[self.delegate didCompletedAnimation];
}
}];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch*)touch {
// 如果点击了container区域, 不需要响应手势
if (CGRectContainsPoint(self.container.frame, [touch locationInView:self])) {
return NO;
} else if (self.disableCancelTouch) {
return NO;
} else {
return YES;
}
}
@end