繁体   English   中英

iOS-在UIView animateWithDuration中无法使用UIView setCenter

[英]iOS - UIView setCenter not working when in an UIView animateWithDuration

我有一个使我发疯的问题,我想我没有足够的知识来找到这个问题的答案。

这里的代码运行良好:

NSLog(@"%f, %f", imageList.center.x, imageList.center.y);
[imageList setCenter:CGPointMake(imageList.center.x, -imageList.center.y)];
NSLog(@"=> %f, %f", imageList.center.x, imageList.center.y);

给我以下输出:

2016-01-08 11:48:42.585 User[4047:600095] 160.000000, 284.000000
2016-01-08 11:48:42.585 User[4047:600095] => 160.000000, -284.000000

现在,如果我将setCenter放入UIView animationWithDuration中,就像这样

NSLog(@"%f, %f", imageList.center.x, imageList.center.y);
[UIView animateWithDuration:0.3 animations:^{
   [imageList setCenter:CGPointMake(imageList.center.x, -imageList.center.y)];  
} completion:^(BOOL finished) {*/
    NSLog(@"=> %f, %f", imageList.center.x, imageList.center.y);
}];

我得到这个输出:

2016-01-08 11:48:42.585 User[4047:600095] 160.000000, 284.000000
2016-01-08 11:48:42.585 User[4047:600095] => 160.000000, 284.000000

你们知道为什么吗? 我检查了约束和autoLayout,一切都很好。

尝试这个 :

目标C

[UIView animateWithDuration:0.3 delay:0.0 options:(UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction) animations:^{
    [imageList setCenter:CGPointMake(imageList.center.x, -imageList.center.y)];
} completion:nil];

斯威夫特4

UIView.animate(withDuration: 0.3, delay: 0.0, options: .curveLinear, animations: {
    imageList.center = CGPoint(x: imageList.center.x, y: -imageList.center.y)
}, completion: nil)

如果使用自动布局 ,则仅更改中心或边框无效, 自动布局系统将自动对其进行校正 您需要更改约束,或者不对视图使用约束。

当layoutSubviews(通常在下一个运行循环中调用)时,将激活自动布局。 这就是为什么您首先没有动画代码起作用的原因。 但是在完整块中的动画一个回调,该回调具有延迟并且居中已经得到纠正。

暂无
暂无

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

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