From d9a5b27e94972757da4a088d338fd945f78b9e02 Mon Sep 17 00:00:00 2001 From: Alisa Mylnikova Date: Fri, 14 Feb 2020 14:13:53 +0700 Subject: [PATCH] Add correct zoom-scroll handling --- .../AnimationsHierarchyViewController.swift | 28 ++++++++++++++++++- .../Cache/AnimationCache.swift | 2 +- .../MorphingGenerator.swift | 2 +- .../OpacityGenerator.swift | 2 +- .../ShapeAnimationGenerator.swift | 2 +- .../TransformGenerator.swift | 2 +- Source/views/MacawView.swift | 13 +++++---- 7 files changed, 39 insertions(+), 12 deletions(-) diff --git a/Example/Example/Examples/AnimationsHierarchy/AnimationsHierarchyViewController.swift b/Example/Example/Examples/AnimationsHierarchy/AnimationsHierarchyViewController.swift index 394c4ebc..e1f554d3 100644 --- a/Example/Example/Examples/AnimationsHierarchy/AnimationsHierarchyViewController.swift +++ b/Example/Example/Examples/AnimationsHierarchy/AnimationsHierarchyViewController.swift @@ -13,6 +13,9 @@ class AnimationsHierarchyViewController: UIViewController { @IBOutlet weak var animView: MacawView! + var startCallbacks: [()->()] = [] + var stopCallbacks: [()->()] = [] + override func viewDidLoad() { super.viewDidLoad() @@ -20,6 +23,22 @@ class AnimationsHierarchyViewController: UIViewController { animView.zoom.enable() } + override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) + + startCallbacks.forEach { + $0() + } + } + + override func viewWillDisappear(_ animated: Bool) { + super.viewWillDisappear(animated) + + stopCallbacks.forEach { + $0() + } + } + func createTree(height: Int) -> Node { let rect = Rect(w: 10, h: 10) @@ -52,7 +71,14 @@ class AnimationsHierarchyViewController: UIViewController { func createLeaf(childForm: Locus, xDelta: Double, yDelta: Double) -> Group { let inset = childForm.bounds().w / 2 let leaf = Shape(form: childForm, fill: Color.teal, place: .move(dx: -inset, dy: -inset)) - leaf.placeVar.animation(angle: 2 * .pi, during: 5).cycle().play() + + let animation = leaf.placeVar.animation(angle: 2 * .pi, during: 5).cycle() + startCallbacks.append({ + animation.play() + }) + stopCallbacks.append({ + animation.stop() + }) let leafGroup = [leaf].group(place: .move(dx: xDelta, dy: yDelta)) leaf.onTap { _ in diff --git a/Source/animation/types/animation_generators/Cache/AnimationCache.swift b/Source/animation/types/animation_generators/Cache/AnimationCache.swift index d54b2c7a..b049f19d 100644 --- a/Source/animation/types/animation_generators/Cache/AnimationCache.swift +++ b/Source/animation/types/animation_generators/Cache/AnimationCache.swift @@ -8,7 +8,7 @@ import AppKit class AnimationUtils { - class func layerForNodeRenderer(_ renderer: NodeRenderer, _ context: AnimationContext, animation: Animation, customBounds: Rect? = .none, shouldRenderContent: Bool = true) -> ShapeLayer { + class func layerForNodeRenderer(_ renderer: NodeRenderer, animation: Animation, customBounds: Rect? = .none, shouldRenderContent: Bool = true) -> ShapeLayer { let node = renderer.node if let cachedLayer = renderer.layer { diff --git a/Source/animation/types/animation_generators/MorphingGenerator.swift b/Source/animation/types/animation_generators/MorphingGenerator.swift index 212cbd9d..09d54277 100644 --- a/Source/animation/types/animation_generators/MorphingGenerator.swift +++ b/Source/animation/types/animation_generators/MorphingGenerator.swift @@ -30,7 +30,7 @@ func addMorphingAnimation(_ animation: BasicAnimation, _ context: AnimationConte let toLocus = morphingAnimation.getVFunc()(animation.autoreverses ? 0.5 : 1.0) let duration = animation.autoreverses ? animation.getDuration() / 2.0 : animation.getDuration() - let layer = AnimationUtils.layerForNodeRenderer(renderer, context, animation: animation, shouldRenderContent: false) + let layer = AnimationUtils.layerForNodeRenderer(renderer, animation: animation, shouldRenderContent: false) // Creating proper animation let generatedAnimation = pathAnimation( diff --git a/Source/animation/types/animation_generators/OpacityGenerator.swift b/Source/animation/types/animation_generators/OpacityGenerator.swift index 71e9441c..9bcdde1b 100644 --- a/Source/animation/types/animation_generators/OpacityGenerator.swift +++ b/Source/animation/types/animation_generators/OpacityGenerator.swift @@ -58,7 +58,7 @@ func addOpacityAnimation(_ animation: BasicAnimation, _ context: AnimationContex completion() } - let layer = AnimationUtils.layerForNodeRenderer(renderer, context, animation: animation) + let layer = AnimationUtils.layerForNodeRenderer(renderer, animation: animation) let animationId = animation.ID layer.add(generatedAnimation, forKey: animationId) animation.removeFunc = { [weak layer] in diff --git a/Source/animation/types/animation_generators/ShapeAnimationGenerator.swift b/Source/animation/types/animation_generators/ShapeAnimationGenerator.swift index 2d3dd9dd..b64023ca 100644 --- a/Source/animation/types/animation_generators/ShapeAnimationGenerator.swift +++ b/Source/animation/types/animation_generators/ShapeAnimationGenerator.swift @@ -30,7 +30,7 @@ func addShapeAnimation(_ animation: BasicAnimation, _ context: AnimationContext, let toShape = shapeAnimation.getVFunc()(animation.autoreverses ? 0.5 : 1.0) let duration = animation.autoreverses ? animation.getDuration() / 2.0 : animation.getDuration() - let layer = AnimationUtils.layerForNodeRenderer(renderer, context, animation: animation, shouldRenderContent: false) + let layer = AnimationUtils.layerForNodeRenderer(renderer, animation: animation, shouldRenderContent: false) // Creating proper animation let generatedAnimation = generateShapeAnimation(context, diff --git a/Source/animation/types/animation_generators/TransformGenerator.swift b/Source/animation/types/animation_generators/TransformGenerator.swift index 98612d1c..bf91385c 100644 --- a/Source/animation/types/animation_generators/TransformGenerator.swift +++ b/Source/animation/types/animation_generators/TransformGenerator.swift @@ -22,7 +22,7 @@ func addTransformAnimation(_ animation: BasicAnimation, _ context: AnimationCont let transactionsDisabled = CATransaction.disableActions() CATransaction.setDisableActions(true) - let layer = AnimationUtils.layerForNodeRenderer(renderer, context, animation: animation, shouldRenderContent: true) + let layer = AnimationUtils.layerForNodeRenderer(renderer, animation: animation, shouldRenderContent: true) // Creating proper animation let generatedAnimation = transformAnimationByFunc(transformAnimation, diff --git a/Source/views/MacawView.swift b/Source/views/MacawView.swift index 0b918eb9..ae01a108 100644 --- a/Source/views/MacawView.swift +++ b/Source/views/MacawView.swift @@ -142,8 +142,14 @@ open class MacawView: MView, MGestureRecognizerDelegate { } private func onZoomChange(t: Transform) { + // TODO: actually we should track all changes placeManager.setZoom(place: t) - self.setNeedsDisplay() + placeManager.setLayout(place: layoutHelper.getTransform(renderer!, contentLayout, bounds.size.toMacaw())) + + if let viewLayer = mLayer { + let deltaTransform = CATransform3DMakeAffineTransform(self.place.toCG()) + viewLayer.transform = CATransform3DConcat(viewLayer.transform, deltaTransform) + } } func initializeView() { @@ -195,11 +201,6 @@ open class MacawView: MView, MGestureRecognizerDelegate { return } renderer.calculateZPositionRecursively() - - // TODO: actually we should track all changes - placeManager.setLayout(place: layoutHelper.getTransform(renderer, contentLayout, bounds.size.toMacaw())) - - ctx.concatenate(self.place.toCG()) renderer.render(in: ctx, force: false, opacity: node.opacity) }