forked from Zero-zhou/iOS-Extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIView+Event.swift
124 lines (100 loc) · 5.02 KB
/
UIView+Event.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
//
// UIView+Event.swift
//
// Created by Augus on 6/28/16.
// Copyright © 2016 iAugus. All rights reserved.
//
import UIKit
// not cmpleted yet, do not use this
private var initialView: UIView?
private var startTouchPosition1: CGPoint?
private var startTouchPosition2: CGPoint?
private var previousTouchPosition1: CGPoint?
private var previousTouchPosition2: CGPoint?
private var currentTouchPosition1: CGPoint?
private var currentTouchPosition2: CGPoint?
private var startTouchTime: TimeInterval!
private let ZOOM_DRAG_MIN: CGFloat = 10
private let SWIPE_DRAG_HORIZ_MIN: CGFloat = 10
extension UIView {
func detectTouchEvent(_ event: UIEvent?) {
guard let event = event else { return }
guard let allTouches = event.allTouches else { return }
guard let touch = allTouches.first else { return }
let touchView = touch.view
if touch.phase == .began {
initialView = touchView
startTouchPosition1 = touch.location(in: self)
startTouchTime = touch.timestamp
if allTouches.count > 1 {
startTouchPosition2 = allTouches.first?.location(in: self)
previousTouchPosition1 = startTouchPosition1
previousTouchPosition2 = startTouchPosition2
}
}
guard let previousTouchPosition1 = previousTouchPosition1, let previousTouchPosition2 = previousTouchPosition2 else { return }
if touch.phase == .moved {
if allTouches.count > 1 {
guard let currentTouchPosition1 = allTouches.object(0)?.location(in: self), let currentTouchPosition2 = allTouches.object(1)?.location(in: self) else { return }
let currentFingerDistance = CGDistance(currentTouchPosition1, currentTouchPosition2)
let previousFingerDistance = CGDistance(previousTouchPosition1, previousTouchPosition2)
if fabs(currentFingerDistance - previousFingerDistance) > ZOOM_DRAG_MIN {
let movedDistance: Int = Int(currentFingerDistance - previousFingerDistance)
if currentFingerDistance > previousFingerDistance {
print("zoom in")
NotificationCenter.default.post(name: NSNotification.Name.NOTIFICATION_ZOOM_IN, object: movedDistance)
}
else {
print("zoom out")
NotificationCenter.default.post(name: NSNotification.Name.NOTIFICATION_ZOOM_OUT, object: movedDistance)
}
}
}
}
guard let _startTouchPosition1 = startTouchPosition1, let _ = startTouchPosition2 else { return }
guard let startTouchTime = startTouchTime else { return }
if touch.phase == .ended {
let currentTouchPosition = touch.location(in: self)
// Check if it's a swipe
if fabsf(Float(_startTouchPosition1.x - currentTouchPosition.x)) >= Float(SWIPE_DRAG_HORIZ_MIN) &&
fabsf(Float(_startTouchPosition1.x - currentTouchPosition.x)) > fabsf(Float(_startTouchPosition1.y - currentTouchPosition.y)) &&
touch.timestamp - startTouchTime < 0.7 {
// It appears to be a swipe.
if _startTouchPosition1.x < currentTouchPosition.x {
NSLog("swipe right")
NotificationCenter.default.post(name: NSNotification.Name.NOTIFICATION_SWIPE_RIGHT, object: self)
}
else {
NSLog("swipe left")
NotificationCenter.default.post(name: NSNotification.Name.NOTIFICATION_SWIPE_LEFT
, object: self)
}
}
else {
//-- else, check if it's a single touch
if touch.tapCount == 1 {
let uInfo: [NSObject : AnyObject] = [
"touch" as NSObject : touch
]
NotificationCenter.default.post(name: NSNotification.Name.NOTIFICATION_TAP, object: self, userInfo: uInfo)
}
/* else if (touch.tapCount > 1) {
handle multi-touch
}
*/
}
startTouchPosition1 = CGPoint(x: -1, y: -1)
initialView = nil
}
if touch.phase == .cancelled {
initialView = nil
}
}
}
extension NSNotification.Name {
static let NOTIFICATION_TAP = NSNotification.Name(rawValue: "NOTIFICATION_TAP")
static let NOTIFICATION_ZOOM_IN = NSNotification.Name(rawValue: "NOTIFICATION_ZOOM_IN")
static let NOTIFICATION_ZOOM_OUT = NSNotification.Name(rawValue: "NOTIFICATION_ZOOM_OUT")
static let NOTIFICATION_SWIPE_LEFT = NSNotification.Name(rawValue: "NOTIFICATION_SWIPE_LEFT")
static let NOTIFICATION_SWIPE_RIGHT = NSNotification.Name(rawValue: "NOTIFICATION_SWIPE_RIGHT")
}