How to control CAKeyframeAnimation with a touch gesture?

I have a CAEmitterLayer animated along a bezier path (closed form, like "8", of four breakpoints) with CAKeyframeAnimation . Now I want to control the animation using a finger slide with an end (but not necessarily on). How is this possible and is it possible?

+6
source share
1 answer

Make the CGpoint click; variable CGpoint click; to remember the start point of the drag, then create a local NSEvent ... handler

 [NSEvent addLocalMonitorForEventsMatchingMask: NSMouseMovedMask | NSLeftMouseDownMask handler:^(NSEvent *e) { if ( e.type == NSLeftMouseDown ) click = e.locationInWindow; else "yourDelta" = click - e.locationInWindow; // pseudoCode return e; }]; 

"yourDelta" is the offset of this starting point from the current location ... you can also achieve similar results with scroll events by tracking NSEventScrollWheelMask ... and looking at the values โ€‹โ€‹of e.deltaX and e.deltaY .

Edit: I am not so familiar with event handling on iOS .. but the same method can be applied with regular event handlers ... i.e.

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)e { click = [e locationInView:_yourView]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent*)e { "yourDelta" = click - [e locationInView:_yourView]; // pseudoCode } 

Regarding the "search" of your animation ... One possible way is simply [layer addAnimation:theNewAnimation] using the previous toValue , but instead of basing fromValue 0, or your layer model. use layer.presentationLayer ? It's hard to say without seeing the full contents of your CAKeyframeAnimation .

+1
source

Source: https://habr.com/ru/post/917452/


All Articles