mirror of
https://github.com/exyte/Macaw.git
synced 2024-09-11 13:15:35 +03:00
Add transparent view for touches
This commit is contained in:
parent
e290b3df27
commit
9e317d78d9
@ -115,7 +115,6 @@ extension AnimationProducer {
|
||||
// MARK: - Combine animation
|
||||
func addCombineAnimation(_ combineAnimation: Animation, _ context: AnimationContext) {
|
||||
guard let combine = combineAnimation as? CombineAnimation,
|
||||
let renderer = combine.nodeRenderer,
|
||||
let node = combine.node else {
|
||||
return
|
||||
}
|
||||
|
@ -10,6 +10,31 @@ import AppKit
|
||||
/// MacawView is a main class used to embed Macaw scene into your Cocoa UI.
|
||||
/// You could create your own view extended from MacawView with predefined scene.
|
||||
///
|
||||
|
||||
internal class TouchInterceptionView: MView {
|
||||
weak var macawView: MacawView?
|
||||
|
||||
open override func touchesBegan(_ touches: Set<MTouch>, with event: MEvent?) {
|
||||
super.touchesBegan(touches, with: event)
|
||||
macawView?.mTouchesBegan(touches, with: event)
|
||||
}
|
||||
|
||||
open override func touchesMoved(_ touches: Set<MTouch>, with event: MEvent?) {
|
||||
super.touchesMoved(touches, with: event)
|
||||
macawView?.mTouchesMoved(touches, with: event)
|
||||
}
|
||||
|
||||
open override func touchesEnded(_ touches: Set<MTouch>, with event: MEvent?) {
|
||||
super.touchesEnded(touches, with: event)
|
||||
macawView?.mTouchesEnded(touches, with: event)
|
||||
}
|
||||
|
||||
override open func touchesCancelled(_ touches: Set<MTouch>, with event: MEvent?) {
|
||||
super.touchesCancelled(touches, with: event)
|
||||
macawView?.mTouchesCancelled(touches, with: event)
|
||||
}
|
||||
}
|
||||
|
||||
open class MacawView: MView, MGestureRecognizerDelegate {
|
||||
|
||||
/// Scene root node
|
||||
@ -53,6 +78,7 @@ open class MacawView: MView, MGestureRecognizerDelegate {
|
||||
}
|
||||
|
||||
private let placeManager = RootPlaceManager()
|
||||
internal let touchInterceptionView = TouchInterceptionView()
|
||||
|
||||
override open var frame: CGRect {
|
||||
didSet {
|
||||
@ -133,18 +159,36 @@ open class MacawView: MView, MGestureRecognizerDelegate {
|
||||
public override init(frame: CGRect) {
|
||||
super.init(frame: frame)
|
||||
zoom.initialize(view: self, onChange: onZoomChange)
|
||||
|
||||
initializeView()
|
||||
}
|
||||
|
||||
@objc public convenience required init?(coder aDecoder: NSCoder) {
|
||||
self.init(node: Group(), coder: aDecoder)
|
||||
}
|
||||
|
||||
open override func didMoveToWindow() {
|
||||
super.didMoveToWindow()
|
||||
|
||||
initializeView()
|
||||
}
|
||||
|
||||
func initializeView() {
|
||||
self.contentLayout = .none
|
||||
self.context = RenderContext(view: self)
|
||||
|
||||
if let superview = self.superview {
|
||||
self.isUserInteractionEnabled = false
|
||||
touchInterceptionView.isMultipleTouchEnabled = true
|
||||
touchInterceptionView.macawView = self
|
||||
touchInterceptionView.backgroundColor = .clear
|
||||
superview.insertSubview(touchInterceptionView, aboveSubview: self)
|
||||
|
||||
touchInterceptionView.translatesAutoresizingMaskIntoConstraints = false
|
||||
touchInterceptionView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
|
||||
touchInterceptionView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
|
||||
touchInterceptionView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
|
||||
touchInterceptionView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
|
||||
}
|
||||
|
||||
let tapRecognizer = MTapGestureRecognizer(target: self, action: #selector(MacawView.handleTap))
|
||||
let longTapRecognizer = MLongPressGestureRecognizer(target: self, action: #selector(MacawView.handleLongTap(recognizer:)))
|
||||
let panRecognizer = MPanGestureRecognizer(target: self, action: #selector(MacawView.handlePan))
|
||||
@ -163,12 +207,12 @@ open class MacawView: MView, MGestureRecognizerDelegate {
|
||||
rotationRecognizer.cancelsTouchesInView = false
|
||||
pinchRecognizer.cancelsTouchesInView = false
|
||||
|
||||
self.removeGestureRecognizers()
|
||||
self.addGestureRecognizer(tapRecognizer)
|
||||
self.addGestureRecognizer(longTapRecognizer)
|
||||
self.addGestureRecognizer(panRecognizer)
|
||||
self.addGestureRecognizer(rotationRecognizer)
|
||||
self.addGestureRecognizer(pinchRecognizer)
|
||||
touchInterceptionView.removeGestureRecognizers()
|
||||
touchInterceptionView.addGestureRecognizer(tapRecognizer)
|
||||
touchInterceptionView.addGestureRecognizer(longTapRecognizer)
|
||||
touchInterceptionView.addGestureRecognizer(panRecognizer)
|
||||
touchInterceptionView.addGestureRecognizer(rotationRecognizer)
|
||||
touchInterceptionView.addGestureRecognizer(pinchRecognizer)
|
||||
}
|
||||
|
||||
open override func layoutSubviews() {
|
||||
|
@ -16,7 +16,7 @@ import AppKit
|
||||
|
||||
open class MacawZoom {
|
||||
|
||||
private var view: MView!
|
||||
private var view: MacawView!
|
||||
private var onChange: ((Transform) -> Void)!
|
||||
private var touches = [TouchData]()
|
||||
private var zoomData = ZoomData()
|
||||
@ -50,7 +50,7 @@ open class MacawZoom {
|
||||
onChange(zoomData.transform())
|
||||
}
|
||||
|
||||
func initialize(view: MView, onChange: @escaping ((Transform) -> Void)) {
|
||||
func initialize(view: MacawView, onChange: @escaping ((Transform) -> Void)) {
|
||||
self.view = view
|
||||
self.onChange = onChange
|
||||
}
|
||||
@ -96,15 +96,7 @@ open class MacawZoom {
|
||||
let e2 = touches[1].current(in: view)
|
||||
let scale = trackScale ? e1.distance(to: e2) / s1.distance(to: s2) : 1
|
||||
let a = trackRotate ? (e1 - e2).angle() - (s1 - s2).angle() : 0
|
||||
var offset = Size.zero
|
||||
if trackMove {
|
||||
let sina = sin(a)
|
||||
let cosa = cos(a)
|
||||
let w = e1.x - scale * (s1.x * cosa - s1.y * sina)
|
||||
let h = e1.y - scale * (s1.x * sina + s1.y * cosa)
|
||||
offset = Size(w: w, h: h)
|
||||
}
|
||||
return ZoomData(offset: offset, scale: scale, angle: a).combine(with: zoomData)
|
||||
return ZoomData(offset: .zero, scale: scale, angle: a).combine(with: zoomData)
|
||||
}
|
||||
|
||||
}
|
||||
@ -146,7 +138,7 @@ fileprivate class TouchData {
|
||||
let touch: MTouch
|
||||
let point: Point
|
||||
|
||||
convenience init(touch: MTouch, in view: MView) {
|
||||
convenience init(touch: MTouch, in view: MacawView) {
|
||||
self.init(touch: touch, point: touch.applyCurrentLayerTransform(view))
|
||||
}
|
||||
|
||||
@ -155,7 +147,7 @@ fileprivate class TouchData {
|
||||
self.point = point
|
||||
}
|
||||
|
||||
func current(in view: MView) -> Point {
|
||||
func current(in view: MacawView) -> Point {
|
||||
return touch.applyCurrentLayerTransform(view)
|
||||
}
|
||||
|
||||
@ -163,9 +155,7 @@ fileprivate class TouchData {
|
||||
|
||||
extension MTouch {
|
||||
|
||||
func applyCurrentLayerTransform(_ view: MView) -> Point {
|
||||
let layerTransform = CATransform3DGetAffineTransform(view.mLayer!.transform).toMacaw()
|
||||
let location = self.location(in: view).toMacaw()
|
||||
return layerTransform.apply(to: location)
|
||||
func applyCurrentLayerTransform(_ view: MacawView) -> Point {
|
||||
return self.location(in: view.touchInterceptionView).toMacaw()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user