简体   繁体   English

iOS - 如何制作动画轨迹

[英]iOS - How to make an animation track touches

What is the best way to implement a smooth reversing animation which tracks touches? 实现跟踪触摸的平滑倒车动画的最佳方法是什么? I am referring to those animations in which, for example, if the user executes a swipe gesture some elements smoothly animate on screen, and others off, but if the user instead slowly drags a pan gesture back and forth the same objects will move forward/backward as a percent in accordance with the touch position. 我指的是那些动画,其中,例如,如果用户执行滑动手势,一些元素在屏幕上平滑地动画,而其他元素关闭, 如果用户反而慢慢地来回拖动平移手势,则相同的对象将向前移动/按照触摸位置向后移动百分比。 This is seen in many app intros and also in transitions. 这可以在许多应用程序介绍中看到,也可以在转换中看到。 I have found 我已经发现

What I don't grasp - and I'm comfortable using CAAnimations and gestures - is how something can be both animated and interactive . 我不掌握-我习惯使用CAAnimations和手势-是怎样的东西既可以是动画 互动

Typically, when I create an animation, I commit the animation and it goes from start to finish. 通常,当我创建动画时,我提交动画,它从头到尾。 While I could interrupt the animation as touches continue, that seems like it would be stilted. 虽然我可以在触摸继续时中断动画,但这似乎是笨拙的。

On the other hand, moving things in response to user input is easy, but that is not animated. 另一方面,响应用户输入移动事物很容易,但这不是动画。

How is the effect achieved where something can change according to an animation, but also have that exact same animation occur tied to touches, and yet still also have it so that although the animation reaches completion it doesn't really "finish" (become irreversible) unless the user releases touch, while at any point during interaction if the user releases panning then the animation either reverts backwards to its starting position or animates to completion depending on the last touch location and velocity. 如果某些内容可以根据动画发生变化,效果如何,但是也可以将完全相同的动画与触摸相关联, 但仍然可以使动画达到完成状态,但它并没有真正“完成”(变得不可逆转) )除非用户释放触摸,否则在交互期间的任何时刻如果用户释放平移,则动画要么向后恢复到其起始位置,要么根据最后的触摸位置和速度动画到完成。 These requirements are baffling. 这些要求令人费解。

The glimpses of this technique I see all involve keyframe animations, but what I don't understand is where the touch events intersect with an animation to create these smooth effects I see. 这种技术我看到所有的一瞥涉及关键帧动画,但我不明白的是,其中触摸事件与动画相交创建这些顺滑的效果我看。

Any tips, examples, or tutorials are most welcome. 我们非常欢迎任何提示,示例或教程。

What I don't grasp - and I'm comfortable using CAAnimations and gestures - is how something can be both animated and interactive. 我无法掌握的 - 我很自在地使用CAAnimations和手势 - 是什么东西既可以动画也可以互动。

It is because, having set up an animation, you can set that animation to any "frame" you wish. 这是因为,在设置动画后,您可以将该动画设置为您想要的任何“帧”。 Thus you can track the animation in correspondence to the movement of a gesture. 因此,您可以根据手势的移动跟踪动画。

The way this works is that an animation is a feature of the render tree, belonging to a CALayer. 这种方式的工作方式是动画是渲染树的一个特征,属于CALayer。 CALayer implements the CAMediaTiming protocol. CALayer实现CAMediaTiming协议。 The timeOffset of a CALayer thus determines what "frame" of an animation that layer displays. 因此,CALayer的timeOffset确定该图层显示的动画的“帧”。 If a complex animation involves many different layers, no problem; 如果一个复杂的动画涉及许多不同的层,没有问题; just set the timeOffset of their mutual superlayer, to control the frame of the entire animation. 只需设置其相互超层的timeOffset ,即可控制整个动画的帧。

That in fact is exactly how the new iOS 7 interactive custom transition feature works (to which you rightly refer in your question). 事实上,这正是新的iOS 7交互式自定义转换功能的工作方式(您在问题中正确引用)。 For instance, in this example code: 例如,在此示例代码中:

https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/iOS7bookExamples/bk2ch06p296customAnimation2/ch19p620customAnimation1/AppDelegate.m https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/iOS7bookExamples/bk2ch06p296customAnimation2/ch19p620customAnimation1/AppDelegate.m

... I keep updating the UIPercentDrivenInteractiveTransition to tell it how far through the gesture the user is, and the animation therefore tracks the gesture. ...我不断更新UIPercentDrivenInteractiveTransition,告诉它用户的手势有多远,动画因此跟踪手势。 Now ask yourself: how the heck is this possible??? 现在问问自己:这怎么可能?

Well, the UIPercentDrivenInteractiveTransition, in turn, behind the scenes, keeps adjusting the layer's timeOffset to portray the animation at the corresponding frame. 嗯,UIPercentDrivenInteractiveTransition反过来在幕后,不断调整图层的timeOffset来描绘相应帧的动画。 (You can actually add logging code to my example to see that this is true.) (您实际上可以在我的示例中添加日志记录代码,以确保这是真的。)

Moreover, when I end the gesture at an incomplete point, the animation either hurries to its end or runs backwards to its beginning - again, this is because of the CAMediaTiming protocol, which lets you change the speed of the animation, including a negative value to run it backwards. 此外,当我在一个不完整的点结束手势时,动画要么快速结束或者向后跑到它的开始 - 再次,这是因为CAMediaTiming协议,它允许你改变动画的speed ,包括负值向后运行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM