更新文本后动画块重置到原始位置 [英] Animation Blocks resets to original position after updating text

查看:27
本文介绍了更新文本后动画块重置到原始位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在测试我的应用程序以发布 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆