1
1
mirror of https://github.com/exyte/Macaw.git synced 2024-08-16 00:20:23 +03:00

Add correct zoom-scroll handling

This commit is contained in:
Alisa Mylnikova 2020-02-14 14:13:53 +07:00
parent 4d630c7586
commit d9a5b27e94
7 changed files with 39 additions and 12 deletions

View File

@ -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

View File

@ -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 {

View File

@ -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(

View File

@ -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

View File

@ -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,

View File

@ -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,

View File

@ -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)
}