更新文本后动画块重置到原始位置

编程入门 行业动态 更新时间:2024-10-12 22:28:48
本文介绍了更新文本后动画块重置到原始位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我目前正在测试我的应用程序以发布 IOS 8.我注意到在我执行动画块后,如果我更新任何标签的文本,动画会重置.我用下面显示的一种方法运行了一个简单的例子.运行此示例会产生以下结果:

I'm currently testing my apps for the release of IOS 8. I noticed that after I performed an animation block, the animation resets if I update the text of any label. I ran a simple example with one method shown below. Running this example results in the following:

第一次点击 myButton - 动画运行但在标签文本更改时重置.第二次点击 myButton - 动画运行但不会重置到原始位置. Clicking myButton the first time- animation runs but resets when the label text is changed. Clicking myButton the second time - animation runs but does not reset to original position.

这似乎是因为标签文本没有改变.如果我完全删除更新文本的行,这也会阻止动画在最后重置.

It seems like this happens because the label text doesn't change. If I completely remove the line updating the text, this also stops the animation from resetting at the end.

我想修复这个问题,以便在方法运行时,可以在不重置动画的情况下更新标签文本.

I would like to fix this so that when the method runs, the label text can be updated without resetting the animation.

- (IBAction)move:(id)sender {

[UIView animateWithDuration:0.4 delay:0.0
                 options:UIViewAnimationOptionBeginFromCurrentState
                 animations:^{
                     self.myButton.center = CGPointMake(200, 300);
                 }completion:^(BOOL finished){

                     if(finished){
                         self.myLabel.text=@"moved";
                     }

                 }];
}

推荐答案

这个问题可能是因为在 UIView 上设置了自动布局.严格来说,如果您使用的是自动布局,那么您不应该为对象的绝对位置设置动画——而应该为它们的约束设置动画.

This problem can be caused by having Auto Layout set on the UIView. Strictly speaking, if you're using Auto Layout, then you shouldn't animate the absolute position of objects -- you should animate their constraints instead.

在动画开始后更改标签文本会触发布局刷新,iOS 会随机调整周围的所有内容以符合原始视图约束.(我怀疑这是 iOS7 的行为变化).

Changing the label text once your animation is underway triggers a layout refresh, and iOS shuffles everything around to comply with the original view constraints. (I suspect this is a behavioural change from iOS7).

快速修复:取消选中视图上的自动布局,这应该会按预期工作.

Quick fix: un-check Auto Layout on the View, and this should work as expected.

这篇关于更新文本后动画块重置到原始位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-21 01:15:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/993831.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:原始   文本   位置   动画

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!