如何用触摸手势控制CAKeyframeAnimation?

hea*_*kit 6 cocoa-touch core-animation path objective-c caemitterlayer

我有一个CAEmitterLayerbezier路径的动画(封闭形式,就像'8',四个控制点中的一个)CAKeyframeAnimation.现在我想通过沿着(但不一定在路径上)触摸手指的滑动来控制动画.这怎么可能,这甚至可能吗?

Ale*_*ray 1

创建一个CGpoint click;变量来记住您最初的“拖动”点,然后创建一个本地NSEvent处理程序...

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

“yourDelta”是该初始点与当前位置的偏移量...您还可以通过滚动事件获得类似的结果,通过监视NSEventScrollWheelMask...并查看e.deltaXe.deltaY值,通过滚动事件获得类似的结果。

编辑:我不太熟悉 iOS 上的事件处理......但相同的技术可以应用于普通的事件处理程序......即。

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

至于“寻找”你的动画..一种可能的方法是简单地[layer addAnimation:theNewAnimation]使用你以前的toValue,但不是基于fromValue0或你的模型layer......而是使用你的layer.presentationLayer值?如果没有看到您的完整内容,很难说CAKeyframeAnimation