/
YJTouchView.swift
66 lines (58 loc) · 2.06 KB
/
YJTouchView.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
//
// YJTouchView.swift
// YJCocoa
//
// HomePage:https://github.com/937447974/YJCocoa
// YJ技术支持群:557445088
//
// Created by 阳君 on 2019/9/10.
// Copyright © 2016-现在 YJCocoa. All rights reserved.
//
import UIKit
/// 扩展触摸的最大区域
public var YJTouchMax: CGFloat = 44
extension UIView {
func hitTestTouch(_ point: CGPoint) -> CGPoint {
var point = point
if !self.frame.contains(point) {
let max: CGFloat = YJTouchMax
let width = self.frame.width > max ? self.frame.width : max
let height = self.frame.height > max ? self.frame.height : max
let newFrame = CGRect(x: (self.frame.width - width)/2, y: (self.frame.height - height)/2, width: width, height: height)
if newFrame.contains(point) {
point = CGPoint(x: newFrame.midX, y: newFrame.midY)
}
}
return point
}
}
/** 扩大触摸区域的UIView*/
open class YJTouchView: UIView {
open override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
return super.hitTest(self.hitTestTouch(point), with: event)
}
}
/** 扩大触摸区域的UIImageView*/
open class YJTouchImageView: UIImageView {
open override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
return super.hitTest(self.hitTestTouch(point), with: event)
}
}
/** 扩大触摸区域的UILabel*/
open class YJTouchLabel: UILabel {
open override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
return super.hitTest(self.hitTestTouch(point), with: event)
}
}
/** 扩大触摸区域的UIButton*/
open class YJTouchButton: UIButton {
open override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
return super.hitTest(self.hitTestTouch(point), with: event)
}
}
/** 扩大触摸区域的UITextView*/
open class YJTouchTextView: UITextView {
open override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
return super.hitTest(self.hitTestTouch(point), with: event)
}
}