简体   繁体   English

iOS中的动画错误

[英]Animation bug in iOS

I'm trying a simple animation by moving a subclass of UIView when a button is pressed.我正在通过在按下按钮时移动 UIView 的子类来尝试一个简单的动画。 When the button is pressed the view moves xx units away from the original position, the bug is that it comes back to the original position after going xx units away.当按下按钮时,视图从原始位置移动了 xx 个单位,错误是在离开 xx 个单位后它会回到原始位置。 I want it to stop at the new position.我希望它停在新位置。 Whats wrong in my code?我的代码有什么问题?

[UIView animateWithDuration:0.5f animations:^{
    self.analogStick.frame = CGRectOffset(self.analogStick.frame, 0, -10);
}];

Connect the layout constraints as IBOutlets from Interface Builder into the code.将布局约束作为 IBOutlets 从 Interface Builder 连接到代码中。 Then in the beginning of the block, set the constraint's constant to the desired value eg setting the left constraint to superview to its constant - 10 .然后在块的开头,将约束的常量设置为所需的值,例如将左约束设置为 superview its constant - 10

Then, inside the animation block code, just call [self layoutIfNeeded] .然后,在动画块代码中,只需调用[self layoutIfNeeded] Do not set the frame in any way when you are using constraints (you can use 3D transforms though I personally don't recommend it unless you know exactly what you are doing).当您使用的约束,不要设置以任何方式帧(可以使用3D变换,虽然我个人并不推荐这样做,除非你知道自己在做什么)。

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

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