CNMainView.swift
6.83 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//
// CNMainView.swift
// metaCode
//
// Created by daojian on 2023/5/31.
//
import Foundation
import YYCategories
typealias closureBlock = ()->Void;
///正面View
class CNFrontMainView : UIView{
///点击反转事件
var tapTransformAciton:closureBlock?
override init(frame: CGRect) {
super.init(frame: frame)
self.initUI()
let tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapAction))
self.addGestureRecognizer(tap)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
lazy var imageView:UIImageView = {
let tempImageView:UIImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: self.width, height: self.width * 0.7))
tempImageView.image = UIImage(named: "homePage_zhongguorenming_bank")
let path : UIBezierPath = UIBezierPath.init(roundedRect: tempImageView.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: 20, height: 20))
let maskLayer = CAShapeLayer()
maskLayer.frame = tempImageView.bounds
maskLayer.path = path.cgPath
tempImageView.layer.mask = maskLayer
return tempImageView
}()
lazy var contentView : UIView = {
let contentView = UIView.init(frame: CGRect(x: 0, y: self.imageView.bottom - 20, width: self.width, height: self.height - self.imageView.bottom + 20))
contentView.backgroundColor = UIColor.white
contentView.clipsToBounds = true
contentView.layer.cornerRadius = 20
let imageView = UIImageView.init(frame: CGRect(x: 10, y: 10, width: contentView.width - 20, height: contentView.height - 20))
imageView.image = UIImage.init(named: "homePage_mainView_front");
contentView.addSubview(imageView)
return contentView
}()
func initUI()
{
self.addSubview(self.imageView)
self.addSubview(self.contentView)
}
@objc func tapAction(){
if (self.tapTransformAciton != nil){
self.tapTransformAciton
}
}
}
///反面View
class CNBackMainView : UIView{
///点击反转事件
var tapTransformAciton:closureBlock?
override init(frame: CGRect) {
super.init(frame: frame)
self.initUI()
let tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapAction))
self.addGestureRecognizer(tap)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
lazy var imageView:UIImageView = {
let tempImageView:UIImageView = UIImageView(frame: CGRect(x: 10, y: 10, width: self.width-20, height: self.height - 20))
tempImageView.image = UIImage(named: "homePage_backViewImage")
return tempImageView
}()
lazy var contentView : UIView = {
let contentView = UIView.init(frame: CGRect(x: 0, y: self.imageView.bottom - 20, width: self.width, height: self.height-self.imageView.bottom + 20))
contentView.backgroundColor = UIColor.white
contentView.clipsToBounds = true
contentView.layer.cornerRadius = 20
return contentView
}()
func initUI()
{
self.addSubview(self.imageView)
self.addSubview(self.contentView)
}
@objc func tapAction(){
if (self.tapTransformAciton != nil){
self.tapTransformAciton
}
}
}
///旋转动画时间
let KTransformAnimationTime:CGFloat = 0.75;
///中间主VIew
class CNMainView : UIView{
override init(frame: CGRect) {
super.init(frame: frame)
self.initUI()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
lazy var frontView:CNFrontMainView = {
// _frontView = [[CNFrontMainView alloc] initWithFrame:self.bounds];
// __weak CNMainView *weakSelf = self;
// _frontView.tapTransformAciton = ^{
// [weakSelf transformToBacnkView];
// };
let frontView = CNFrontMainView.init(frame: self.bounds)
frontView.tapTransformAciton = {
print("点击了")
}
return frontView
}()
lazy var backView:CNBackMainView = {
let backView = CNBackMainView.init(frame: self.bounds)
backView.tapTransformAciton = {
print("点击了")
}
return backView
}()
func initUI()
{
self.addSubview(self.frontView)
self.addSubview(self.backView)
self.backView.isHidden = true;
self.layer.cornerRadius = 20;
self.layer.shadowOpacity = 1;
self.layer.shadowColor = UIColor("#eeeeee")?.cgColor
self.layer.shadowOffset = CGSize(width: 10, height: 10)
self.layer.shadowRadius = 10;
}
/***********事件*****************/
///旋转到反面
func transformToBacnkView()
{
//旋转动画
UIView.animate(withDuration: KTransformAnimationTime) {
self.layer.transform = CATransform3DMakeRotation(.pi, 0, 1, 0);
}
//让背面也旋转180度
self.backView.layer.transform = CATransform3DMakeRotation(.pi, 0, 1, 0);
//在中间时刻进行正反面view的隐藏显示处理
let deadline = DispatchTime.now() - DispatchTimeInterval.milliseconds(50) + DispatchTimeInterval.milliseconds(Int (KTransformAnimationTime*1000/2))
DispatchQueue.main.asyncAfter(deadline: deadline, execute: DispatchWorkItem(block: {
UIView.animate(withDuration: 0.05) {
self.frontView.isHidden = true;
self.backView.isHidden = false;
}
}))
}
///旋转到正面
func transformToFrontView()
{
//旋转动画
UIView.animate(withDuration: KTransformAnimationTime) {
self.layer.transform = CATransform3DMakeRotation(0, 0, 1, 0);
} completion: { _ in
//背面恢复旋转
self.backView.layer.transform = CATransform3DMakeRotation(0, 0, 1, 0);
}
//在中间时刻进行正反面view的隐藏显示处理
let deadline = DispatchTime.now() - DispatchTimeInterval.milliseconds(50) + DispatchTimeInterval.milliseconds(Int (KTransformAnimationTime*1000/2))
DispatchQueue.main.asyncAfter(deadline: deadline, execute: DispatchWorkItem(block: {
UIView.animate(withDuration: 0.05) {
self.frontView.isHidden = false;
self.backView.isHidden = true;
}
}))
}
///缩放
open func scaledView(scale:CGFloat)
{
self.frontView.imageView.transform = CGAffineTransformMakeScale(scale, scale);
}
}