如何使用按钮修改 xcode 中 UIView 的动画持续时间
我希望有人能提供帮助。我有一个相当标准的 UIView 动画,其中唯一真正的动画是设置在循环上的 CGAffineTransformMakeScale。我想要两个按钮,一个用于增加缩放速率(或更准确地减少动画持续时间),另一个按钮用于降低缩放速率(或更准确地增加动画持续时间)。
这可能吗?如果这是显而易见的,我深表歉意,但我是一个新手,正在寻求建议 - 即使它是定向阅读。让我知道我是否应该提供任何进一步的信息来提供帮助。
非常感谢!
I'm hoping someone can help. I have a fairly standard UIView animation where the only real animation is a CGAffineTransformMakeScale that is set on a loop. I want to have two buttons, one that increases the rate (or more accurately reduces the animation duration) of scale and one that decreases the rate (or more accurately increases the animation duration) of the scale.
Is this even possible? I apologise if this is obvious but I am a novice and looking for advice - even if it's directed reading. Let me know if I should supply any further info to help.
Many thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道修改正在进行的动画的方法。
我认为这里的代码可以满足您的要求。它使用增加/减少按钮来更改 ivar
animationDuration
,并手动将动画分为两半(前半部分和后半部分)循环。每次新动画(向前或向后)开始时,它都会及时获取该时间点的animationDuration
值,因此对于短动画来说,它似乎会立即发生变化。然而,它对于长时间动画效果不佳,因为它实际上只改变动画最大/最小点处的动画速度。如果您想更频繁地更新,您可以将动画分解得更小 - 例如将其分成 4 而不是 2 (1 -> 1.5, 1.5 -> 2.0, 2.0 -> 1.5, 1.5 -> 1.0) 或如果您需要的话,甚至更多。
标头 (MyViewController.h):
实现 (MyViewController.m):
I don't know of a way to modify an in-progress animations.
Here's code that I think does what you want. It uses the increase/decrease buttons to change an ivar
animationDuration
, and manually loops the animation as two halves (a forward half and a reverse half). Each time the new animation (forwards or reverse) starts, it gets the value foranimationDuration
at that point in time, so for short animations it will appear to change pretty much instantly. It would not work well for long duration animations however, as it actually only changes the animation speed at the max/min points of the animation.You could break the animation up even smaller if you wanted to update more frequently - e.g. split it into 4 instead of 2 (1 -> 1.5, 1.5 -> 2.0, 2.0 -> 1.5, 1.5 -> 1.0) or even more if you need it.
Header (MyViewController.h):
Implementation (MyViewController.m):
您想要 UIView 类的 setAnimationDuration:
这是相关文档:
https://developer.apple.com/library/ios/#documentation/uikit/reference/UIView_Class/UIView/UIView.html#//apple_ref/文档/uid/TP40006816-CH3-SW62
You want setAnimationDuration of the UIView class:
Here's the relevant documentation:
https://developer.apple.com/library/ios/#documentation/uikit/reference/UIView_Class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-SW62