问题:对tableview进行手势

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


//
// GetCarMapTableExtension.swift
// leocar
//
// Created by CodeRiding on 2018/8/14.
// Copyright © 2018年 com.lcsoft. All rights reserved.
//

import UIKit

extension MXGetCarMapViewChild :UITableViewDelegate{

func initCloseImagev(){
let iv = UIImageView()
iv.frame = CGRect.init(x: 20, y: 40, width: 32, height: 32)
iv.image = UIImage.init(named: "getmap_cancle_icon")
iv.isUserInteractionEnabled = true
closeImageV = iv
addGesture(ss: #selector(tapClose), v: closeImageV)
}

//MARK:手势:点击关闭用车
@objc func tapClose() {
UIView.animate(withDuration: 0.4, animations: {
self.tableView.frame = CGRect.init(x: 0, y: SCREEN_HEIGHT, width: SCREEN_WIDTH, height: SCREEN_HEIGHT)
self.closeImageV.removeFromSuperview()
})

}

func initTableView() {
tableView = UITableView.init(frame: CGRect.init(x: 0, y: SCREEN_HEIGHT-200, width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
tableView.dataSource = getCarDataSourceDelegate
tableView.delegate = self
tableView.isScrollEnabled = false
tableView.showsVerticalScrollIndicator = false
tableView.register(UINib.init(nibName: "CellGetCar", bundle: nil), forCellReuseIdentifier:"CellGetCar")

let g = UISwipeGestureRecognizer(target: self, action: #selector(viewSwipeUp(sender:)))
g.direction = UISwipeGestureRecognizerDirection.up
tableView.addGestureRecognizer(g)

let g2 = UISwipeGestureRecognizer(target: self, action: #selector(viewSwipeDown(sender:)))
g2.direction = UISwipeGestureRecognizerDirection.down
tableView.addGestureRecognizer(g2)

let panGestureRecognizer = UIPanGestureRecognizer(target: self, action:#selector(panGestureRecognizerAction(_:)) )
//tableView.addGestureRecognizer(panGestureRecognizer)

// 默认添加关闭按钮
initCloseImagev()
}

@objc func panGestureRecognizerAction(_ gesture: UIPanGestureRecognizer){
let translation = gesture.translation(in: tableView)
if translation.y < currentY {
print(currentY)
print(translation.y)
}
if (translation.y + currentY ) < 0 {
UIView.animate(withDuration: 0.5, animations: {
self.tableView.frame.origin.y = 0
self.currentY = 0
})

}else{
UIView.animate(withDuration: 0.5, animations: {
self.tableView.frame.origin.y = translation.y + self.currentY
self.currentY = translation.y + self.currentY
})

}


print(translation.y)
print(gesture.state)
if gesture.state == .ended{
let velocity = gesture.velocity(in: tableView)
print(velocity.y)
if velocity.y >= 1100{
self.dismiss(animated: true, completion: nil)
}else{
UIView.animate(withDuration: 0.5, animations: {
//self.tableView.frame.origin = CGPoint(x: 0, y: SCREEN_HEIGHT-translation.y)
})
}
}
}

@objc func viewSwipeUp(sender: UISwipeGestureRecognizer) {
if sender.direction == UISwipeGestureRecognizerDirection.up {
if self.viewGree == 2{
UIView.animate(withDuration: 0.4) {
self.tableView.frame.origin.y = SCREEN_HEIGHT-400
self.viewGree = 1
}
}else if self.viewGree == 1{
UIView.animate(withDuration: 0.4) {
self.tableView.frame.origin.y = 0
self.viewGree = 0
}
}
}

}

@objc func viewSwipeDown(sender: UISwipeGestureRecognizer) {

if sender.direction == UISwipeGestureRecognizerDirection.down {
if self.viewGree == 1{
UIView.animate(withDuration: 0.4) {
self.tableView.frame.origin.y = SCREEN_HEIGHT-200
self.viewGree = 2
}
}else if self.viewGree == 0{
UIView.animate(withDuration: 0.4) {
self.tableView.frame.origin.y = SCREEN_HEIGHT-400
self.viewGree = 1
}
}
}
}

}