弹跳式动画。当您将其应用于同一属性时
如果我想对 UITableViewCell 进行动画处理,使其从左向右弹跳几次,我该怎么做?我正在尝试:
var bounds = activeCell.Bounds;
var originalLocation = bounds.Location;
var loc = originalLocation;
UIView.Animate(0.2,()=>{
loc.X = originalLocation.X + 20;
activeCell.Bounds = new RectangleF (loc, bounds.Size);
loc.X = originalLocation.X - 20;
activeCell.Bounds = new RectangleF (loc, bounds.Size);
});
它仅对最后一个状态进行动画处理(即将元素向左移动)。我尝试将它们放在单独的 Animate
块中 - 但没有帮助。尝试使用不同的 UIAnimationOptions - 相同。
If I want to animate UITableViewCell so it would bounce from left to right a few times, How can I do that? I'm trying that:
var bounds = activeCell.Bounds;
var originalLocation = bounds.Location;
var loc = originalLocation;
UIView.Animate(0.2,()=>{
loc.X = originalLocation.X + 20;
activeCell.Bounds = new RectangleF (loc, bounds.Size);
loc.X = originalLocation.X - 20;
activeCell.Bounds = new RectangleF (loc, bounds.Size);
});
It animates only the last state (i.e. moves element to the left). I tried to put them in separated Animate
blocks - it didn't help. Tried to use different UIAnimationOptions
- the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一篇很好的文章,解释了如何使其弹跳。
http://khanlou.com/2012/01/cakeyframeanimation-make-it- ounce/
此外,还有用于计算反弹路径的公式的解释。
为了我个人的使用,我采用了计算的绝对值来模拟地面上的反弹。
Here is a nice article explaining how to make it bounce.
http://khanlou.com/2012/01/cakeyframeanimation-make-it-bounce/
Moreover, there is an explanation the formula used to compute the bounce path.
For my personal use, I've taken the absolute value of the computation to simulate a rebound on ground.
您的方法的问题在于 UIView.Animate 将记录您对视图所做的更改,但仅记录它们的最终状态。
如果您在动画块中更改 Bounds 属性一百次,则从动画框架的角度来看,只有最后一个才是重要的。
CoreAnimation 有一些怪癖,WWDC 2010 和 WWDC2011 视频中对此进行了解释。他们有很棒的材料,并且解释了一些不太明显的技巧。
话虽这么说,为 UITableView 中的单元格设置动画是一件复杂的事情,因为您实际上是在研究 UITableView 的内部结构,因此预计会出现各种奇怪的副作用。您可以从 TweetStation 中提取执行该动画并处理各种极端情况的代码。但即使是 TweetStation 和 Twitter for iOS 应用程序也无法做到完美,因为您是在 UIView 后面对事物进行动画处理,而 UIView 会不断更新并更改您正在动画化的相同属性。
The problem with your approach is that UIView.Animate will record the changes that you make to your view, but only the final state for them.
If you change the Bounds property one hundred times in your animate block, only the last one is the one that will matter from the perspective of the animation framework.
CoreAnimation has a couple of quirks that are explained in the WWDC 2010 and WWDC2011 videos. They have great material and they explain a few of the tricks that are not very obvious.
That being said, animating cells in a UITableView is a complicated matter because you are really poking at a UITableView internals, so expect various strange side effects. You could lift the code from TweetStation that does that animation and deals with various corner cases. But even TweetStation and the Twitter for iOS app do not manage to be perfect, because you are animating things behind the back of a UIView that is constantly updating and making changes to very same properties you are animating.
从我的想法来看,最简单的方法是将动画代码放入一个方法中,并根据需要多次递归调用该方法。代码未经测试,但它应该可以工作或者至少给你一个想法。
不过,您也可以使用路径动画。您可以定义一条路径“向右 20 个单位,回到中心,向左 20 个单位,回到中心”,并根据需要重复该动画。
这需要您处理 CAKeyFrameAnimation 并且代码会稍微多一些。
这个网站可以帮助您快速入门:http:// www.bdunagan.com/2009/04/26/core-animation-on-the-iphone/
From the top of my head, the easiest approach would be to put the animation code into a method and call that recursive as often as you want. Code untested, but it should work or at least give you an idea.
You can however also use a path animation. You would define a path "20 units right, back to center, 20 units left, back to center" and repeat that animation as often as you like.
This requires you to deal with CAKeyFrameAnimation and will be slightly more code.
This site can get you jump started: http://www.bdunagan.com/2009/04/26/core-animation-on-the-iphone/
缺乏文档和好的示例有时甚至会让简单的任务变得如此令人恼火的挑战。
这是解决方案
当然代码并不优雅,但它可以工作。希望有一天它能对其他人有所帮助,这样他或她就不需要花半天时间在这样愚蠢的简单事情上
Lack of documentation and good samples sometimes really makes even simple tasks so annoyingly challenging.
Here is the solution
Sure code isn't elegant, but it works. Hope it will someday help somebody else, so he or she wouldn't need to spend half a day on something stupidly simple like that