How to control CAKeyframeAnimation with touch gesture?

523 views Asked by At

I have a CAEmitterLayer animated along a bezier path (closed form, like an '8', out of four control points) with a CAKeyframeAnimation. Now I want to control the animation by a slide of a touch-finger along (but not necessarily on) the path. How is this possible and is this even possible?

1

There are 1 answers

1
Alex Gray On

Make a CGpoint click; variable to remember your initial "drag" point, 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 that initial point from the current location... you could also achieve similar results with scroll events, by monitoring NSEventScrollWheelMask... and looking at the e.deltaX and e.deltaY values.

Edit: I'm not as familiar with event handling on iOS.. but the same technique could be applied with normal event handlers... ie.

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

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