-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAGFloatCard.swift
451 lines (373 loc) · 17.5 KB
/
AGFloatCard.swift
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//
// AGFloatCard.swift
//
// Created by Александр Зверь on 27.01.16.
// Copyright © 2016 ppApp. All rights reserved.
//
import UIKit
// Позиция карточки
enum AGFloatCardLocation {
case Unknown
case TopLeft
case TopRight
case BottomLeft
case BottomRight
}
// Протокол
protocol AGFloatCardDelegate {
// По карточке кликнули
func didClickingOnCard(card: AGFloatCard)
// Карточка начинает менять позицию
func willChangeCardPosition(card: AGFloatCard, offset: CGFloat)
// Карточка начинает возвращаться в исходное положение
func willReturnCardToCenterSubview(card: AGFloatCard, fromLocation: AGFloatCardLocation)
// Карточка вернулась в исходное положеие
func didReturnCardToCenterSubview(card: AGFloatCard, fromLocation: AGFloatCardLocation)
// Карточка начинает уходить за пределы экрана
func willCardGoOffscreenSubview(card: AGFloatCard, fromLocation: AGFloatCardLocation)
// Карточка ушла за пределы экрана
func didCardGoOffscreenSubview(card: AGFloatCard, fromLocation: AGFloatCardLocation)
}
class AGFloatCard: UIView, CAAnimationDelegate {
// Константы
private let defSwipeDistance: CGFloat = 90.0
private let defRotationAngle: CGFloat = 9.0
private let defDelayAnimation = 0.4
private let defRasterization = true
// Для отслеживания перемещения карточки
private var lastLocation: CGPoint = CGPoint.zero
private var startLocation: CGPoint = CGPoint.zero
private var savePosition: CGPoint = CGPoint.zero
private var velocity: CGPoint = CGPoint.zero
private var slideFactor: CGFloat = 0.0
private var rotation: CGFloat = 0.0
// Флаг нажатия сверху карточки
private var topTap: Bool = false
private var numTouches: Int = 0
// Флаг нахождения вне зоны видимости на экране
private var offscreen: Bool = false
func isOffscreen()->Bool {return self.offscreen}
// Делегат
var delegate: AGFloatCardDelegate?
static var likeImageName: String = "CardLike"
static var dislikeImageName: String = "CardDislike"
// Like&Dislike ImageView
let likeDislikeEnable: Bool
var likeImgView: UIImageView?
var dislikeImgView: UIImageView?
// Пользовательские данные
var userData: Any? = nil
// Верстка из nib/xib файла
func makeupFromNib(nibName name: String) {
self.autoresizesSubviews = true
self.translatesAutoresizingMaskIntoConstraints = true
if let view = UINib(nibName: name, bundle: nil).instantiate(withOwner: nil, options: nil)[0] as? UIView {
view.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height)
view.bounds = self.bounds
view.autoresizesSubviews = true
view.translatesAutoresizingMaskIntoConstraints = true
self.addSubview(view)
view.center = CGPoint(x: self.bounds.midX, y: self.bounds.midY)
view.autoresizingMask = [UIViewAutoresizing.flexibleLeftMargin, UIViewAutoresizing.flexibleRightMargin, UIViewAutoresizing.flexibleTopMargin, UIViewAutoresizing.flexibleBottomMargin]
}
}
// Инициализация
init(nibName name: String, frame cgFrame: CGRect, bkgColor: UIColor, likeInit: Bool = true) {
likeDislikeEnable = likeInit
super.init(frame: cgFrame)
self.initCard()
self.makeupFromNib(nibName: name)
if likeImgView != nil {
self.bringSubview(toFront: likeImgView!)
}
if dislikeImgView != nil {
self.bringSubview(toFront: dislikeImgView!)
}
self.backgroundColor = bkgColor
}
init(frame cgFrame: CGRect, bkgColor: UIColor, likeInit: Bool = true) {
likeDislikeEnable = likeInit
super.init(frame: cgFrame)
self.initCard()
self.backgroundColor = bkgColor
}
override init(frame cgFrame: CGRect) {
likeDislikeEnable = false
super.init(frame: cgFrame)
self.initCard()
}
required init?(coder aDecoder: NSCoder) {
likeDislikeEnable = false
super.init(coder: aDecoder)
self.initCard()
}
// Инициализация карточки
private func initCard()
{
backgroundColor = UIColor.white
layer.cornerRadius = 8.0
// Создание тени
layer.shadowColor = UIColor.black.cgColor
layer.shadowRadius = 3.0
layer.shadowOpacity = 0.3334
layer.cornerRadius = 8.0
layer.shadowOffset = CGSize(width: 0, height: 1)
layer.shadowPath = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: layer.frame.width, height: layer.frame.height), cornerRadius: 8.0).cgPath
if(defRasterization)
{
layer.shouldRasterize = true
layer.rasterizationScale = UIScreen.main.scale
}
layer.transform = CATransform3DIdentity
// Отслеживаем перемещение
let pan = UIPanGestureRecognizer(target: self, action: #selector(AGFloatCard.handlePan(recognizer:)))
addGestureRecognizer(pan)
// Отслеживаем нажатия
let tap = UITapGestureRecognizer(target: self, action:#selector(AGFloatCard.handleTap(recognizer:)))
addGestureRecognizer(tap)
// Инициализация Like&Dislike
if(likeDislikeEnable){initLikeDislike()}
}
private func initLikeDislike()
{
var k: CGFloat = 0.0
let width: CGFloat = self.frame.size.width*0.6
likeImgView = UIImageView(image: UIImage(named: AGFloatCard.likeImageName))
if(likeImgView != nil)
{
likeImgView?.layer.opacity = 0.0
k = likeImgView!.frame.size.height/likeImgView!.frame.size.width
if(k.isNaN || k == 0) {
k = 0.9883
}
likeImgView!.frame = CGRect(x: (self.frame.size.width-width)*0.5, y: (self.frame.size.height-(width*k))*0.5, width: width, height: width*k)
addSubview(likeImgView!)
}
dislikeImgView = UIImageView(image: UIImage(named: AGFloatCard.dislikeImageName))
if(dislikeImgView != nil)
{
dislikeImgView?.layer.opacity = 0.0
k = dislikeImgView!.frame.size.height/dislikeImgView!.frame.size.width
if(k.isNaN || k == 0){
k = 0.9883
}
dislikeImgView!.frame = CGRect(x: (self.frame.size.width-width)*0.5, y: (self.frame.size.height-(width*k))*0.5, width: width, height: width*k)
addSubview(dislikeImgView!)
}
}
func resetMove() {
self.layer.position = self.savePosition
self.layer.transform = CATransform3DMakeRotation(0.0, 0.0, 0.0, 1.0)
}
// Взаимодействие с карточкой (перемещение)
@IBAction func handlePan(recognizer:UIPanGestureRecognizer)
{
let location = recognizer.location(in: superview)
if(recognizer.state == UIGestureRecognizerState.began)
{
savePosition = layer.position
startLocation = location
topTap = (recognizer.location(in: self).y <= savePosition.y)
}
else if(recognizer.state == UIGestureRecognizerState.changed)
{
if(numTouches != recognizer.numberOfTouches)
{
lastLocation = location
numTouches = recognizer.numberOfTouches
}
layer.position.x -= (lastLocation.x-location.x)
layer.position.y -= (lastLocation.y-location.y)
self.rotation = (defRotationAngle)*(-layer.position.x/savePosition.x+1)
if(self.rotation>0.0){self.rotation = min(self.rotation, self.defRotationAngle)}
else{self.rotation = max(self.rotation, -self.defRotationAngle)}
if(self.topTap){self.self.rotation = -self.rotation;}
self.layer.transform = CATransform3DMakeRotation(self.rotation*CGFloat(M_PI)/180.0, 0.0, 0.0, 1.0)
lastLocation = location
// Рассчитываем дельту
var delta:Float = Float(savePosition.x-layer.position.x)
if(delta<0)
{
delta = (-delta/Float(defSwipeDistance) > 1 ? 1 : -delta/Float(defSwipeDistance))
// Показываем LIKE и скрываем DISLIKE
if(likeImgView != nil){likeImgView?.layer.opacity = 0.85*delta}
if(dislikeImgView != nil){dislikeImgView?.layer.opacity = 0.0}
}
else
{
delta = (delta/Float(defSwipeDistance) > 1 ? 1 : delta/Float(defSwipeDistance))
// Показываем DISLIKE и скрываем LIKE
if(likeImgView != nil){likeImgView?.layer.opacity = 0.0}
if(dislikeImgView != nil){dislikeImgView?.layer.opacity = 0.85*delta}
}
if(self.delegate != nil) {
self.delegate?.willChangeCardPosition(card: self, offset: CGFloat(delta))
}
}
else if(recognizer.state == UIGestureRecognizerState.ended)
{
self.numTouches = 0
//------------------
self.velocity = recognizer.velocity(in: superview)
self.slideFactor = 0.0025 * (sqrt((velocity.x * velocity.x) + (velocity.y * velocity.y))/200.0)
//------------------
self.playAnimationFly(toPosition: CGPoint(x: layer.position.x+(self.velocity.x*self.slideFactor),
y: layer.position.y+(self.velocity.y*self.slideFactor)), duration: Double(self.slideFactor)*1.85);
}
lastLocation = location
}
// MARK: Animation
private func playAnimationFly(toPosition:CGPoint, duration: CFTimeInterval) {
let fly:CABasicAnimation = CABasicAnimation(keyPath: "position")
fly.fromValue = NSValue(cgPoint: self.layer.position)
fly.toValue = NSValue(cgPoint: toPosition)
fly.duration = duration
fly.delegate = self
fly.setValue("fly", forKey: "id")
// Запускаем анимацию
self.layer.removeAllAnimations()
self.layer.add(fly, forKey: "fly")
self.layer.position = toPosition
}
private func playAnimationToCenter(duration:CFTimeInterval) {
let group: CAAnimationGroup = CAAnimationGroup()
group.duration = duration
let rotate:CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
rotate.fromValue = self.rotation*CGFloat(M_PI)/180.0
rotate.toValue = 0.0
rotate.duration = duration
let fly:CABasicAnimation = CABasicAnimation(keyPath: "position")
fly.fromValue = NSValue(cgPoint: self.layer.position)
fly.toValue = NSValue(cgPoint: self.savePosition)
fly.duration = duration
group.animations = [rotate, fly]
group.delegate = self
group.setValue("center", forKey: "id")
// Запускаем анимацию
self.layer.add(group, forKey: "center")
self.layer.position = self.savePosition
self.layer.transform = CATransform3DMakeRotation(0, 0, 0, 1.0)
if(self.likeImgView != nil) {
if(Double((self.likeImgView?.layer.opacity)!) > 0.0){
let alphaLike:CABasicAnimation = CABasicAnimation(keyPath: "opacity")
alphaLike.fromValue = self.likeImgView?.layer.opacity
alphaLike.toValue = 0.0
alphaLike.duration = duration
self.likeImgView?.layer.add(alphaLike, forKey: "likeOpacityReset")
self.likeImgView?.layer.opacity = 0.0
}
}
if(self.dislikeImgView != nil) {
if(Double((self.dislikeImgView?.layer.opacity)!) > 0.0) {
let alphaDisLike:CABasicAnimation = CABasicAnimation(keyPath: "opacity")
alphaDisLike.fromValue = self.dislikeImgView?.layer.opacity
alphaDisLike.toValue = 0.0
alphaDisLike.duration = duration
self.dislikeImgView?.layer.add(alphaDisLike, forKey: "dislikeOpacityReset")
self.dislikeImgView?.layer.opacity = 0.0
}
}
}
private func playAnimationToEnd(duration:CFTimeInterval)
{
let end:CABasicAnimation = CABasicAnimation(keyPath: "position")
if(self.superview != nil) {
let toPosition = CGPoint(x:((self.savePosition.x-self.layer.position.x) > 0 ? -(self.superview?.frame.origin.x)!-self.bounds.size.width : (self.superview?.frame.size.width)!+self.bounds.size.width), y: self.layer.position.y+(self.velocity.y*self.slideFactor))
end.toValue = NSValue(cgPoint: toPosition)
} else {
end.toValue = NSValue(cgPoint: self.layer.position)
}
end.fromValue = NSValue(cgPoint: self.layer.position)
end.duration = duration
end.delegate = self
end.setValue("end", forKey: "id")
// Запускаем анимацию
self.layer.add(end, forKey: "end")
self.layer.position = (end.toValue as! NSValue).cgPointValue
}
func animationDidStop(_ anim: CAAnimation, finished flag: Bool)
{
if let key = anim.value(forKey: "id") as? String {
if(key == "fly")
{
if(CGFloat(labs(Int(self.savePosition.x-self.layer.position.x))) < self.defSwipeDistance)
{
if(self.delegate != nil) {
self.delegate?.willReturnCardToCenterSubview(card: self, fromLocation: getCardLocation(point: self.layer.position))
}
self.playAnimationToCenter(duration: self.defDelayAnimation*0.5)
} else {
if(self.delegate != nil) {
self.delegate?.willCardGoOffscreenSubview(card: self, fromLocation: getCardLocation(point: self.layer.position))
}
self.playAnimationToEnd(duration: self.defDelayAnimation*0.5)
}
} else if(key == "center") {
if(self.delegate != nil) {
self.delegate?.didReturnCardToCenterSubview(card: self, fromLocation: getCardLocation(point: self.layer.position))
}
} else if(key == "end"){
if(self.delegate != nil) {
self.delegate?.didCardGoOffscreenSubview(card: self, fromLocation: getCardLocation(point: self.layer.position))
}
}
}
}
// Взаимодействие с карточкой (нажатие)
@IBAction func handleTap(recognizer:UITapGestureRecognizer)
{
if(self.delegate != nil) {
self.delegate?.didClickingOnCard(card: self)
}
}
// Сброс прозрачности элементов Like&Dislike до 0.0
func resetLikeAndDislikeOpacity()
{
if(likeImgView != nil){likeImgView?.alpha = 0.0}
if(dislikeImgView != nil){dislikeImgView?.alpha = 0.0}
}
// Установка трансформаций (для стека и его красивых анимаций)
func setTransforms(scale: CGFloat, i: Int, offset: CGFloat, listOffset: CGFloat, resetOpacity: Bool, a: CGFloat)
{
// Установка транформации вращения и масштаба
transform = CGAffineTransform(rotationAngle: 0)
layer.transform.m11 = scale
layer.transform.m22 = scale
if(superview != nil){
layer.position.y = superview!.center.y+(CGFloat(i)*listOffset)+(listOffset*offset)+(layer.frame.size.height*(CGFloat(i)+offset)*listOffset*0.002)
}
if(a>=0.0)
{self.alpha = a}
if(resetOpacity)
{resetLikeAndDislikeOpacity()}
}
// Получение локации по позиции
func getCardLocation(point:CGPoint)->AGFloatCardLocation
{
if((superview) != nil)
{
let middleX:CGFloat = (superview?.frame.size.width)!/2;
let middleY:CGFloat = (superview?.frame.size.height)!/2;
if(point.x<middleX){
if(point.y<middleY){
return AGFloatCardLocation.TopLeft
}
return AGFloatCardLocation.BottomLeft
}
else
{
if(point.y<middleY){
return AGFloatCardLocation.TopRight
}
return AGFloatCardLocation.BottomRight
}
}
return AGFloatCardLocation.Unknown
}
//MARK: DEINIT
deinit {
if userData != nil {
userData = nil
}
}
}