-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from p-x9/feature/uikit
Support UIKit
- Loading branch information
Showing
6 changed files
with
306 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
// | ||
// TouchPointUIView.swift | ||
// | ||
// | ||
// Created by p-x9 on 2023/04/14. | ||
// | ||
// | ||
|
||
#if canImport(UIKit) | ||
import UIKit | ||
|
||
class TouchPointUIView: UIWindow { | ||
var location: CGPoint { | ||
didSet { | ||
let x = String(format: "%.1f", location.x) | ||
let y = String(format: "%.1f", location.y) | ||
locationLabel.text = "x: \(x)\ny: \(y)" | ||
} | ||
} | ||
var radius: CGFloat | ||
|
||
var color: UIColor = .red | ||
|
||
var isBordered: Bool = true | ||
var borderColor: UIColor = .black | ||
var borderWidth: CGFloat = 1 | ||
|
||
var isDropShadow: Bool = true | ||
var shadowColor: UIColor = .black | ||
var shadowRadius: CGFloat = 3 | ||
|
||
var image: UIImage? | ||
|
||
var isShowLocation: Bool = false | ||
|
||
lazy var imageView: UIImageView = { | ||
let view = UIImageView() | ||
view.contentMode = .scaleAspectFit | ||
view.translatesAutoresizingMaskIntoConstraints = false | ||
|
||
return view | ||
}() | ||
|
||
lazy var locationLabel: UILabel = { | ||
let label = UILabel() | ||
label.numberOfLines = 2 | ||
label.font = .systemFont(ofSize: 10) | ||
label.translatesAutoresizingMaskIntoConstraints = false | ||
|
||
return label | ||
}() | ||
|
||
init( | ||
location: CGPoint, | ||
radius: CGFloat, | ||
color: UIColor, | ||
isBordered: Bool, | ||
borderColor: UIColor, | ||
borderWidth: CGFloat, | ||
isDropShadow: Bool, | ||
shadowColor: UIColor, | ||
shadowRadius: CGFloat, | ||
image: UIImage? = nil, | ||
isShowLocation: Bool | ||
) { | ||
self.location = location | ||
self.radius = radius | ||
self.color = color | ||
self.isBordered = isBordered | ||
self.borderColor = borderColor | ||
self.borderWidth = borderWidth | ||
self.isDropShadow = isDropShadow | ||
self.shadowColor = shadowColor | ||
self.shadowRadius = shadowRadius | ||
self.image = image | ||
self.isShowLocation = isShowLocation | ||
|
||
let frame = CGRect(origin: location, | ||
size: .init(width: radius * 2, height: radius * 2)) | ||
super.init(frame: frame) | ||
|
||
setupViews() | ||
setupViewConstraints() | ||
|
||
windowLevel = .statusBar | ||
isUserInteractionEnabled = false | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
func setupViews() { | ||
layer.cornerRadius = radius | ||
backgroundColor = color | ||
|
||
if isBordered { | ||
layer.borderWidth = borderWidth | ||
layer.borderColor = borderColor.cgColor | ||
} | ||
|
||
if isDropShadow { | ||
layer.shadowColor = shadowColor.cgColor | ||
layer.shadowRadius = shadowRadius | ||
} | ||
|
||
if let image { | ||
imageView.image = image | ||
addSubview(imageView) | ||
} | ||
|
||
if isShowLocation { | ||
addSubview(locationLabel) | ||
} | ||
} | ||
|
||
func setupViewConstraints() { | ||
translatesAutoresizingMaskIntoConstraints = false | ||
|
||
NSLayoutConstraint.activate([ | ||
self.heightAnchor.constraint(equalToConstant: radius * 2), | ||
self.widthAnchor.constraint(equalToConstant: radius * 2), | ||
]) | ||
|
||
if let _ = image { | ||
NSLayoutConstraint.activate([ | ||
imageView.topAnchor.constraint(equalTo: topAnchor), | ||
imageView.bottomAnchor.constraint(equalTo: bottomAnchor), | ||
imageView.leftAnchor.constraint(equalTo: leftAnchor), | ||
imageView.rightAnchor.constraint(equalTo: rightAnchor), | ||
]) | ||
} | ||
|
||
if isShowLocation { | ||
NSLayoutConstraint.activate([ | ||
locationLabel.centerXAnchor.constraint(equalTo: centerXAnchor), | ||
locationLabel.bottomAnchor.constraint(equalTo: topAnchor, constant: -shadowRadius) | ||
]) | ||
} | ||
} | ||
|
||
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool { | ||
false | ||
} | ||
} | ||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// | ||
// CocoaTrackPointManager.swift | ||
// | ||
// | ||
// Created by p-x9 on 2023/04/14. | ||
// | ||
// | ||
|
||
#if canImport(UIKit) | ||
import UIKit | ||
|
||
public class TouchTrackingUIView: UIView { | ||
let radius: CGFloat | ||
|
||
var color: UIColor = .red | ||
|
||
var isBordered: Bool = false | ||
var borderColor: UIColor = .black | ||
var borderWidth: CGFloat = 1 | ||
|
||
var isDropShadow: Bool = true | ||
var shadowColor: UIColor = .black | ||
var shadowRadius: CGFloat = 3 | ||
|
||
var image: UIImage? | ||
|
||
var isShowLocation: Bool = false | ||
|
||
|
||
var touches: Set<UITouch> = [] | ||
var locations: [CGPoint] = [] { | ||
didSet { | ||
updatePoints() | ||
} | ||
} | ||
|
||
var pointViews = [TouchPointUIView]() | ||
|
||
public init( | ||
radius: CGFloat = 20, | ||
color: UIColor = .red, | ||
isBordered: Bool = false, | ||
borderColor: UIColor = .black, | ||
borderWidth: CGFloat = 1, | ||
isDropShadow: Bool = true, | ||
shadowColor: UIColor = .black, | ||
shadowRadius: CGFloat = 3, | ||
image: UIImage? = nil, | ||
isShowLocation: Bool = false | ||
) { | ||
self.radius = radius | ||
self.color = color | ||
self.isBordered = isBordered | ||
self.borderColor = borderColor | ||
self.borderWidth = borderWidth | ||
self.isDropShadow = isDropShadow | ||
self.shadowColor = shadowColor | ||
self.shadowRadius = shadowRadius | ||
self.image = image | ||
self.isShowLocation = isShowLocation | ||
|
||
super.init(frame: .null) | ||
|
||
isUserInteractionEnabled = false | ||
|
||
UIWindow.hook() | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
public override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { | ||
self.touches.formUnion(touches) | ||
self.locations = self.touches.map { $0.location(in: self) } | ||
} | ||
|
||
public override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { | ||
self.locations = self.touches.map { $0.location(in: self) } | ||
} | ||
|
||
public override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { | ||
self.touches.subtract(touches) | ||
self.locations = self.touches.map { $0.location(in: self) } | ||
} | ||
|
||
public override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) { | ||
self.touches.subtract(touches) | ||
self.locations = self.touches.map { $0.location(in: self) } | ||
} | ||
|
||
func updatePoints() { | ||
if pointViews.count > touches.count { | ||
pointViews[touches.count..<pointViews.count].forEach { | ||
$0.isHidden = true | ||
$0.windowScene = nil | ||
} | ||
pointViews = Array(pointViews[0..<touches.count]) | ||
} | ||
if pointViews.count < touches.count { | ||
let diff = touches.count - pointViews.count | ||
pointViews += (0..<diff).map { _ in | ||
TouchPointUIView( | ||
location: .zero, | ||
radius: radius, | ||
color: color, | ||
isBordered: isBordered, | ||
borderColor: borderColor, | ||
borderWidth: borderWidth, | ||
isDropShadow: isDropShadow, | ||
shadowColor: shadowColor, | ||
shadowRadius: shadowRadius, | ||
isShowLocation: isShowLocation | ||
) | ||
} | ||
} | ||
|
||
zip(pointViews, locations).forEach { view, location in | ||
view.location = location | ||
view.center = location | ||
view.makeKeyAndVisible() | ||
} | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters