gpt4 book ai didi

ios - 自动布局违反平移旋转动画

转载 作者:可可西里 更新时间:2023-11-01 05:12:55 26 4
gpt4 key购买 nike

我有 UIView,我想通过旋转为其设置动画。我也想使用自动布局。我已经看到了很多 stackoverflow 常见问题,但我没有找到任何适用的解决方案。那么,让我们从我的界面和动画代码开始吧;我有一个带有图像的 UIView,必须旋转它。现在我有一个按钮,可以激活旋转。在屏幕截图中你可以看到红叉,这一定是旋转中心(现在它在图像上,但我想在旋转的 UIView 之外制作旋转中心,我知道这可以用 AnchorPoint 存档)。

enter image description here

这是我的旋转动画代码:

#define ROTATE_DURATION 3.0

- (IBAction)rotateArrow {

CGAffineTransform transform = self.hand.transform;

NSLog(@"layer possition before animation x: %f; y: %f",self.hand.layer.position.x,self.hand.layer.position.y);

[UIView animateWithDuration:ROTATE_DURATION/3 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{

self.hand.transform = CGAffineTransformRotate(transform, 2*M_PI/3) ;
[self.hand layoutIfNeeded];

NSLog(@"layer possition after animation 1 x: %f; y: %f",self.hand.layer.position.x,self.hand.layer.position.y);
}
completion:^(BOOL finished) {
[UIView animateWithDuration:ROTATE_DURATION/3 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{

self.hand.transform = CGAffineTransformRotate(transform, -2*M_PI/3) ;
[self.hand layoutIfNeeded];

NSLog(@"layer possition after animation 2 x: %f; y: %f",self.hand.layer.position.x,self.hand.layer.position.y);
}
completion:^(BOOL finished) {
[UIView animateWithDuration:ROTATE_DURATION/3 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{

self.hand.transform = CGAffineTransformRotate(transform, 0) ;
[self.hand layoutIfNeeded];

NSLog(@"layer possition after animation 3 x: %f; y: %f",self.hand.layer.position.x,self.hand.layer.position.y);
}
completion:^(BOOL finished) {

}];
}];
}];



}

所以,问题是什么:当旋转正在进行时,UIView 会更改它的 centerlayer.position 属性,这就是我的 UIView 在动画时“跳跃”的原因。如果关闭自动布局,则动画没问题。我看了 WWDC 2012“Auto Layout by Example”,发现我会使用 [self.hand layoutIfNeeded]; 一切都很好,但根本不是。动画变得更流畅了,但我看到了这个“跳跃”。所以,这是我的“输出”。

enter image description here

动画时,UIView 会像您在图像上看到的那样向右移动,然后回到正常位置。我该如何解决这个“跳跃”?

这是一个日志:

 layer possition before animation x: 160.000000; y: 99.500000
layer possition after animation 1 x: 197.349030; y: 114.309601
layer possition after animation 2 x: 197.349030; y: 114.309601
layer possition after animation 3 x: 160.000000; y: 99.500000

谢谢。

最佳答案

当我布局我的 View 时,一切都开始正常工作。当您进行转换时,请务必记住,自动布局始终会根据您设置的约束计算您的 View 边界和框架。所以,在我的例子中,我只是在我的旋转 View 中添加了中心垂直和水平对齐、宽度和高度,所以自动布局机制知道我的 View 到底在什么时候。一切顺利。这是一个很好的自动布局教程:

http://www.raywenderlich.com/50319/beginning-auto-layout-tutorial-in-ios-7-part-1

关于ios - 自动布局违反平移旋转动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19917420/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com