简体   繁体   English

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

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

I've a problem that is driving me crazy, and I guess I've not enough knowledge to find an answer to this problem. 我有一个使我发疯的问题,我想我没有足够的知识来找到这个问题的答案。

here a code that is working perfectly : 这里的代码运行良好:

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);

giving me the following output : 给我以下输出:

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

Now, if I put this setCenter into an UIView animationWithDuration, like this 现在,如果我将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);
}];

I'm getting this output : 我得到这个输出:

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

Do you guys have any idea why ? 你们知道为什么吗? I checked the constraints and the autoLayout, everything is fine there. 我检查了约束和autoLayout,一切都很好。

Try this : 尝试这个 :

Objective-C 目标C

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

Swift 4 斯威夫特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)

If you use autolayout, only change center or frame will have no effect, auto layout system will correct it automatically . 如果使用自动布局 ,则仅更改中心或边框无效, 自动布局系统将自动对其进行校正 you need to change your constraints, or not use constraint for your view. 您需要更改约束,或者不对视图使用约束。

Auto layout will activate when layoutSubviews, this normally called at next run loop. 当layoutSubviews(通常在下一个运行循环中调用)时,将激活自动布局。 that's why you first no animated code work. 这就是为什么您首先没有动画代码起作用的原因。 But the animation one callback in the complete block, which have a delay and center already got corrected. 但是在完整块中的动画一个回调,该回调具有延迟并且居中已经得到纠正。

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

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