簡體   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